00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 module mango.sys.Epoch;
00041
00042
00043
00044
00045
00046
00047
00048
00049 version (Win32)
00050 {
00051 private import std.c.windows.windows;
00052 }
00053
00054 version (Posix)
00055 {
00056 version (linux)
00057 private import std.c.linux.linux;
00058
00059 version (darwin)
00060 private import std.c.darwin.darwin;
00061
00062 extern (C) int mktime(tm *);
00063 extern (C) tm *gmtime(int *);
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 abstract class Epoch
00078 {
00079 private static long beginTime;
00080
00081 const long InvalidEpoch = -1;
00082
00083
00084
00085
00086
00087
00088
00089
00090 struct Fields
00091 {
00092 int year,
00093 month,
00094 day,
00095 hour,
00096 min,
00097 sec,
00098 ms,
00099 dow;
00100
00101
00102
00103
00104
00105
00106 private static char[][] Days =
00107 [
00108 "Sunday",
00109 "Monday",
00110 "Tuesday",
00111 "Wednesday",
00112 "Thursday",
00113 "Friday",
00114 "Saturday",
00115 ];
00116
00117
00118
00119
00120
00121 private static char[][] Months =
00122 [
00123 "null",
00124 "January",
00125 "February",
00126 "March",
00127 "April",
00128 "May",
00129 "June",
00130 "July",
00131 "August",
00132 "September",
00133 "October",
00134 "November",
00135 "December",
00136 ];
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 void setDate (int year, int month, int day, int dow = 0)
00150 {
00151 this.year = year;
00152 this.month = month;
00153 this.day = day;
00154 this.dow = dow;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void setTime (int hour, int min, int sec, int ms = 0)
00169 {
00170 this.hour = hour;
00171 this.min = min;
00172 this.sec = sec;
00173 this.ms = ms;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182 char[] toDowName ()
00183 {
00184 return Days[dow];
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 char[] toMonthName ()
00194 {
00195 return Months[month];
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 version (Win32)
00205 {
00206
00207
00208
00209
00210
00211
00212
00213 long getUtcTime ()
00214 {
00215 SYSTEMTIME sTime;
00216 FILETIME fTime;
00217
00218 sTime.wYear = year;
00219 sTime.wMonth = month;
00220 sTime.wDayOfWeek = 0;
00221 sTime.wDay = day;
00222 sTime.wHour = hour;
00223 sTime.wMinute = min;
00224 sTime.wSecond = sec;
00225 sTime.wMilliseconds = ms;
00226
00227 SystemTimeToFileTime (&sTime, &fTime);
00228
00229 return fromFileTime (&fTime);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239 void setUtcTime (long time)
00240 {
00241 SYSTEMTIME sTime;
00242 FILETIME fTime;
00243
00244 toFileTime (&fTime, time);
00245 FileTimeToSystemTime (&fTime, &sTime);
00246
00247 year = sTime.wYear;
00248 month = sTime.wMonth;
00249 day = sTime.wDay;
00250 hour = sTime.wHour;
00251 min = sTime.wMinute;
00252 sec = sTime.wSecond;
00253 ms = sTime.wMilliseconds;
00254 dow = sTime.wDayOfWeek;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264 void setLocalTime (long time)
00265 {
00266 FILETIME fTime,
00267 local;
00268
00269 toFileTime (&fTime, time);
00270 FileTimeToLocalFileTime (&fTime, &local);
00271 setUtcTime (fromFileTime (&local));
00272 }
00273 }
00274
00275
00276
00277
00278
00279
00280
00281
00282 version (Posix)
00283 {
00284
00285
00286
00287
00288
00289
00290
00291 long getUtcTime ()
00292 {
00293 tm t;
00294
00295 t.tm_year = year - 1900;
00296 t.tm_mon = month - 1;
00297 t.tm_mday = day;
00298 t.tm_hour = hour;
00299 t.tm_min = min;
00300 t.tm_sec = sec;
00301 return 1000L * cast(long) mktime(&t) + ms;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 void setUtcTime (long time)
00313 {
00314 ms = time % 1000;
00315 int utc = time / 1000;
00316
00317 synchronized (lock)
00318 {
00319 tm* t = gmtime (&utc);
00320 assert (t);
00321
00322 year = t.tm_year + 1900;
00323 month = t.tm_mon + 1;
00324 day = t.tm_mday;
00325 hour = t.tm_hour;
00326 min = t.tm_min;
00327 sec = t.tm_sec;
00328 dow = t.tm_wday;
00329 }
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339 void setLocalTime (long time)
00340 {
00341 ms = time % 1000;
00342 int utc = time / 1000;
00343
00344 synchronized (lock)
00345 {
00346 tm* t = localtime (&utc);
00347 year = t.tm_year + 1900;
00348 month = t.tm_mon + 1;
00349 day = t.tm_mday;
00350 hour = t.tm_hour;
00351 min = t.tm_min;
00352 sec = t.tm_sec;
00353 dow = t.tm_wday;
00354 }
00355 }
00356 }
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366 final static long startTime ()
00367 {
00368 return startTime;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378 version (Win32)
00379 {
00380 private static long epochOffset;
00381
00382
00383
00384
00385
00386
00387
00388
00389 static long utcTime ()
00390 {
00391 FILETIME fTime;
00392
00393 GetSystemTimeAsFileTime (&fTime);
00394 return fromFileTime (&fTime);
00395 }
00396
00397
00398
00399
00400
00401
00402
00403 static int tzMinutes ()
00404 {
00405 TIME_ZONE_INFORMATION tz;
00406
00407 int ret = GetTimeZoneInformation (&tz);
00408 return -tz.Bias;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 static this ()
00418 {
00419 SYSTEMTIME sTime;
00420 FILETIME fTime;
00421
00422
00423 sTime.wYear = 1970;
00424 sTime.wMonth = 1;
00425 sTime.wDayOfWeek = 0;
00426 sTime.wDay = 1;
00427 sTime.wHour = 0;
00428 sTime.wMinute = 0;
00429 sTime.wSecond = 0;
00430 sTime.wMilliseconds = 0;
00431 SystemTimeToFileTime (&sTime, &fTime);
00432
00433 epochOffset = (cast(long) fTime.dwHighDateTime) << 32 |
00434 fTime.dwLowDateTime;
00435 beginTime = utcTime();
00436 }
00437
00438
00439
00440
00441
00442
00443
00444 private static long fromFileTime (FILETIME* ft)
00445 {
00446 long tmp = (cast(long) ft.dwHighDateTime) << 32 |
00447 ft.dwLowDateTime;
00448
00449
00450 return (tmp - epochOffset) / 10_000;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459 private static void toFileTime (FILETIME* ft, long et)
00460 {
00461 et = et * 10_000 + epochOffset;
00462
00463 ft.dwHighDateTime = et >> 32;
00464 ft.dwLowDateTime = et & 0xFFFFFFFF;
00465 }
00466 }
00467
00468
00469
00470
00471
00472
00473 version (Posix)
00474 {
00475
00476 extern static int timezone;
00477 extern static int daylight;
00478
00479 private static Object lock;
00480
00481
00482
00483
00484
00485
00486
00487 static this()
00488 {
00489 lock = new Object;
00490 beginTime = utcTime();
00491 }
00492
00493
00494
00495
00496
00497
00498
00499
00500 static long utcTime ()
00501 {
00502 timeval tv;
00503
00504 if (gettimeofday (&tv, null))
00505 throw new Exception ("linux timer is not available");
00506
00507 return 1000L * cast(long) tv.tv_sec + tv.tv_usec / 1000;
00508 }
00509
00510
00511
00512
00513
00514
00515
00516 static int tzMinutes ()
00517 {
00518
00519 return timezone / 60;
00520 }
00521 }
00522 }