Source code of Windows XP (NT5)
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.

135 lines
3.0 KiB

  1. /****************************************************************************************************************
  2. FILENAME: GetTime.cpp
  3. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  4. */
  5. #include "stdafx.h"
  6. #include <windows.h>
  7. #include "ErrMacro.h"
  8. /****************************************************************************************************************
  9. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  10. ROUTINE DESCRIPTION:
  11. This routine gets the current system time in the FILETIME format.
  12. INPUT + OUTPUT:
  13. pFileTime - Where to write the current time.
  14. GLOBALS:
  15. None.
  16. RETURN:
  17. TRUE - Success.
  18. FALSE - Fatal error.
  19. */
  20. BOOL
  21. GetTime(
  22. FILETIME* pFileTime
  23. )
  24. {
  25. SYSTEMTIME SystemTime;
  26. GetLocalTime(&SystemTime);
  27. EF(SystemTimeToFileTime(&SystemTime, pFileTime));
  28. return TRUE;
  29. }
  30. /****************************************************************************************************************
  31. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  32. ROUTINE DESCRIPTION:
  33. This routine returns a formatted time string for output.
  34. The string is overwritten on each call.
  35. INPUT:
  36. pTime - system time struct.
  37. RETURN:
  38. Formatted time string: "mm/dd/yyyy hh:mm:ss.ms"
  39. */
  40. PTCHAR
  41. GetTmpTimeString(
  42. SYSTEMTIME & pTime
  43. )
  44. {
  45. static TCHAR cTimeStr[100];
  46. wsprintf(cTimeStr, TEXT("%02d/%02d/%04d %02d:%02d:%02d.%03d"),
  47. pTime.wMonth, pTime.wDay, pTime.wYear,
  48. pTime.wHour, pTime.wMinute, pTime.wSecond, pTime.wMilliseconds);
  49. return cTimeStr;
  50. }
  51. /****************************************************************************************************************
  52. COPYRIGHT 2001 Microsoft Corporation and Executive Software International, Inc.
  53. ROUTINE DESCRIPTION:
  54. This routine returns the number of seconds between two system times.
  55. INPUT:
  56. pStartTime - start time
  57. pEndTime - end time
  58. OUTPUT:
  59. pdwSeconds - number of elapsed seconds
  60. RETURN:
  61. TRUE - Success.
  62. FALSE - Fatal error.
  63. */
  64. BOOL
  65. GetDeltaTime(
  66. SYSTEMTIME * pStartTime,
  67. SYSTEMTIME * pEndTime,
  68. DWORD * pdwSeconds
  69. )
  70. {
  71. FILETIME ftStartTime;
  72. FILETIME ftEndTime;
  73. LARGE_INTEGER liStartTime;
  74. LARGE_INTEGER liEndTime;
  75. LONGLONG llDeltaTime;
  76. // clear returned seconds
  77. *pdwSeconds = 0;
  78. // It is not recommended that you add and subtract values from the SYSTEMTIME
  79. // structure to obtain relative times. Instead, you should
  80. // Convert the SYSTEMTIME structures to FILETIME structures.
  81. EF(SystemTimeToFileTime(pStartTime, &ftStartTime));
  82. EF(SystemTimeToFileTime(pEndTime, &ftEndTime));
  83. // Copy the resulting FILETIME structures to LARGE_INTEGER structures.
  84. liStartTime.LowPart = ftStartTime.dwLowDateTime;
  85. liStartTime.HighPart = ftStartTime.dwHighDateTime;
  86. liEndTime.LowPart = ftEndTime.dwLowDateTime;
  87. liEndTime.HighPart = ftEndTime.dwHighDateTime;
  88. // Use normal 64-bit arithmetic on the LARGE_INTEGER value.
  89. llDeltaTime = liEndTime.QuadPart - liStartTime.QuadPart;
  90. // convert to seconds
  91. *pdwSeconds = (DWORD) (llDeltaTime / 10000000);
  92. return TRUE;
  93. }