/core/time.d

http://github.com/wilkie/djehuty · D · 234 lines · 142 code · 53 blank · 39 comment · 10 complexity · 59a34d809069642f3a6d761b73e32b17 MD5 · raw file

  1. module core.time;
  2. import Scaffold = scaffold.time;
  3. import core.string;
  4. import core.definitions;
  5. import core.timezone;
  6. import io.console;
  7. // Section: Types
  8. // Description: This struct stores a description of time.
  9. class Time {
  10. // Description: This will construct a Time object that will represent midnight.
  11. this() {
  12. }
  13. // Description: This will construct a Time object that will represent the
  14. // amount of time equal to the number of microseconds given by the
  15. // parameter.
  16. // microseconds: The number of microseconds to be represented.
  17. this(long microseconds) {
  18. _micros = microseconds;
  19. }
  20. // Description: This will construct a Time object that will represent the
  21. // time given by each parameter such that it will represent
  22. // hour:minute:second.microsecond
  23. // hour: The hours to be represented by this Time object.
  24. // minute: The minutes to be represented by this Time object.
  25. // second: The seconds to be represented by this Time object.
  26. // microsecond: The microseconds to be represented by this Time object.
  27. this(long hour, long minute, long second, long microsecond = 0) {
  28. _micros = hour;
  29. _micros *= 60;
  30. _micros += minute;
  31. _micros *= 60;
  32. _micros += second;
  33. _micros *= 1000000;
  34. _micros += microsecond;
  35. }
  36. // Description: This will return a Time object that represents the coordinated
  37. // universal time (UTC) assumed by the system.
  38. // Returns: A Time class that represents the system's idea of the current universal time.
  39. static Time Now() {
  40. return new Time(Scaffold.TimeGet());
  41. }
  42. // Description: This will return a Time object that represents the local time
  43. // according to the given TimeZone. If a TimeZone is not given, the local
  44. // time of the current locale is returned.
  45. // localTZ: The TimeZone to use to infer the localized time from the UTC.
  46. // Returns: A Time class that represents the local time.
  47. static Time Local(TimeZone localTZ = null) {
  48. Time ret = new Time(Scaffold.TimeGet());
  49. // Get the local timezone if one was not specified.
  50. if (localTZ is null) {
  51. localTZ = new TimeZone();
  52. }
  53. ret._micros += localTZ.utcOffset;
  54. // Make sure it is within a day
  55. // I am sure this breaks some leap second business
  56. if (ret._micros < 0) {
  57. ret._micros += (24L * 60L * 60L * 1000000L);
  58. }
  59. return ret;
  60. }
  61. // Description: This will give the floored number of hours this Time
  62. // object represents.
  63. long hours() {
  64. long h, ms, s, m;
  65. long tmp = _micros;
  66. tmp /= 1000000;
  67. tmp /= 60;
  68. tmp /= 60;
  69. return tmp;
  70. }
  71. void hours(long value) {
  72. _micros = value * 60L * 60L * 1000000L;
  73. }
  74. // Description: This will give the floored number of seconds this Time
  75. // object represents.
  76. long seconds() {
  77. long h, ms, s, m;
  78. long tmp = _micros;
  79. tmp /= 1000000;
  80. return tmp;
  81. }
  82. void seconds(long value) {
  83. _micros = value * 1000000L;
  84. }
  85. // Description: This will give the floored number of minutes this Time
  86. // object represents.
  87. long minutes() {
  88. long h, ms, s, m;
  89. long tmp = _micros;
  90. tmp /= 1000000;
  91. tmp /= 60;
  92. return tmp;
  93. }
  94. void minutes(long value) {
  95. _micros = value * 60L * 1000000L;
  96. }
  97. // Description: This will give the floored number of milliseconds this Time
  98. // object represents.
  99. long milliseconds() {
  100. long h, ms, s, m;
  101. long tmp = _micros;
  102. return tmp / 1000;
  103. }
  104. void milliseconds(long value) {
  105. _micros = value * 1000L;
  106. }
  107. // Description: This will give the floored number of microseconds this Time
  108. // object represents.
  109. long microseconds() {
  110. return _micros;
  111. }
  112. void microseconds(long value) {
  113. _micros = value;
  114. }
  115. // comparator functions
  116. int opCmp(Time o) {
  117. return cast(int)(_micros - o._micros);
  118. }
  119. int opEquals(Time o) {
  120. return cast(int)(o._micros == _micros);
  121. }
  122. // string functions
  123. string toString() {
  124. long h, ms, s, m;
  125. long tmp = _micros;
  126. string str = "";
  127. if (tmp < 0) {
  128. tmp *= -1;
  129. }
  130. ms = (tmp % 1000000) / 1000;
  131. tmp /= 1000000;
  132. s = tmp % 60;
  133. tmp /= 60;
  134. m = tmp % 60;
  135. tmp /= 60;
  136. h = tmp;
  137. if (_micros < 0) {
  138. str ~= "-";
  139. }
  140. if (h < 10) {
  141. str ~= "0";
  142. }
  143. str ~= toStr(h, ":");
  144. if (m < 10) {
  145. str ~= "0";
  146. }
  147. str ~= toStr(m, ":");
  148. if (s < 10) {
  149. str ~= "0";
  150. }
  151. str ~= toStr(s, ".");
  152. if (ms < 100) {
  153. str ~= "0";
  154. }
  155. if (ms < 10) {
  156. str ~= "0";
  157. }
  158. str ~= toStr(ms);
  159. return str;
  160. }
  161. // mathematical functions
  162. Time opAdd(Time o) {
  163. Time ret = new Time();
  164. ret._micros = _micros + o._micros;
  165. return ret;
  166. }
  167. Time opSub(Time o) {
  168. Time ret = new Time();
  169. ret._micros = _micros - o._micros;
  170. return ret;
  171. }
  172. void opAddAssign(Time o) {
  173. _micros += o._micros;
  174. }
  175. void opSubAssign(Time o) {
  176. _micros -= o._micros;
  177. }
  178. protected:
  179. // Description: The microsecond.
  180. long _micros;
  181. }