nginx-0.0.1-2003-11-13-09:14:05 import
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 98404a0..2038e7d 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -70,7 +70,9 @@
     ngx_log_t        *log;
     ngx_cycle_t      *cycle;
     ngx_open_file_t  *file;
+#if !(WIN32)
     ngx_core_conf_t  *ccf;
+#endif
 
 #if (NGX_DEBUG) && (__FreeBSD__)
 #if __FreeBSD_version >= 500014
@@ -83,6 +85,8 @@
 
     /* TODO */ ngx_max_sockets = -1;
 
+    ngx_init_time();
+
     log = ngx_log_init_errlog();
 
     if (ngx_os_init(log) == NGX_ERROR) {
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 8bda9e9..6dda801 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -60,7 +60,6 @@
 #endif
 {
     char      errstr[MAX_ERROR_STR];
-    ngx_tm_t  tm;
     size_t    len;
 #if (HAVE_VARIADIC_MACROS)
     va_list   args;
@@ -73,10 +72,17 @@
         return;
     }
 
+    ngx_memcpy(errstr, ngx_cached_err_log_time.data,
+               ngx_cached_err_log_time.len);
+
+    len = ngx_cached_err_log_time.len;
+
+#if 0
     ngx_localtime(&tm);
     len = ngx_snprintf(errstr, sizeof(errstr), "%4d/%02d/%02d %02d:%02d:%02d",
                        tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday,
                        tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
+#endif
 
     len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
                         " [%s] ", err_levels[level]);
diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c
index fcc7152..e215243 100644
--- a/src/core/ngx_times.c
+++ b/src/core/ngx_times.c
@@ -5,6 +5,11 @@
 
 time_t       ngx_cached_time;
 
+ngx_tm_t     ngx_cached_gmtime;
+
+static char  cached_err_log_time[] = "1970/09/28 12:00:00";
+ngx_str_t    ngx_cached_err_log_time;
+
 static char  cached_http_time[] = "Mon, 28 Sep 1970 06:00:00 GMT";
 ngx_str_t    ngx_cached_http_time;
 
@@ -12,56 +17,62 @@
 ngx_str_t    ngx_cached_http_log_time;
 
 
+static char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fir", "Sat" };
+static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+
+void ngx_init_time()
+{
+    struct timeval  tv;
+
+    ngx_memzero(&ngx_cached_gmtime, sizeof(ngx_tm_t));
+#ifdef ngx_tm_zone
+    ngx_cached_gmtime.ngx_tm_zone = "GMT";
+#endif
+
+    ngx_cached_err_log_time.data = cached_err_log_time;
+    ngx_cached_http_time.data = cached_http_time;
+    ngx_cached_http_log_time.data = cached_http_log_time;
+
+    ngx_gettimeofday(&tv);
+    ngx_cached_time = tv.tv_sec;
+    ngx_time_update();
+}
+
+
 time_t ngx_time()
 {
     return ngx_cached_time;
 }
 
 
-/* TODO:
- *   cache ngx_tm_t
- *   write own gmtime()
- *   remove strftime()
- *   we can remove localtime_r
- */
-
 void ngx_time_update()
 {
-    ngx_tm_t     *tp, tm;
-    static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
-                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+    ngx_tm_t  tm;
 
-    /* STUB: need to move to ngx_init_time() */
-    ngx_cached_http_time.data = cached_http_time;
-    ngx_cached_http_log_time.data = cached_http_log_time;
+    ngx_gmtime(ngx_cached_time, &ngx_cached_gmtime);
 
-#if 0
-
-    days = sec / 86400;
-    days = days - 31 - 28 + 719527;
-
-    year = days * 400 / (365 * 400 + 100 - 4 + 1);
-    yday = days - (365 * year + year / 4 - year / 100 + year / 400);
-
-    month = (yday + 30) * 12 / 367;
-    mday = yday - (month * 367 / 12 - 31);
-
-    if (++month > 11) {
-        month -= 12;
-        year++;
-    }
-
-#endif
-
-    tp = gmtime(&ngx_cached_time);
-
-    ngx_cached_http_time.len = strftime(ngx_cached_http_time.data,
-                                        sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
-                                        "%a, %d %b %Y %H:%M:%S GMT", tp);
-
+    ngx_cached_http_time.len = ngx_snprintf(ngx_cached_http_time.data,
+                                       sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
+                                       "%s, %02d %s %4d %02d:%02d:%02d GMT",
+                                       week[ngx_cached_gmtime.ngx_tm_wday],
+                                       ngx_cached_gmtime.ngx_tm_mday,
+                                       months[ngx_cached_gmtime.ngx_tm_mon - 1],
+                                       ngx_cached_gmtime.ngx_tm_year,
+                                       ngx_cached_gmtime.ngx_tm_hour,
+                                       ngx_cached_gmtime.ngx_tm_min,
+                                       ngx_cached_gmtime.ngx_tm_sec);
 
     ngx_localtime(&tm);
 
+    ngx_cached_err_log_time.len = ngx_snprintf(ngx_cached_err_log_time.data,
+                                       sizeof("1970/09/28 12:00:00"),
+                                       "%4d/%02d/%02d %02d:%02d:%02d",
+                                       tm.ngx_tm_year, tm.ngx_tm_mon,
+                                       tm.ngx_tm_mday, tm.ngx_tm_hour,
+                                       tm.ngx_tm_min, tm.ngx_tm_sec);
+
     ngx_cached_http_log_time.len = ngx_snprintf(ngx_cached_http_log_time.data,
                                        sizeof("28/Sep/1970:12:00:00"),
                                        "%02d/%s/%d:%02d:%02d:%02d",
@@ -72,3 +83,90 @@
                                        tm.ngx_tm_min,
                                        tm.ngx_tm_sec);
 }
+
+
+size_t ngx_http_time(char *buf, time_t t)
+{
+    ngx_tm_t  tm;
+
+    ngx_gmtime(t, &tm);
+
+    return ngx_snprintf(buf, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"),
+                                       "%s, %02d %s %4d %02d:%02d:%02d GMT",
+                                       week[tm.ngx_tm_wday],
+                                       tm.ngx_tm_mday,
+                                       months[tm.ngx_tm_mon - 1],
+                                       tm.ngx_tm_year,
+                                       tm.ngx_tm_hour,
+                                       tm.ngx_tm_min,
+                                       tm.ngx_tm_sec);
+}
+
+
+void ngx_gmtime(time_t t, ngx_tm_t *tp)
+{
+    int  sec, min, hour, mday, mon, year, wday, yday, days;
+
+    days = t / 86400;
+
+    /* Jaunary 1, 1970 was Thursday */
+    wday = (4 + days) % 7;
+
+    t %= 86400;
+    hour = t / 3600;
+    t %= 3600;
+    min = t / 60; 
+    sec = t % 60;
+
+    /* the algorithm based on Gauss's formula */
+    
+    days = days - (31 + 28) + 719527;
+
+    year = days * 400 / (365 * 400 + 100 - 4 + 1);
+    yday = days - (365 * year + year / 4 - year / 100 + year / 400);
+
+    mon = (yday + 31) * 12 / 367;
+    mday = yday - (mon * 367 / 12 - 31);
+
+    mon += 2;
+
+    if (yday >= 306) {
+        yday -= 306;
+        year++;
+        mon -= 12;
+
+        if (mday == 0) {
+            /* Jaunary 31 */
+            mon = 1;
+            mday = 31;
+
+        } else if (mon == 2) {
+
+            if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
+                if (mday > 29) {
+                    mon = 3;
+                    mday -= 29;
+                }
+
+            } else if (mday > 28) {
+                mon = 3;
+                mday -= 28;
+            }
+        }
+
+    } else {
+        yday += 31 + 28;
+
+        if ((year % 4 == 0) && (year % 100 || (year % 400 == 0))) {
+             yday++;
+        }
+    }
+
+    tp->ngx_tm_sec = sec;
+    tp->ngx_tm_min = min;
+    tp->ngx_tm_hour = hour;
+    tp->ngx_tm_mday = mday;
+    tp->ngx_tm_mon = mon;
+    tp->ngx_tm_year = year;
+    tp->ngx_tm_wday = wday;
+}
diff --git a/src/core/ngx_times.h b/src/core/ngx_times.h
index 2cbcc72..eb618cd 100644
--- a/src/core/ngx_times.h
+++ b/src/core/ngx_times.h
@@ -6,11 +6,15 @@
 #include <ngx_core.h>
 
 
+void ngx_init_time();
 time_t ngx_time();
 void ngx_time_update();
+size_t ngx_http_time(char *buf, time_t t);
+void ngx_gmtime(time_t t, ngx_tm_t *tp);
 
 
 extern time_t     ngx_cached_time;
+extern ngx_str_t  ngx_cached_err_log_time;
 extern ngx_str_t  ngx_cached_http_time;
 extern ngx_str_t  ngx_cached_http_log_time;