ReactOS 0.4.15-dev-7931-gfd331f1
time.c File Reference
#include <rtl.h>
#include <debug.h>
Include dependency graph for time.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#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
 
#define TICKSTO1970   0x019db1ded53e8000i64
 
#define TICKSTO1980   0x01a8e79fe1d58000i64
 

Functions

static __inline int IsLeapYear (int Year)
 
static int DaysSinceEpoch (int Year)
 
BOOLEAN NTAPI RtlCutoverTimeToSystemTime (IN PTIME_FIELDS CutoverTimeFields, OUT PLARGE_INTEGER SystemTime, IN PLARGE_INTEGER CurrentTime, IN BOOLEAN ThisYearsCutoverOnly)
 
BOOLEAN NTAPI RtlTimeFieldsToTime (IN PTIME_FIELDS TimeFields, OUT PLARGE_INTEGER Time)
 
VOID NTAPI RtlTimeToElapsedTimeFields (IN PLARGE_INTEGER Time, OUT PTIME_FIELDS TimeFields)
 
VOID NTAPI RtlTimeToTimeFields (IN PLARGE_INTEGER Time, OUT PTIME_FIELDS TimeFields)
 
BOOLEAN NTAPI RtlTimeToSecondsSince1970 (IN PLARGE_INTEGER Time, OUT PULONG SecondsSince1970)
 
BOOLEAN NTAPI RtlTimeToSecondsSince1980 (IN PLARGE_INTEGER Time, OUT PULONG SecondsSince1980)
 
NTSTATUS NTAPI RtlLocalTimeToSystemTime (IN PLARGE_INTEGER LocalTime, OUT PLARGE_INTEGER SystemTime)
 
NTSTATUS NTAPI RtlSystemTimeToLocalTime (IN PLARGE_INTEGER SystemTime, OUT PLARGE_INTEGER LocalTime)
 
VOID NTAPI RtlSecondsSince1970ToTime (IN ULONG SecondsSince1970, OUT PLARGE_INTEGER Time)
 
VOID NTAPI RtlSecondsSince1980ToTime (IN ULONG SecondsSince1980, OUT PLARGE_INTEGER Time)
 

Variables

static const unsigned int YearLengths [2]
 
static const UCHAR MonthLengths [2][MONSPERYEAR]
 

Macro Definition Documentation

◆ DAYSPERLEAPYEAR

#define DAYSPERLEAPYEAR   366

Definition at line 28 of file time.c.

◆ DAYSPERNORMALYEAR

#define DAYSPERNORMALYEAR   365

Definition at line 27 of file time.c.

◆ DAYSPERWEEK

#define DAYSPERWEEK   7

Definition at line 25 of file time.c.

◆ EPOCHWEEKDAY

#define EPOCHWEEKDAY   1

Definition at line 24 of file time.c.

◆ EPOCHYEAR

#define EPOCHYEAR   1601

Definition at line 26 of file time.c.

◆ HOURSPERDAY

#define HOURSPERDAY   24

Definition at line 23 of file time.c.

◆ MINSPERHOUR

#define MINSPERHOUR   60

Definition at line 22 of file time.c.

◆ MONSPERYEAR

#define MONSPERYEAR   12

Definition at line 29 of file time.c.

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file time.c.

◆ SECSPERDAY

#define SECSPERDAY   86400

Definition at line 19 of file time.c.

◆ SECSPERHOUR

#define SECSPERHOUR   3600

Definition at line 20 of file time.c.

◆ SECSPERMIN

#define SECSPERMIN   60

Definition at line 21 of file time.c.

◆ TICKSPERMIN

#define TICKSPERMIN   600000000

Definition at line 16 of file time.c.

◆ TICKSPERMSEC

#define TICKSPERMSEC   10000

Definition at line 18 of file time.c.

◆ TICKSPERSEC

#define TICKSPERSEC   10000000

Definition at line 17 of file time.c.

◆ TICKSTO1970

#define TICKSTO1970   0x019db1ded53e8000i64

Definition at line 35 of file time.c.

◆ TICKSTO1980

#define TICKSTO1980   0x01a8e79fe1d58000i64

Definition at line 36 of file time.c.

Function Documentation

◆ DaysSinceEpoch()

static int DaysSinceEpoch ( int  Year)
static

Definition at line 55 of file time.c.

56{
57 int Days;
58 Year--; /* Don't include a leap day from the current year */
59 Days = Year * DAYSPERNORMALYEAR + Year / 4 - Year / 100 + Year / 400;
60 Days -= (EPOCHYEAR - 1) * DAYSPERNORMALYEAR + (EPOCHYEAR - 1) / 4 - (EPOCHYEAR - 1) / 100 + (EPOCHYEAR - 1) / 400;
61 return Days;
62}
#define EPOCHYEAR
Definition: time.c:26
#define DAYSPERNORMALYEAR
Definition: time.c:27

Referenced by RtlTimeFieldsToTime(), and RtlTimeToTimeFields().

◆ IsLeapYear()

static __inline int IsLeapYear ( int  Year)
static

Definition at line 50 of file time.c.

51{
52 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
53}

◆ RtlCutoverTimeToSystemTime()

BOOLEAN NTAPI RtlCutoverTimeToSystemTime ( IN PTIME_FIELDS  CutoverTimeFields,
OUT PLARGE_INTEGER  SystemTime,
IN PLARGE_INTEGER  CurrentTime,
IN BOOLEAN  ThisYearsCutoverOnly 
)

Definition at line 70 of file time.c.

74{
75 TIME_FIELDS AdjustedTimeFields;
76 TIME_FIELDS CurrentTimeFields;
77 TIME_FIELDS CutoverSystemTimeFields;
78 LARGE_INTEGER CutoverSystemTime;
79 UCHAR MonthLength;
80 CSHORT Days;
81 BOOLEAN NextYearsCutover = FALSE;
82
83 /* Check fixed cutover time */
84 if (CutoverTimeFields->Year != 0)
85 {
86 if (!RtlTimeFieldsToTime(CutoverTimeFields, SystemTime))
87 return FALSE;
88
89 if (SystemTime->QuadPart < CurrentTime->QuadPart)
90 return FALSE;
91
92 return TRUE;
93 }
94
95 /*
96 * Compute recurring cutover time
97 */
98
99 /* Day must be between 1(first) and 5(last) */
100 if (CutoverTimeFields->Day == 0 || CutoverTimeFields->Day > 5)
101 return FALSE;
102
103 RtlTimeToTimeFields(CurrentTime, &CurrentTimeFields);
104
105 while (TRUE)
106 {
107 /* Compute the cutover time of the first day of the current month */
108 AdjustedTimeFields.Year = CurrentTimeFields.Year;
109 if (NextYearsCutover)
110 AdjustedTimeFields.Year++;
111
112 AdjustedTimeFields.Month = CutoverTimeFields->Month;
113 AdjustedTimeFields.Day = 1;
114 AdjustedTimeFields.Hour = CutoverTimeFields->Hour;
115 AdjustedTimeFields.Minute = CutoverTimeFields->Minute;
116 AdjustedTimeFields.Second = CutoverTimeFields->Second;
117 AdjustedTimeFields.Milliseconds = CutoverTimeFields->Milliseconds;
118
119 if (!RtlTimeFieldsToTime(&AdjustedTimeFields, &CutoverSystemTime))
120 return FALSE;
121
122 RtlTimeToTimeFields(&CutoverSystemTime, &CutoverSystemTimeFields);
123
124 /* Adjust day to first matching weekday */
125 if (CutoverSystemTimeFields.Weekday != CutoverTimeFields->Weekday)
126 {
127 if (CutoverSystemTimeFields.Weekday < CutoverTimeFields->Weekday)
128 Days = CutoverTimeFields->Weekday - CutoverSystemTimeFields.Weekday;
129 else
130 Days = DAYSPERWEEK - (CutoverSystemTimeFields.Weekday - CutoverTimeFields->Weekday);
131
132 AdjustedTimeFields.Day += Days;
133 }
134
135 /* Adjust the number of weeks */
136 if (CutoverTimeFields->Day > 1)
137 {
138 Days = DAYSPERWEEK * (CutoverTimeFields->Day - 1);
139 MonthLength = MonthLengths[IsLeapYear(AdjustedTimeFields.Year)][AdjustedTimeFields.Month - 1];
140 if ((AdjustedTimeFields.Day + Days) > MonthLength)
141 Days -= DAYSPERWEEK;
142
143 AdjustedTimeFields.Day += Days;
144 }
145
146 if (!RtlTimeFieldsToTime(&AdjustedTimeFields, &CutoverSystemTime))
147 return FALSE;
148
149 if (ThisYearsCutoverOnly ||
150 NextYearsCutover ||
151 CutoverSystemTime.QuadPart >= CurrentTime->QuadPart)
152 {
153 break;
154 }
155
156 NextYearsCutover = TRUE;
157 }
158
159 SystemTime->QuadPart = CutoverSystemTime.QuadPart;
160
161 return TRUE;
162}
unsigned char BOOLEAN
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOLEAN RtlTimeToTimeFields(IN PLARGE_INTEGER Time, IN PTIME_FIELDS TimeFields)
BOOLEAN RtlTimeFieldsToTime(IN PTIME_FIELDS TimeFields, IN PLARGE_INTEGER Time)
static const int MonthLengths[2][12]
Definition: time.c:111
#define DAYSPERWEEK
Definition: time.c:25
USHORT Milliseconds
Definition: env_spec_w32.h:717
USHORT Weekday
Definition: env_spec_w32.h:718
short CSHORT
Definition: umtypes.h:127
LONGLONG QuadPart
Definition: typedefs.h:114
#define IsLeapYear(y)
Definition: variant.c:1048
unsigned char UCHAR
Definition: xmlstorage.h:181

◆ RtlLocalTimeToSystemTime()

NTSTATUS NTAPI RtlLocalTimeToSystemTime ( IN PLARGE_INTEGER  LocalTime,
OUT PLARGE_INTEGER  SystemTime 
)

Definition at line 356 of file time.c.

358{
359 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation;
361
363 &TimeInformation,
364 sizeof(TimeInformation),
365 NULL);
366 if (!NT_SUCCESS(Status))
367 return Status;
368
369 SystemTime->QuadPart = LocalTime->QuadPart +
370 TimeInformation.TimeZoneBias.QuadPart;
371
372 return STATUS_SUCCESS;
373}
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSYSAPI NTSTATUS NTAPI ZwQuerySystemInformation(IN SYSTEM_INFORMATION_CLASS SystemInfoClass, OUT PVOID SystemInfoBuffer, IN ULONG SystemInfoBufferSize, OUT PULONG BytesReturned OPTIONAL)
@ SystemTimeOfDayInformation
Definition: ntddk_ex.h:14
Status
Definition: gdiplustypes.h:25
#define STATUS_SUCCESS
Definition: shellext.h:65
LARGE_INTEGER TimeZoneBias
Definition: extypes.h:859

◆ RtlSecondsSince1970ToTime()

VOID NTAPI RtlSecondsSince1970ToTime ( IN ULONG  SecondsSince1970,
OUT PLARGE_INTEGER  Time 
)

Definition at line 406 of file time.c.

408{
409 Time->QuadPart = ((LONGLONG)SecondsSince1970 * TICKSPERSEC) + TICKSTO1970;
410}
#define TICKSPERSEC
Definition: time.c:40
static PLARGE_INTEGER Time
Definition: time.c:105
#define TICKSTO1970
Definition: time.c:35
int64_t LONGLONG
Definition: typedefs.h:68

◆ RtlSecondsSince1980ToTime()

VOID NTAPI RtlSecondsSince1980ToTime ( IN ULONG  SecondsSince1980,
OUT PLARGE_INTEGER  Time 
)

Definition at line 417 of file time.c.

419{
420 Time->QuadPart = ((LONGLONG)SecondsSince1980 * TICKSPERSEC) + TICKSTO1980;
421}
#define TICKSTO1980
Definition: time.c:36

◆ RtlSystemTimeToLocalTime()

NTSTATUS NTAPI RtlSystemTimeToLocalTime ( IN PLARGE_INTEGER  SystemTime,
OUT PLARGE_INTEGER  LocalTime 
)

Definition at line 381 of file time.c.

383{
384 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation;
386
388 &TimeInformation,
389 sizeof(TimeInformation),
390 NULL);
391 if (!NT_SUCCESS(Status))
392 return Status;
393
394 LocalTime->QuadPart = SystemTime->QuadPart -
395 TimeInformation.TimeZoneBias.QuadPart;
396
397 return STATUS_SUCCESS;
398}

◆ RtlTimeFieldsToTime()

BOOLEAN NTAPI RtlTimeFieldsToTime ( IN PTIME_FIELDS  TimeFields,
OUT PLARGE_INTEGER  Time 
)

Definition at line 170 of file time.c.

172{
173 ULONG CurMonth;
174 TIME_FIELDS IntTimeFields;
175
176 RtlCopyMemory(&IntTimeFields,
178 sizeof(TIME_FIELDS));
179
180 if (TimeFields->Milliseconds < 0 || TimeFields->Milliseconds > 999 ||
181 TimeFields->Second < 0 || TimeFields->Second > 59 ||
182 TimeFields->Minute < 0 || TimeFields->Minute > 59 ||
183 TimeFields->Hour < 0 || TimeFields->Hour > 23 ||
184 TimeFields->Month < 1 || TimeFields->Month > 12 ||
185 TimeFields->Day < 1 ||
186 TimeFields->Day >
188 TimeFields->Year < 1601)
189 {
190 return FALSE;
191 }
192
193 /* Compute the time */
194 Time->QuadPart = DaysSinceEpoch(IntTimeFields.Year);
195 for (CurMonth = 1; CurMonth < IntTimeFields.Month; CurMonth++)
196 {
197 Time->QuadPart += MonthLengths[IsLeapYear(IntTimeFields.Year)][CurMonth - 1];
198 }
199 Time->QuadPart += IntTimeFields.Day - 1;
201 Time->QuadPart += IntTimeFields.Hour * SECSPERHOUR + IntTimeFields.Minute * SECSPERMIN +
202 IntTimeFields.Second;
204 Time->QuadPart += IntTimeFields.Milliseconds * TICKSPERMSEC;
205
206 return TRUE;
207}
#define SECSPERMIN
Definition: time.c:36
#define SECSPERDAY
Definition: time.c:33
#define SECSPERHOUR
Definition: time.c:34
static PTIME_FIELDS TimeFields
Definition: time.c:104
static int DaysSinceEpoch(int Year)
Definition: time.c:55
#define TICKSPERMSEC
Definition: time.c:18
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
uint32_t ULONG
Definition: typedefs.h:59

◆ RtlTimeToElapsedTimeFields()

VOID NTAPI RtlTimeToElapsedTimeFields ( IN PLARGE_INTEGER  Time,
OUT PTIME_FIELDS  TimeFields 
)

Definition at line 215 of file time.c.

217{
219 ULONG SecondsInDay;
220 ULONG SecondsInMinute;
221
222 /* Extract millisecond from time */
224
225 /* Compute elapsed seconds */
227
228 /* Compute seconds within the day */
229 SecondsInDay = ElapsedSeconds % SECSPERDAY;
230
231 /* Compute elapsed minutes within the day */
232 SecondsInMinute = SecondsInDay % SECSPERHOUR;
233
234 /* Compute elapsed time of day */
235 TimeFields->Hour = (CSHORT)(SecondsInDay / SECSPERHOUR);
236 TimeFields->Minute = (CSHORT)(SecondsInMinute / SECSPERMIN);
237 TimeFields->Second = (CSHORT)(SecondsInMinute % SECSPERMIN);
238
239 /* Compute elapsed days */
241
242 /* The elapsed number of months and days cannot be calculated */
243 TimeFields->Month = 0;
244 TimeFields->Year = 0;
245}
_Out_ PULONG ElapsedSeconds
Definition: rtlfuncs.h:4646
uint64_t ULONGLONG
Definition: typedefs.h:67

◆ RtlTimeToSecondsSince1970()

BOOLEAN NTAPI RtlTimeToSecondsSince1970 ( IN PLARGE_INTEGER  Time,
OUT PULONG  SecondsSince1970 
)

Definition at line 312 of file time.c.

314{
315 LARGE_INTEGER IntTime;
316
317 IntTime.QuadPart = Time->QuadPart - TICKSTO1970;
318 IntTime.QuadPart = IntTime.QuadPart / TICKSPERSEC;
319
320 if (IntTime.u.HighPart != 0)
321 return FALSE;
322
323 *SecondsSince1970 = IntTime.u.LowPart;
324
325 return TRUE;
326}
struct _LARGE_INTEGER::@2295 u

◆ RtlTimeToSecondsSince1980()

BOOLEAN NTAPI RtlTimeToSecondsSince1980 ( IN PLARGE_INTEGER  Time,
OUT PULONG  SecondsSince1980 
)

Definition at line 334 of file time.c.

336{
337 LARGE_INTEGER IntTime;
338
339 IntTime.QuadPart = Time->QuadPart - TICKSTO1980;
340 IntTime.QuadPart = IntTime.QuadPart / TICKSPERSEC;
341
342 if (IntTime.u.HighPart != 0)
343 return FALSE;
344
345 *SecondsSince1980 = IntTime.u.LowPart;
346
347 return TRUE;
348}

◆ RtlTimeToTimeFields()

VOID NTAPI RtlTimeToTimeFields ( IN PLARGE_INTEGER  Time,
OUT PTIME_FIELDS  TimeFields 
)

Definition at line 253 of file time.c.

255{
256 const UCHAR *Months;
257 ULONG SecondsInDay, CurYear;
258 ULONG LeapYear, CurMonth;
259 ULONG Days;
260 ULONGLONG IntTime = Time->QuadPart;
261
262 /* Extract millisecond from time and convert time into seconds */
264 IntTime = IntTime / TICKSPERSEC;
265
266 /* Split the time into days and seconds within the day */
267 Days = (ULONG)(IntTime / SECSPERDAY);
268 SecondsInDay = IntTime % SECSPERDAY;
269
270 /* Compute time of day */
271 TimeFields->Hour = (CSHORT)(SecondsInDay / SECSPERHOUR);
272 SecondsInDay = SecondsInDay % SECSPERHOUR;
273 TimeFields->Minute = (CSHORT)(SecondsInDay / SECSPERMIN);
274 TimeFields->Second = (CSHORT)(SecondsInDay % SECSPERMIN);
275
276 /* Compute day of week */
278
279 /* Compute year */
280 CurYear = EPOCHYEAR;
281 CurYear += Days / DAYSPERLEAPYEAR;
282 Days -= DaysSinceEpoch(CurYear);
283 while (TRUE)
284 {
285 LeapYear = IsLeapYear(CurYear);
286 if (Days < YearLengths[LeapYear])
287 {
288 break;
289 }
290 CurYear++;
291 Days = Days - YearLengths[LeapYear];
292 }
293 TimeFields->Year = (CSHORT)CurYear;
294
295 /* Compute month of year */
296 LeapYear = IsLeapYear(CurYear);
297 Months = MonthLengths[LeapYear];
298 for (CurMonth = 0; Days >= Months[CurMonth]; CurMonth++)
299 {
300 Days = Days - Months[CurMonth];
301 }
302 TimeFields->Month = (CSHORT)(CurMonth + 1);
303 TimeFields->Day = (CSHORT)(Days + 1);
304}
static WORD LeapYear(IN WORD Year)
Definition: monthcal.c:84
#define EPOCHWEEKDAY
Definition: time.c:24
static const unsigned int YearLengths[2]
Definition: time.c:40
#define DAYSPERLEAPYEAR
Definition: time.c:28

Variable Documentation

◆ MonthLengths

const UCHAR MonthLengths[2][MONSPERYEAR]
static
Initial value:
=
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
}

Definition at line 44 of file time.c.

◆ YearLengths

const unsigned int YearLengths[2]
static
Initial value:

Definition at line 40 of file time.c.

Referenced by RtlTimeToTimeFields().