ReactOS 0.4.15-dev-7660-g0086d05
mktime.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: LGPL, See LGPL.txt in the top level directory
3 * PROJECT: ReactOS CRT library
4 * FILE: lib/sdk/crt/time/mktime.c
5 * PURPOSE: Implementation of mktime, _mkgmtime
6 * PROGRAMERS: Timo Kreuzer
7 */
8#include <precomp.h>
9#include "bitsfixup.h"
10
11#define MAX_32BIT_TIME 0xFFFFFFFFULL
12
13static int g_monthdays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
14
15__time64_t
16mktime_worker(struct tm * ptm, int utc)
17{
18 struct tm *ptm2;
19 __time64_t time;
20 int mons, years, leapyears;
22 DWORD ret;
23
24 /* Normalize year and month */
25 if (ptm->tm_mon < 0)
26 {
27 mons = -ptm->tm_mon - 1;
28 ptm->tm_year -= 1 + mons / 12;
29 ptm->tm_mon = 11 - (mons % 12);
30 }
31 else if (ptm->tm_mon > 11)
32 {
33 mons = ptm->tm_mon;
34 ptm->tm_year += (mons / 12);
35 ptm->tm_mon = mons % 12;
36 }
37
38 /* Is it inside margins */
39 if (ptm->tm_year < 70 || ptm->tm_year > 139) // FIXME: max year for 64 bits
40 {
41 return -1;
42 }
43
44 years = ptm->tm_year - 70;
45
46 /* Number of leapyears passed since 1970 */
47 leapyears = (years + 1) / 4;
48
49 /* Calculate days up to 1st of Jan */
50 time = years * 365 + leapyears;
51
52 /* Calculate days up to 1st of month */
53 time += g_monthdays[ptm->tm_mon];
54
55 /* Check if we need to add a leap day */
56 if (((years + 2) % 4) == 0)
57 {
58 if (ptm->tm_mon > 2)
59 {
60 time++;
61 }
62 }
63
64 time += ptm->tm_mday - 1;
65
66 time *= 24;
67 time += ptm->tm_hour;
68
69 time *= 60;
70 time += ptm->tm_min;
71
72 time *= 60;
73 time += ptm->tm_sec;
74
75 if (time < 0)
76 {
77 return -1;
78 }
79
80 /* Finally get normalized tm struct */
81 ptm2 = _gmtime64(&time);
82 if (!ptm2)
83 {
84 return -1;
85 }
86 *ptm = *ptm2;
87
88 /* Finally adjust by the difference to GMT in seconds */
91 {
92 time += tzi.Bias * 60;
93 }
94
95 return time;
96}
97
98/* int tm_sec;
99 int tm_min;
100 int tm_hour;
101 int tm_mday;
102 int tm_mon;
103 int tm_year;
104 int tm_wday;
105 int tm_yday;
106 int tm_isdst;
107*/
108
113time_t
114_mkgmtime(struct tm *ptm)
115{
116 __time64_t time = mktime_worker(ptm, 1);
117 return (time_t)((time > MAX_32BIT_TIME) ? -1 : time);
118}
119
120time_t
121mktime(struct tm *ptm)
122{
123 __time64_t time = mktime_worker(ptm, 0);
124 return (time_t)((time > MAX_32BIT_TIME) ? -1 : time);
125}
126
128_mkgmtime32(struct tm *ptm)
129{
130 __time64_t time = mktime_worker(ptm, 1);
131 return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time);
132}
133
135_mktime32(struct tm *ptm)
136{
137 __time64_t time = mktime_worker(ptm, 0);
138 return (__time32_t)((time > MAX_32BIT_TIME) ? -1 : time);
139}
140
141__time64_t
142_mkgmtime64(struct tm *ptm)
143{
144 return mktime_worker(ptm, 1);
145}
146
147__time64_t
148_mktime64(struct tm *ptm)
149{
150 return mktime_worker(ptm, 0);
151}
long __time32_t
Definition: crtdefs.h:379
static DOUBLE utc(DOUBLE time, DateInstance *date)
Definition: date.c:357
DWORD WINAPI GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
Definition: timezone.c:262
__kernel_time_t time_t
Definition: linux.h:252
unsigned long DWORD
Definition: ntddk_ex.h:95
struct tm * _gmtime64(const __time64_t *ptime)
Definition: gmtime.c:104
__u16 time
Definition: mkdosfs.c:8
_CRTIMP time_t __cdecl mktime(struct tm *_Tm)
Definition: time.h:418
__time32_t _mkgmtime32(struct tm *ptm)
Definition: mktime.c:128
__time32_t _mktime32(struct tm *ptm)
Definition: mktime.c:135
__time64_t mktime_worker(struct tm *ptm, int utc)
Definition: mktime.c:16
time_t _mkgmtime(struct tm *ptm)
Definition: mktime.c:114
static int g_monthdays[13]
Definition: mktime.c:13
#define MAX_32BIT_TIME
Definition: mktime.c:11
__time64_t _mkgmtime64(struct tm *ptm)
Definition: mktime.c:142
__time64_t _mktime64(struct tm *ptm)
Definition: mktime.c:148
Definition: time.h:68
int tm_mon
Definition: time.h:73
int tm_year
Definition: time.h:74
int tm_hour
Definition: time.h:71
int tm_sec
Definition: time.h:69
int tm_mday
Definition: time.h:72
int tm_min
Definition: time.h:70
int ret
#define TIME_ZONE_ID_INVALID
Definition: winbase.h:286