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.

90 lines
2.3 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990-1991 **/
  4. /********************************************************************/
  5. /* :ts=4 */
  6. /*** dostime.cpp - map between DOS & NET time formats
  7. */
  8. #include "npcommon.h"
  9. #include <convtime.h>
  10. #define YR_MASK 0xFE00
  11. #define LEAPYR_MASK 0x0600
  12. #define YR_BITS 7
  13. #define MON_MASK 0x01E0
  14. #define MON_BITS 4
  15. #define DAY_MASK 0x001F
  16. #define DAY_BITS 5
  17. #define HOUR_MASK 0xF800
  18. #define HOUR_BITS 5
  19. #define MIN_MASK 0x07E0
  20. #define MIN_BITS 6
  21. #define SEC2_MASK 0x001F
  22. #define SEC2_BITS 5
  23. void
  24. NetToDosDate(
  25. DWORD time,
  26. dos_time *pinfo) // ptr for return data:
  27. {
  28. UINT secs, days;
  29. WORD r;
  30. time = (time - _70_to_80_bias) / 2; // # of 2 second periods since 1980
  31. secs = time % SEC2S_IN_DAY; // 2 second period into day
  32. days = time / SEC2S_IN_DAY; // days since Jan 1 1980
  33. r = secs % 30; // # of 2 second steps
  34. secs /= 30;
  35. r |= (secs % 60) << SEC2_BITS; // # of minutes
  36. r |= (secs / 60) << (SEC2_BITS+MIN_BITS); // # of hours
  37. pinfo->dt_time = r;
  38. r = days / FOURYEARS; // (r) = four year period past 1980
  39. days %= FOURYEARS; // (days) = days into four year period
  40. r *= 4; // (r) = years since 1980 (within 3)
  41. if (days == 31+28) {
  42. //* Special case for FEB 29th
  43. r = (r<<(MON_BITS+DAY_BITS)) + (2<<DAY_BITS) + 29;
  44. } else {
  45. if (days > 31+28)
  46. --days; // compensate for leap year
  47. while (days >= 365) {
  48. ++r;
  49. days -= 365;
  50. }
  51. for (secs = 1; days >= MonTotal[secs+1] ; ++secs)
  52. ;
  53. days -= MonTotal[secs];
  54. r <<= MON_BITS;
  55. r += (unsigned short) secs;
  56. r <<= DAY_BITS;
  57. r += (unsigned short) days+1;
  58. }
  59. pinfo->dt_date = r;
  60. }
  61. DWORD
  62. DosToNetDate(dos_time dt)
  63. {
  64. UINT days, secs2;
  65. days = dt.dt_date >> (MON_BITS + DAY_BITS);
  66. days = days*365 + days/4; // # of years in days
  67. days += (dt.dt_date & DAY_MASK) + MonTotal[(dt.dt_date&MON_MASK) >> DAY_BITS];
  68. if ((dt.dt_date&LEAPYR_MASK) == 0
  69. && (dt.dt_date&MON_MASK) <= (2<<DAY_BITS))
  70. --days; // adjust days for early in leap year
  71. secs2 = ( ((dt.dt_time&HOUR_MASK) >> (MIN_BITS+SEC2_BITS)) * 60
  72. + ((dt.dt_time&MIN_MASK) >> SEC2_BITS) ) * 30
  73. + (dt.dt_time&SEC2_MASK);
  74. return (DWORD)days*SECS_IN_DAY + _70_to_80_bias + (DWORD)secs2*2;
  75. }