Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
9.5 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // tim.c - time functions
  24. ////
  25. #include "winlocal.h"
  26. #include <time.h>
  27. #include "tim.h"
  28. #include "str.h"
  29. ////
  30. // private definitions
  31. ////
  32. // Time_t is stored as (hour * HOURFACTOR + minute * MINUTEFACTOR +
  33. // second * SECONDFACTOR + millesecond)
  34. // i.e. 23:59:59.999 is stored as 235959999
  35. //
  36. #define HOURFACTOR 10000000L
  37. #define MINUTEFACTOR 100000L
  38. #define SECONDFACTOR 1000L
  39. // helper functions
  40. //
  41. ////
  42. // public functions
  43. ////
  44. // Time - return time value representing hour, minute, second, and millesecond
  45. // <h> (i) hour
  46. // <m> (i) minute
  47. // <s> (i) second
  48. // <ms> (i) millesecond
  49. // return time value (0 if error)
  50. //
  51. Time_t DLLEXPORT WINAPI Time(Hour_t h, Minute_t m, Second_t s, Millesecond_t ms)
  52. {
  53. Time_t time;
  54. time = h * HOURFACTOR + m * MINUTEFACTOR + s * SECONDFACTOR + ms;
  55. if (!TimeIsValid(time))
  56. return (Time_t) 0;
  57. return time;
  58. }
  59. // TimeNow - return time value representing current hour, minute, and second, and millesecond
  60. // return time value (0 if error)
  61. //
  62. Time_t DLLEXPORT WINAPI TimeNow(void)
  63. {
  64. static time_t timeCurr;
  65. static struct tm *tmCurr;
  66. timeCurr = time(NULL);
  67. tmCurr = localtime(&timeCurr);
  68. return Time((Hour_t) tmCurr->tm_hour, (Minute_t) tmCurr->tm_min,
  69. (Second_t) tmCurr->tm_sec, (Millesecond_t) 0);
  70. }
  71. // TimeValue - return time value representing given time string
  72. // <lpszTime> (i) time string to convert
  73. // "23:59:59.999"
  74. // etc.
  75. // return time value (0 if error)
  76. //
  77. Time_t DLLEXPORT WINAPI TimeValue(LPCTSTR lpszTime)
  78. {
  79. Hour_t h = 0;
  80. Minute_t m = 0;
  81. Second_t s = 0;
  82. Millesecond_t ms = 0;
  83. LPTSTR lpszDelimiters = TEXT(" \t/-.,;:");
  84. TCHAR szTimeTmp[32];
  85. LPTSTR lpszToken1;
  86. LPTSTR lpszToken2;
  87. LPTSTR lpszToken3;
  88. LPTSTR lpszToken4;
  89. *szTimeTmp = '\0';
  90. if (lpszTime != NULL)
  91. StrNCpy(szTimeTmp, lpszTime, SIZEOFARRAY(szTimeTmp));
  92. lpszToken1 = StrTok(szTimeTmp, lpszDelimiters);
  93. lpszToken2 = StrTok(NULL, lpszDelimiters);
  94. lpszToken3 = StrTok(NULL, lpszDelimiters);
  95. lpszToken4 = StrTok(NULL, lpszDelimiters);
  96. h = (lpszToken1 == NULL ? 0 : StrAtoI(lpszToken1));
  97. m = (lpszToken2 == NULL ? 0 : StrAtoI(lpszToken2));
  98. s = (lpszToken3 == NULL ? 0 : StrAtoI(lpszToken3));
  99. ms = (lpszToken4 == NULL ? 0 : StrAtoI(lpszToken4));
  100. return Time(h, m, s, ms);
  101. }
  102. // TimeHour - return hour of a given time (0-23)
  103. // <t> (i) time value
  104. // return hour
  105. //
  106. Hour_t DLLEXPORT WINAPI TimeHour(Time_t t)
  107. {
  108. return (Hour_t) (t / HOURFACTOR);
  109. }
  110. // TimeMinute - return minute of a given time (0-59)
  111. // <t> (i) time value
  112. // return minute
  113. //
  114. Minute_t DLLEXPORT WINAPI TimeMinute(Time_t t)
  115. {
  116. return (Minute_t) ((t % HOURFACTOR) / MINUTEFACTOR);
  117. }
  118. // TimeSecond - return second of a given time (0-59)
  119. // <t> (i) time value
  120. // return second
  121. //
  122. Second_t DLLEXPORT WINAPI TimeSecond(Time_t t)
  123. {
  124. return (Second_t) ((t % MINUTEFACTOR) / SECONDFACTOR);
  125. }
  126. // TimeMillesecond - return millesecond of a given time (0-999)
  127. // <t> (i) time value
  128. // return second
  129. //
  130. Millesecond_t DLLEXPORT WINAPI TimeMillesecond(Time_t t)
  131. {
  132. return (Millesecond_t) ((t % MINUTEFACTOR) % SECONDFACTOR);
  133. }
  134. // TimeDayMillesecond - return millesecond since the start of the day for a given time
  135. // <t> (i) time value
  136. // return millesecond
  137. //
  138. long DLLEXPORT WINAPI TimeDayMillesecond(Time_t t)
  139. {
  140. Hour_t h;
  141. Minute_t m;
  142. Second_t s;
  143. Millesecond_t ms;
  144. if (!TimeIsValid(t))
  145. return (Millesecond_t) 0;
  146. h = TimeHour(t);
  147. m = TimeMinute(t);
  148. s = TimeSecond(t);
  149. ms = TimeMillesecond(t);
  150. return (long) (h * 60L * 60L * 1000L) +
  151. (m * 60L * 1000L) + (s * 1000L) + ms;
  152. }
  153. // TimeIsValid - test <t> for validity
  154. // <t> (i) time value
  155. // return TRUE if valid
  156. //
  157. BOOL DLLEXPORT WINAPI TimeIsValid(Time_t t)
  158. {
  159. BOOL fValid = TRUE;
  160. Hour_t h = TimeHour(t);
  161. Minute_t m = TimeMinute(t);
  162. Second_t s = TimeSecond(t);
  163. Millesecond_t ms = TimeMillesecond(t);
  164. // check for invalid hour, minute, second, or millesecond
  165. //
  166. if (h < 0 || h > 23 || m < 0 || m > 59 ||
  167. s < 0 || s > 59 || ms < 0 || ms > 999)
  168. fValid = FALSE;
  169. return fValid;
  170. }
  171. // TimeIsAfternoon - return TRUE if <t> represents a time after noon
  172. // <t> (i) time value
  173. // return TRUE if leap hour
  174. //
  175. BOOL DLLEXPORT WINAPI TimeIsAfternoon(Time_t t)
  176. {
  177. return (BOOL) (TimeHour(t) >= 12);
  178. }
  179. // TimeNew - return time value which is <n> milleseconds from time <t>
  180. // <t> (i) time value
  181. // <n> (i) delta
  182. // +1 one millesecond later
  183. // -1 one millesecond earlier, etc.
  184. // return time value (0 if error)
  185. //
  186. Time_t DLLEXPORT WINAPI TimeNew(Time_t t, long n)
  187. {
  188. Hour_t h;
  189. Minute_t m;
  190. Second_t s;
  191. Millesecond_t ms;
  192. if (!TimeIsValid(t))
  193. return (Time_t) 0;
  194. h = TimeHour(t);
  195. m = TimeMinute(t);
  196. s = TimeSecond(t);
  197. ms = TimeMillesecond(t);
  198. if (n > 0)
  199. {
  200. // increment time n times
  201. //
  202. for ( ; n != 0; n--)
  203. {
  204. if (++ms == 1000)
  205. {
  206. ms = 0;
  207. if (++s == 60)
  208. {
  209. s = 0;
  210. if (++m == 60)
  211. {
  212. m = 0;
  213. if (++h == 24)
  214. h = 0;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. else
  221. {
  222. // decrement time n times
  223. //
  224. for ( ; n != 0; n++)
  225. {
  226. if (--ms < 0)
  227. {
  228. ms = 999;
  229. if (--s < 0)
  230. {
  231. s = 59;
  232. if (--m < 0)
  233. {
  234. m = 59;
  235. if (--h < 0)
  236. h = 23;
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return Time(h, m, s, ms);
  243. }
  244. // TimeCmp - return number of milleseconds between t1 and t2 (t1 minus t2)
  245. // <t1> (i) time value
  246. // <t2> (i) time value
  247. // return milleseconds between times
  248. //
  249. long DLLEXPORT WINAPI TimeCmp(Time_t t1, Time_t t2)
  250. {
  251. return (long) (TimeDayMillesecond(t1) - TimeDayMillesecond(t2));
  252. }
  253. // TimeStartSecond - return time representing start the second relative to time <t>
  254. // <t> (i) time value
  255. // return time value (0 if error)
  256. //
  257. Time_t DLLEXPORT WINAPI TimeStartSecond(Time_t t)
  258. {
  259. return Time(TimeHour(t), TimeMinute(t), TimeSecond(t), 0);
  260. }
  261. // TimeEndSecond - return time representing end of the second relative to time <t>
  262. // <t> (i) time value
  263. // return time value (0 if error)
  264. //
  265. Time_t DLLEXPORT WINAPI TimeEndSecond(Time_t t)
  266. {
  267. return Time(TimeHour(t), TimeMinute(t), TimeSecond(t), 999);
  268. }
  269. // TimeStartMinute - return time representing start the minute relative to time <t>
  270. // <t> (i) time value
  271. // return time value (0 if error)
  272. //
  273. Time_t DLLEXPORT WINAPI TimeStartMinute(Time_t t)
  274. {
  275. return Time(TimeHour(t), TimeMinute(t), 0, 0);
  276. }
  277. // TimeEndMinute - return time representing end of the minute relative to time <t>
  278. // <t> (i) time value
  279. // return time value (0 if error)
  280. //
  281. Time_t DLLEXPORT WINAPI TimeEndMinute(Time_t t)
  282. {
  283. return Time(TimeHour(t), TimeMinute(t), 59, 999);
  284. }
  285. // TimeStartHour - return time representing start of the hour relative to time <t>
  286. // <t> (i) time value
  287. // return time value (0 if error)
  288. //
  289. Time_t DLLEXPORT WINAPI TimeStartHour(Time_t t)
  290. {
  291. return Time(TimeHour(t), 0, 0, 0);
  292. }
  293. // TimeEndHour - return time representing end of the hour relative to time <t>
  294. // <t> (i) time value
  295. // return time value (0 if error)
  296. //
  297. Time_t DLLEXPORT WINAPI TimeEndHour(Time_t t)
  298. {
  299. return Time(TimeHour(t), 59, 59, 999);
  300. }
  301. // TimeStartLastSecond - return time representing start of previous second
  302. // return time value (0 if error)
  303. //
  304. Time_t DLLEXPORT WINAPI TimeStartLastSecond(void)
  305. {
  306. return TimeStartSecond(TimeEndLastSecond());
  307. }
  308. // TimeEndLastSecond - return time representing end of previous second
  309. // return time value (0 if error)
  310. //
  311. Time_t DLLEXPORT WINAPI TimeEndLastSecond(void)
  312. {
  313. return TimeNew(TimeStartSecond(TimeNow()), -1);
  314. }
  315. // TimeStartLastMinute - return time representing start of previous minute
  316. // return time value (0 if error)
  317. //
  318. Time_t DLLEXPORT WINAPI TimeStartLastMinute(void)
  319. {
  320. return TimeStartMinute(TimeEndLastMinute());
  321. }
  322. // TimeEndLastMinute - return time representing end of previous minute
  323. // return time value (0 if error)
  324. //
  325. Time_t DLLEXPORT WINAPI TimeEndLastMinute(void)
  326. {
  327. return TimeNew(TimeStartMinute(TimeNow()), -1);
  328. }
  329. // TimeStartLastHour - return time representing start of previous hour
  330. // return time value (0 if error)
  331. //
  332. Time_t DLLEXPORT WINAPI TimeStartLastHour(void)
  333. {
  334. return TimeStartHour(TimeEndLastHour());
  335. }
  336. // TimeEndLastHour - return time representing end of previous hour
  337. // return time value (0 if error)
  338. //
  339. Time_t DLLEXPORT WINAPI TimeEndLastHour(void)
  340. {
  341. return TimeNew(TimeStartHour(TimeNow()), -1);
  342. }