source: trunk/src/NTDLL/time.cpp@ 51

Last change on this file since 51 was 51, checked in by sandervl, 26 years ago

* empty log message *

File size: 7.2 KB
Line 
1/*
2 * Project Odin Software License can be found in LICENSE.TXT
3 * Win32 NT Runtime / NTDLL for OS/2
4 *
5 * Copyright 1998 original WINE Author
6 * Copyright 1998, 1999 Patrick Haller (phaller@gmx.net)
7 *
8 * Conversion between Time and TimeFields
9 *
10 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
11 * adapted to wine with special permissions of the author
12 * Rex Jolliff (rex@lvcablemodem.com)
13 *
14 *
15 */
16
17#include "ntdll.h"
18#include <string.h>
19
20
21#define TICKSPERSEC 10000000
22#define TICKSPERMSEC 10000
23#define SECSPERDAY 86400
24#define SECSPERHOUR 3600
25#define SECSPERMIN 60
26#define MINSPERHOUR 60
27#define HOURSPERDAY 24
28#define EPOCHWEEKDAY 0
29#define DAYSPERWEEK 7
30#define EPOCHYEAR 1601
31#define DAYSPERNORMALYEAR 365
32#define DAYSPERLEAPYEAR 366
33#define MONSPERYEAR 12
34
35static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
36static const int MonthLengths[2][MONSPERYEAR] =
37{
38 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
39 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
40};
41
42static int IsLeapYear(int Year)
43{
44 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
45}
46
47static void NormalizeTimeFields(CSHORT *FieldToNormalize,
48 CSHORT *CarryField,
49 int Modulus)
50{
51 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
52 *CarryField = (CSHORT) (*CarryField + 1);
53}
54
55/******************************************************************************
56 * RtlTimeToTimeFields [NTDLL.265]
57 *
58 */
59
60/* @@@PH we need 64-bit arithmetics here */
61#if 0
62VOID WINAPI RtlTimeToTimeFields(
63 PLARGE_INTEGER liTime,
64 PTIME_FIELDS TimeFields)
65{
66 const int *Months;
67 int LeapSecondCorrections, SecondsInDay, CurYear;
68 int LeapYear, CurMonth, GMTOffset;
69 long int Days;
70 long long int Time = *(long long int *)&liTime;
71
72 /* Extract millisecond from time and convert time into seconds */
73 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
74 Time = Time / TICKSPERSEC;
75
76 /* FIXME: Compute the number of leap second corrections here */
77 LeapSecondCorrections = 0;
78
79 /* FIXME: get the GMT offset here */
80 GMTOffset = 0;
81
82 /* Split the time into days and seconds within the day */
83 Days = Time / SECSPERDAY;
84 SecondsInDay = Time % SECSPERDAY;
85
86 /* Adjust the values for GMT and leap seconds */
87 SecondsInDay += (GMTOffset - LeapSecondCorrections);
88 while (SecondsInDay < 0)
89 { SecondsInDay += SECSPERDAY;
90 Days--;
91 }
92 while (SecondsInDay >= SECSPERDAY)
93 { SecondsInDay -= SECSPERDAY;
94 Days++;
95 }
96
97 /* compute time of day */
98 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
99 SecondsInDay = SecondsInDay % SECSPERHOUR;
100 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
101 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
102
103 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
104
105 /* compute day of week */
106 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
107
108 /* compute year */
109 CurYear = EPOCHYEAR;
110 /* FIXME: handle calendar modifications */
111 while (1)
112 { LeapYear = IsLeapYear(CurYear);
113 if (Days < (long) YearLengths[LeapYear])
114 { break;
115 }
116 CurYear++;
117 Days = Days - (long) YearLengths[LeapYear];
118 }
119 TimeFields->Year = (CSHORT) CurYear;
120
121 /* Compute month of year */
122 Months = MonthLengths[LeapYear];
123 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
124 Days = Days - (long) Months[CurMonth];
125 TimeFields->Month = (CSHORT) (CurMonth + 1);
126 TimeFields->Day = (CSHORT) (Days + 1);
127}
128
129
130/******************************************************************************
131 * RtlTimeFieldsToTime [NTDLL.265]
132 *
133 */
134BOOLEAN WINAPI RtlTimeFieldsToTime(
135 PTIME_FIELDS tfTimeFields,
136 PLARGE_INTEGER Time)
137{
138 int CurYear, CurMonth;
139 long long int rcTime;
140 TIME_FIELDS TimeFields = *tfTimeFields;
141
142 rcTime = 0;
143
144 /* FIXME: normalize the TIME_FIELDS structure here */
145 while (TimeFields.Second >= SECSPERMIN)
146 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
147 }
148 while (TimeFields.Minute >= MINSPERHOUR)
149 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
150 }
151 while (TimeFields.Hour >= HOURSPERDAY)
152 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
153 }
154 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
155 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
156 }
157 while (TimeFields.Month > MONSPERYEAR)
158 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
159 }
160
161 /* FIXME: handle calendar corrections here */
162 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
163 { rcTime += YearLengths[IsLeapYear(CurYear)];
164 }
165 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
166 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
167 }
168 rcTime += TimeFields.Day - 1;
169 rcTime *= SECSPERDAY;
170 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
171 rcTime *= TICKSPERSEC;
172 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
173 *Time = *(LARGE_INTEGER *)&rcTime;
174
175 return TRUE;
176}
177#endif
178
179/************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
180
181/******************************************************************************
182 * RtlSystemTimeToLocalTime [NTDLL]
183 */
184VOID WINAPI RtlSystemTimeToLocalTime(PLARGE_INTEGER SystemTime,
185 PLARGE_INTEGER LocalTime)
186{
187 dprintf(("NTDLL: RtlSystemTimeToLocalTime(%08xh,%08xh) not implemented.\n",
188 SystemTime,
189 LocalTime));
190
191 memcpy (LocalTime,
192 SystemTime,
193 sizeof (PLARGE_INTEGER));
194}
195
196
197/******************************************************************************
198 * RtlToTimeInSecondsSince1980 [NTDLL]
199 */
200BOOLEAN WINAPI RtlTimeToSecondsSince1980(LPFILETIME ft,
201 LPDWORD timeret)
202{
203 dprintf(("NTDLL: RtlTimeToSecondsSince1980(%08xh,%08xh) not implemented.\n",
204 ft,
205 timeret));
206
207 /* 1980 = 1970+10*365 days + 29. februar 1972 + 29.februar 1976 */
208 //*timeret = DOSFS_FileTimeToUnixTime(ft,NULL) - (10*365+2)*24*3600;
209 return 1;
210}
211
212/******************************************************************************
213 * RtlToTimeInSecondsSince1970 [NTDLL]
214 */
215BOOLEAN WINAPI RtlTimeToSecondsSince1970(LPFILETIME ft,
216 LPDWORD timeret)
217{
218 dprintf(("NTDLL: RtlTimeToSecondsSince1970(%08xh,%08xh) not implemented.\n",
219 ft,
220 timeret));
221
222 //*timeret = DOSFS_FileTimeToUnixTime(ft,NULL);
223 return 1;
224}
225
226
227/******************************************************************************
228 * RtlTimeToElapsedTimeFields [NTDLL.502]
229 * FIXME: prototype guessed
230 */
231VOID WINAPI RtlTimeToElapsedTimeFields(PLARGE_INTEGER liTime,
232 PTIME_FIELDS TimeFields)
233{
234 dprintf(("NTDLL: RtlTimeToElapsedTimeFields(%08xh,%08xh) not implemented.\n",
235 liTime,
236 TimeFields));
237}
Note: See TracBrowser for help on using the repository browser.