本代码来自ReactOS-0.4.0\lib\rtl\time.c中的函数RtlTimeToTimeFields(IN PLARGE_INTEGER Time, OUT PTIME_FIELDS TimeFields);
源代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #define TICKSPERMIN 600000000 #define TICKSPERSEC 10000000 #define TICKSPERMSEC 10000 #define SECSPERDAY 86400 #define SECSPERHOUR 3600 #define SECSPERMIN 60 #define MINSPERHOUR 60 #define HOURSPERDAY 24 #define EPOCHWEEKDAY 1 #define DAYSPERWEEK 7 #define EPOCHYEAR 1601 #define DAYSPERNORMALYEAR 365 #define DAYSPERLEAPYEAR 366 #define MONSPERYEAR 12 static const unsigned int YearLengths[2] = { DAYSPERNORMALYEAR, DAYSPERLEAPYEAR }; static const UCHAR MonthLengths[2][MONSPERYEAR] = { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; static __inline int IsLeapYear( int Year) { return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0; } static int DaysSinceEpoch( int Year) { int Days; Year--; /* Don't include a leap day from the current year */ Days = Year * DAYSPERNORMALYEAR + Year / 4 - Year / 100 + Year / 400; Days -= (EPOCHYEAR - 1) * DAYSPERNORMALYEAR + (EPOCHYEAR - 1) / 4 - (EPOCHYEAR - 1) / 100 + (EPOCHYEAR - 1) / 400; return Days; } VOID RtlTimeToTimeFields(IN PLARGE_INTEGER Time, OUT LPSYSTEMTIME TimeFields) { const UCHAR *Months; ULONG SecondsInDay, CurYear; ULONG LeapYear, CurMonth; ULONG Days; ULONGLONG IntTime = Time->QuadPart; /* Extract millisecond from time and convert time into seconds */ TimeFields->wMilliseconds = ( WORD ) ((IntTime % TICKSPERSEC) / TICKSPERMSEC); IntTime = IntTime / TICKSPERSEC; /* Split the time into days and seconds within the day */ Days = ( ULONG )(IntTime / SECSPERDAY); SecondsInDay = IntTime % SECSPERDAY; /* compute time of day */ TimeFields->wHour = ( WORD ) (SecondsInDay / SECSPERHOUR); SecondsInDay = SecondsInDay % SECSPERHOUR; TimeFields->wMinute = ( WORD ) (SecondsInDay / SECSPERMIN); TimeFields->wSecond = ( WORD ) (SecondsInDay % SECSPERMIN); /* compute day of week */ TimeFields->wDayOfWeek = ( WORD ) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK); /* compute year */ CurYear = EPOCHYEAR; CurYear += Days / DAYSPERLEAPYEAR; Days -= DaysSinceEpoch(CurYear); while (1) { LeapYear = IsLeapYear(CurYear); if (Days < YearLengths[LeapYear]) { break ; } CurYear++; Days = Days - YearLengths[LeapYear]; } TimeFields->wYear = ( WORD ) CurYear; /* Compute month of year */ LeapYear = IsLeapYear(CurYear); Months = MonthLengths[LeapYear]; for (CurMonth = 0; Days >= Months[CurMonth]; CurMonth++) Days = Days - Months[CurMonth]; TimeFields->wMonth = ( WORD ) (CurMonth + 1); TimeFields->wDay = ( WORD ) (Days + 1); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步