ReactOS 0.4.16-dev-1020-gf135cab
localtime.cpp File Reference
Include dependency graph for localtime.cpp:

Go to the source code of this file.

Functions

template<typename TimeType >
static errno_t __cdecl common_localtime_s (tm *const ptm, TimeType const *const ptime) throw ()
 
errno_t __cdecl _localtime32_s (tm *const ptm, __time32_t const *const ptime)
 
errno_t __cdecl _localtime64_s (tm *const ptm, __time64_t const *const ptime)
 
template<typename TimeType >
 _Success_ (return !=0) static tm *__cdecl common_localtime(TimeType const *const ptime) throw ()
 
tm *__cdecl _localtime32 (__time32_t const *const ptime)
 
tm *__cdecl _localtime64 (__time64_t const *const ptime)
 

Function Documentation

◆ _localtime32()

tm *__cdecl _localtime32 ( __time32_t const *const  ptime)

Definition at line 203 of file localtime.cpp.

204{
205 return common_localtime(ptime);
206}

◆ _localtime32_s()

errno_t __cdecl _localtime32_s ( tm *const  ptm,
__time32_t const *const  ptime 
)

Definition at line 165 of file localtime.cpp.

169{
170 return common_localtime_s(ptm, ptime);
171}
static errno_t __cdecl common_localtime_s(tm *const ptm, TimeType const *const ptime)
Definition: localtime.cpp:33

◆ _localtime64()

tm *__cdecl _localtime64 ( __time64_t const *const  ptime)

Definition at line 208 of file localtime.cpp.

209{
210 return common_localtime(ptime);
211}

Referenced by localtime().

◆ _localtime64_s()

errno_t __cdecl _localtime64_s ( tm *const  ptm,
__time64_t const *const  ptime 
)

Definition at line 173 of file localtime.cpp.

177{
178 return common_localtime_s(ptm, ptime);
179}

◆ _Success_()

template<typename TimeType >
_Success_ ( return = 0) const
throw (
)

Definition at line 187 of file localtime.cpp.

189{
190 typedef __crt_time_time_t_traits<TimeType> time_traits;
191
192 tm* const ptm = __getgmtimebuf();
193 if (ptm == nullptr)
194 return nullptr;
195
196 errno_t const status = time_traits::localtime_s(ptm, ptime);
197 if (status != 0)
198 return nullptr;
199
200 return ptm;
201}
tm *__cdecl __getgmtimebuf()
Definition: gmtime.cpp:154
Definition: ps.c:97
Definition: time.h:68
int errno_t
Definition: corecrt.h:615

◆ common_localtime_s()

template<typename TimeType >
static errno_t __cdecl common_localtime_s ( tm *const  ptm,
TimeType const *const  ptime 
)
throw (
)
static

Definition at line 33 of file localtime.cpp.

37{
38 typedef __crt_time_time_t_traits<TimeType> time_traits;
39
40 _VALIDATE_RETURN_ERRCODE(ptm != nullptr, EINVAL);
41 memset(ptm, 0xff, sizeof(tm));
42
43 _VALIDATE_RETURN_ERRCODE(ptime != nullptr, EINVAL);
44
45 // Check for illegal time_t value:
47 _VALIDATE_RETURN_ERRCODE_NOEXC(*ptime <= time_traits::max_time_t, EINVAL);
48
49 __tzset();
50
51 int daylight = 0;
52 long dstbias = 0;
53 long timezone = 0;
55 _ERRCHECK(_get_dstbias (&dstbias ));
57
58 if (*ptime > 3 * _DAY_SEC && *ptime < time_traits::max_time_t - 3 * _DAY_SEC)
59 {
60 // The date does not fall within the first three or last three representable
61 // days; therefore, there is no possibility of overflowing or underflowing
62 // the time_t representation as we compensate for time zone and daylight
63 // savings time.
64 TimeType ltime = *ptime - timezone;
65
66 errno_t status0 = time_traits::gmtime_s(ptm, &ltime);
67 if (status0 != 0)
68 return status0;
69
70 // Check and adjust for daylight savings time:
71 if (daylight && _isindst(ptm))
72 {
73 ltime -= dstbias;
74
75 errno_t const status1 = time_traits::gmtime_s(ptm, &ltime);
76 if (status1 != 0)
77 return status1;
78
79 ptm->tm_isdst = 1;
80 }
81 }
82 else
83 {
84 // The date falls within the first three or last three representable days;
85 // therefore, it is possible that the time_t representation would overflow
86 // or underflow while compensating for time zone and daylight savings time.
87 // Therefore, we make the time zone and daylight savings time adjustments
88 // directly in the tm structure.
89 errno_t const status0 = time_traits::gmtime_s(ptm, ptime);
90 if (status0 != 0)
91 return status0;
92
93 TimeType ltime = static_cast<TimeType>(ptm->tm_sec);
94
95 // First, adjust for the time zone:
96 if (daylight && _isindst(ptm))
97 {
98 ltime -= (timezone + dstbias);
99 ptm->tm_isdst = 1;
100 }
101 else
102 {
103 ltime -= timezone;
104 }
105
106 ptm->tm_sec = static_cast<int>(ltime % 60);
107 if (ptm->tm_sec < 0)
108 {
109 ptm->tm_sec += 60;
110 ltime -= 60;
111 }
112
113 ltime = static_cast<TimeType>(ptm->tm_min) + ltime / 60;
114 ptm->tm_min = static_cast<int>(ltime % 60);
115 if (ptm->tm_min < 0)
116 {
117 ptm->tm_min += 60;
118 ltime -= 60;
119 }
120
121 ltime = static_cast<TimeType>(ptm->tm_hour) + ltime / 60;
122 ptm->tm_hour = static_cast<int>(ltime % 24);
123 if (ptm->tm_hour < 0)
124 {
125 ptm->tm_hour += 24;
126 ltime -=24;
127 }
128
129 ltime /= 24;
130
131 if (ltime > 0)
132 {
133 // There is no possibility of overflowing the tm_day and tm_yday
134 // members because the date can be no later than January 19.
135 ptm->tm_wday = (ptm->tm_wday + static_cast<int>(ltime)) % 7;
136 ptm->tm_mday += static_cast<int>(ltime);
137 ptm->tm_yday += static_cast<int>(ltime);
138 }
139 else if (ltime < 0)
140 {
141 // It is possible to underflow the tm_mday and tm_yday fields. If
142 // this happens, then the adjusted date must lie in December 1969:
143 ptm->tm_wday = (ptm->tm_wday + 7 + static_cast<int>(ltime)) % 7;
144 ptm->tm_mday += static_cast<int>(ltime);
145 if (ptm->tm_mday <= 0)
146 {
147 ptm->tm_mday += 31;
148
149 // Per assumption #4 above, the time zone can cause the date to
150 // underflow the epoch by more than a day.
151 ptm->tm_yday = ptm->tm_yday + static_cast<int>(ltime) + 365;
152 ptm->tm_mon = 11;
153 ptm->tm_year--;
154 }
155 else
156 {
157 ptm->tm_yday += static_cast<int>(ltime);
158 }
159 }
160 }
161
162 return 0;
163}
#define EINVAL
Definition: acclib.h:90
#define _ERRCHECK(e)
void __cdecl __tzset()
Definition: tzset.cpp:392
int __cdecl _isindst(_In_ tm *_Time)
#define _DAY_SEC
#define _VALIDATE_RETURN_ERRCODE(expr, errorcode)
#define _VALIDATE_RETURN_ERRCODE_NOEXC(expr, errorcode)
_CRTIMP errno_t __cdecl _get_daylight(_Out_ int *_Daylight)
_CRTIMP errno_t __cdecl _get_dstbias(_Out_ long *_Daylight_savings_bias)
_CRTIMP int daylight
_CRTIMP errno_t __cdecl _get_timezone(_Out_ long *_Timezone)
#define memset(x, y, z)
Definition: compat.h:39
Definition: fake.h:14
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_isdst
Definition: time.h:77
int tm_yday
Definition: time.h:76
int tm_mday
Definition: time.h:72
int tm_min
Definition: time.h:70
int tm_wday
Definition: time.h:75

Referenced by _localtime32_s(), and _localtime64_s().