/platform/unix/scaffold/time.d

http://github.com/wilkie/djehuty · D · 160 lines · 119 code · 27 blank · 14 comment · 9 complexity · 02aa7ffdc7b16df8e008ec2ed94558df MD5 · raw file

  1. /*
  2. * time.d
  3. *
  4. * This Scaffold holds the Time implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. * Originated: May 19th, 2009
  8. *
  9. */
  10. module scaffold.time;
  11. import platform.unix.common;
  12. import core.definitions;
  13. import core.string;
  14. import core.date;
  15. import core.time;
  16. // Timing
  17. private {
  18. struct tm {
  19. int tm_sec;
  20. int tm_min;
  21. int tm_hour;
  22. int tm_mday;
  23. int tm_mon;
  24. int tm_year;
  25. int tm_wday;
  26. int tm_yday;
  27. int tm_isdst;
  28. char* tm_zone;
  29. int tm_gmtoff;
  30. }
  31. extern(C) tm* gmtime(time_t* tim);
  32. extern(C) tm* gmtime_r(time_t* tim, tm* output);
  33. extern(C) tm* localtime(time_t* tim);
  34. extern(C) tm* localtime_r(time_t* tim, tm* output);
  35. extern(C) size_t strftime(char*, size_t, char*, tm*);
  36. extern(C) void* memcpy(void*, void*, size_t);
  37. extern(C) time_t mktime(tm*);
  38. extern(C) void tzset();
  39. extern(C) {
  40. extern char* tzname[2];
  41. }
  42. }
  43. string TimeZoneGet() {
  44. int foo;
  45. timeval val;
  46. gettimeofday(&val, null);
  47. tm LOCAL;
  48. tm GMT;
  49. gmtime_r(cast(time_t*)&val.tv_sec, &GMT);
  50. localtime_r(cast(time_t*)&val.tv_sec, &LOCAL);
  51. // Get GMT difference
  52. // Which is earliest?
  53. time_t gmt_time = mktime(&GMT);
  54. time_t local_time = mktime(&LOCAL);
  55. time_t diff = local_time - gmt_time;
  56. int diff_min = diff % (60*60);
  57. int diff_hr = diff / 60 / 60;
  58. char[] strret = new char[128];
  59. strret[0..3] = "UTC";
  60. size_t len = sprintf(&strret[3], "%d", diff_hr);
  61. strret[3+len] = ':';
  62. len+=4;
  63. if (diff_min < 10) {
  64. strret[len] = '0';
  65. len++;
  66. }
  67. if (diff_min > 0) {
  68. len = sprintf(&strret[len], "%d", diff_min);
  69. }
  70. else {
  71. strret[len] = '0';
  72. len++;
  73. }
  74. strret = strret[0..len];
  75. if (tzname[0] !is null) {
  76. len = strlen(tzname[0]);
  77. char[] tzstr = new char[len];
  78. tzstr[0..tzstr.length] = tzname[0][0..tzstr.length];
  79. switch(tzstr[0..len]) {
  80. case "EST":
  81. if (strret == "UTC-5:00") {
  82. return "Eastern Standard Time";
  83. }
  84. break;
  85. case "PST":
  86. if (strret == "UTC-8:00") {
  87. return "Pacific Standard Time";
  88. }
  89. break;
  90. default:
  91. return strret;
  92. }
  93. }
  94. return strret;
  95. }
  96. // getting microseconds
  97. long TimeGet() {
  98. timeval val;
  99. gettimeofday(&val, null);
  100. tm time_struct;
  101. gmtime_r(cast(time_t*)&val.tv_sec, &time_struct);
  102. // get microseconds
  103. long micros;
  104. micros = time_struct.tm_sec + (time_struct.tm_min * 60);
  105. micros += time_struct.tm_hour * 60 * 60;
  106. micros *= 1000000;
  107. micros += val.tv_usec;
  108. return micros;
  109. }
  110. ulong SystemTimeGet() {
  111. timeval val;
  112. gettimeofday(&val, null);
  113. return (val.tv_sec * 1000000) + val.tv_usec;
  114. }
  115. float gettimeofday_difference(timeval *b, timeval *a) {
  116. timeval diff_tv;
  117. diff_tv.tv_usec = b.tv_usec - a.tv_usec;
  118. diff_tv.tv_sec = b.tv_sec - a.tv_sec;
  119. if (a.tv_usec > b.tv_usec) {
  120. diff_tv.tv_usec += 1000000;
  121. diff_tv.tv_sec--;
  122. }
  123. return cast(float)diff_tv.tv_sec + (cast(float)diff_tv.tv_usec / 1000000.0);
  124. }
  125. void DateGet(out Month month, out uint day, out uint year) {
  126. timeval val;
  127. gettimeofday(&val, null);
  128. tm time_struct;
  129. gmtime_r(cast(time_t*)&val.tv_sec, &time_struct);
  130. month = cast(Month)time_struct.tm_mon;
  131. day = time_struct.tm_mday;
  132. year = time_struct.tm_year + 1900;
  133. }