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.

214 lines
5.5 KiB

  1. /* itime.c -- functions to handle time in our program
  2. *
  3. * Copyright 1990 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 2 $
  7. * $Date: 11/07/00 12:25p $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. #include <time.h>
  12. #include <memory.h>
  13. #include <tdll\stdtyp.h>
  14. #include <tdll\assert.h>
  15. #include "itime.h"
  16. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  17. * *
  18. * R E A D M E *
  19. * *
  20. * Everybody keeps changing the time standard to whatever they feel might be *
  21. * a little bit better for them. So far I have found 3 different standards *
  22. * in Microsoft functions. This does not even count the fact that HyperP *
  23. * uses its own format for time. *
  24. * *
  25. * Henceforth, all time values that are passed around in the program will be *
  26. * based on the old UCT format of the number of seconds since Jan 1, 1970. *
  27. * *
  28. * Please use an unsigned long for these values. *
  29. * *
  30. *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  31. unsigned long itimeGetBasetime(void);
  32. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  33. * FUNCTION:
  34. * itimeSetFileTime
  35. *
  36. * DESCRIPTION:
  37. * This function is called by the transfer routines to set the date/time of a
  38. * file.
  39. *
  40. * PARAMETERS:
  41. * pszName -- pointer to a file name
  42. * ulTime -- our internal standard time format
  43. *
  44. * RETURNS:
  45. * Nothing.
  46. *
  47. */
  48. void itimeSetFileTime(LPCTSTR pszName, unsigned long ulTime)
  49. {
  50. time_t base_time;
  51. struct tm *pstT;
  52. WORD wDOSDate;
  53. WORD wDOSTime;
  54. HANDLE hFile;
  55. FILETIME stFileTime;
  56. /* Yes, we need to open the file */
  57. hFile = CreateFile(pszName,
  58. GENERIC_READ,
  59. FILE_SHARE_READ,
  60. 0,
  61. OPEN_EXISTING,
  62. 0,
  63. 0);
  64. if (hFile == INVALID_HANDLE_VALUE)
  65. return; /* No such file */
  66. base_time = itimeGetBasetime();
  67. if ((long)base_time == (-1))
  68. goto SFTexit;
  69. base_time += ulTime; /* Convert to 1990 base */
  70. pstT = localtime(&base_time);
  71. assert(pstT);
  72. /* For some reason, this sometimes returns a NULL */
  73. if (pstT)
  74. {
  75. /* Build the "DOS" formats */
  76. wDOSDate = ((pstT->tm_year - 80) << 9) |
  77. (pstT->tm_mon << 5) |
  78. pstT->tm_mday;
  79. DbgOutStr("Date %d %d %d 0x%x\r\n",
  80. pstT->tm_year, pstT->tm_mon, pstT->tm_mday, wDOSDate, 0);
  81. wDOSTime = ((pstT->tm_hour - 1) << 11) |
  82. (pstT->tm_min << 5) |
  83. (pstT->tm_sec / 2);
  84. DbgOutStr("Time %d %d %d 0x%x\r\n",
  85. pstT->tm_hour, pstT->tm_min, pstT->tm_sec, wDOSTime, 0);
  86. /* Convert to CHICAGO format */
  87. /* TODO: as of 14-Mar-94, this doesn't work. Check later */
  88. if (!DosDateTimeToFileTime(wDOSDate, wDOSTime, &stFileTime))
  89. goto SFTexit;
  90. /* Set the time */
  91. SetFileTime(hFile, &stFileTime, &stFileTime, &stFileTime);
  92. }
  93. SFTexit:
  94. /* Close the handle */
  95. CloseHandle(hFile);
  96. }
  97. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  98. * FUNCTION:
  99. * itimeGetFileTime
  100. *
  101. * DESCRIPTION:
  102. * This function is called by the transfer routines to get the date/time of a
  103. * file.
  104. *
  105. * PARAMETERS:
  106. * pszName -- pointer to a file name
  107. *
  108. * RETURNS:
  109. * The file date/time in our internal standard time format.
  110. *
  111. */
  112. unsigned long itimeGetFileTime(LPCTSTR pszName)
  113. {
  114. unsigned long ulTime = 0;
  115. struct tm stT;
  116. WORD wDOSDate;
  117. WORD wDOSTime;
  118. HANDLE hFile = INVALID_HANDLE_VALUE;
  119. FILETIME stFileTime;
  120. /* Yes, we need to open the file */
  121. hFile = CreateFile(pszName,
  122. GENERIC_READ,
  123. FILE_SHARE_READ,
  124. 0,
  125. OPEN_EXISTING,
  126. 0,
  127. 0);
  128. if (hFile == INVALID_HANDLE_VALUE)
  129. goto GFTexit;
  130. if (!GetFileTime(hFile, NULL, NULL, &stFileTime))
  131. goto GFTexit;
  132. if (!FileTimeToDosDateTime(&stFileTime, &wDOSDate, &wDOSTime))
  133. goto GFTexit;
  134. memset(&stT, 0, sizeof(struct tm));
  135. stT.tm_mday = (wDOSDate & 0x1F);
  136. stT.tm_mon = ((wDOSDate >> 5) & 0xF);
  137. stT.tm_year = ((wDOSDate >> 9) & 0x7F);
  138. stT.tm_sec = (wDOSTime & 0x1F) * 2;
  139. stT.tm_min = ((wDOSTime >> 5) & 0x3F);
  140. stT.tm_hour = ((wDOSTime >> 11) & 0x1F);
  141. stT.tm_year += 80;
  142. ulTime = (unsigned long) mktime(&stT);
  143. if ((long)ulTime == (-1))
  144. ulTime = 0;
  145. else
  146. ulTime -= itimeGetBasetime();
  147. GFTexit:
  148. if (hFile != INVALID_HANDLE_VALUE)
  149. CloseHandle(hFile);
  150. return ulTime;
  151. }
  152. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  153. * FUNCTION:
  154. *
  155. * DESCRIPTION:
  156. * This function converts the "new" internal MICROSOFT time format (based at
  157. * 1900) to the "old" format (based at 1970).
  158. *
  159. * PARAMETERS:
  160. *
  161. * RETURNS:
  162. *
  163. */
  164. unsigned long itimeGetBasetime()
  165. {
  166. unsigned long ulBaseTime = 0;
  167. struct tm stT;
  168. memset(&stT, 0, sizeof(struct tm));
  169. /* Get our base time */
  170. stT.tm_mday = 1; /* Jan 1, 1970 */
  171. stT.tm_mon = 1;
  172. stT.tm_year = 70;
  173. ulBaseTime = (unsigned long) mktime(&stT);
  174. return ulBaseTime;
  175. }
  176. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  177. * FUNCTION:
  178. *
  179. * DESCRIPTION:
  180. *
  181. * PARAMETERS:
  182. *
  183. * RETURNS:
  184. *
  185. */