]>
Commit | Line | Data |
---|---|---|
056a1eb7 SF |
1 | /* $Id: time.cpp $ */ |
2 | /** @file | |
3 | * IPRT - Time. | |
4 | */ | |
5 | ||
6 | /* | |
7 | * Copyright (C) 2006-2016 Oracle Corporation | |
8 | * | |
9 | * This file is part of VirtualBox Open Source Edition (OSE), as | |
10 | * available from http://www.virtualbox.org. This file is free software; | |
11 | * you can redistribute it and/or modify it under the terms of the GNU | |
12 | * General Public License (GPL) as published by the Free Software | |
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the | |
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the | |
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. | |
16 | * | |
17 | * The contents of this file may alternatively be used under the terms | |
18 | * of the Common Development and Distribution License Version 1.0 | |
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the | |
20 | * VirtualBox OSE distribution, in which case the provisions of the | |
21 | * CDDL are applicable instead of those of the GPL. | |
22 | * | |
23 | * You may elect to license modified versions of this file under the | |
24 | * terms and conditions of either the GPL or the CDDL or both. | |
25 | */ | |
26 | ||
27 | ||
28 | /********************************************************************************************************************************* | |
29 | * Header Files * | |
30 | *********************************************************************************************************************************/ | |
31 | #define LOG_GROUP RTLOGGROUP_TIME | |
32 | #include <iprt/time.h> | |
33 | #include "internal/iprt.h" | |
34 | ||
35 | #include <iprt/ctype.h> | |
36 | #include <iprt/string.h> | |
37 | #include <iprt/assert.h> | |
38 | #include "internal/time.h" | |
39 | ||
40 | ||
41 | /********************************************************************************************************************************* | |
42 | * Defined Constants And Macros * | |
43 | *********************************************************************************************************************************/ | |
44 | /** The max year we possibly could implode. */ | |
45 | #define RTTIME_MAX_YEAR (292 + 1970) | |
46 | /** The min year we possibly could implode. */ | |
47 | #define RTTIME_MIN_YEAR (-293 + 1970) | |
48 | ||
49 | /** The max day supported by our time representation. (2262-04-11T23-47-16.854775807) */ | |
50 | #define RTTIME_MAX_DAY (365*292+71 + 101-1) | |
51 | /** The min day supported by our time representation. (1677-09-21T00-12-43.145224192) */ | |
52 | #define RTTIME_MIN_DAY (365*-293-70 + 264-1) | |
53 | ||
54 | /** The max nano second into the max day. (2262-04-11T23-47-16.854775807) */ | |
55 | #define RTTIME_MAX_DAY_NANO ( INT64_C(1000000000) * (23*3600 + 47*60 + 16) + 854775807 ) | |
56 | /** The min nano second into the min day. (1677-09-21T00-12-43.145224192) */ | |
57 | #define RTTIME_MIN_DAY_NANO ( INT64_C(1000000000) * (00*3600 + 12*60 + 43) + 145224192 ) | |
58 | ||
59 | ||
60 | /********************************************************************************************************************************* | |
61 | * Global Variables * | |
62 | *********************************************************************************************************************************/ | |
63 | /** | |
64 | * Days per month in a common year. | |
65 | */ | |
66 | static const uint8_t g_acDaysInMonths[12] = | |
67 | { | |
68 | /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */ | |
69 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | |
70 | }; | |
71 | ||
72 | /** | |
73 | * Days per month in a leap year. | |
74 | */ | |
75 | static const uint8_t g_acDaysInMonthsLeap[12] = | |
76 | { | |
77 | /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */ | |
78 | 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | |
79 | }; | |
80 | ||
81 | /** | |
82 | * The day of year for each month in a common year. | |
83 | */ | |
84 | static const uint16_t g_aiDayOfYear[12 + 1] = | |
85 | { | |
86 | 1, /* Jan */ | |
87 | 1+31, /* Feb */ | |
88 | 1+31+28, /* Mar */ | |
89 | 1+31+28+31, /* Apr */ | |
90 | 1+31+28+31+30, /* May */ | |
91 | 1+31+28+31+30+31, /* Jun */ | |
92 | 1+31+28+31+30+31+30, /* Jul */ | |
93 | 1+31+28+31+30+31+30+31, /* Aug */ | |
94 | 1+31+28+31+30+31+30+31+31, /* Sep */ | |
95 | 1+31+28+31+30+31+30+31+31+30, /* Oct */ | |
96 | 1+31+28+31+30+31+30+31+31+30+31, /* Nov */ | |
97 | 1+31+28+31+30+31+30+31+31+30+31+30, /* Dec */ | |
98 | 1+31+28+31+30+31+30+31+31+30+31+30+31 | |
99 | }; | |
100 | ||
101 | /** | |
102 | * The day of year for each month in a leap year. | |
103 | */ | |
104 | static const uint16_t g_aiDayOfYearLeap[12 + 1] = | |
105 | { | |
106 | 1, /* Jan */ | |
107 | 1+31, /* Feb */ | |
108 | 1+31+29, /* Mar */ | |
109 | 1+31+29+31, /* Apr */ | |
110 | 1+31+29+31+30, /* May */ | |
111 | 1+31+29+31+30+31, /* Jun */ | |
112 | 1+31+29+31+30+31+30, /* Jul */ | |
113 | 1+31+29+31+30+31+30+31, /* Aug */ | |
114 | 1+31+29+31+30+31+30+31+31, /* Sep */ | |
115 | 1+31+29+31+30+31+30+31+31+30, /* Oct */ | |
116 | 1+31+29+31+30+31+30+31+31+30+31, /* Nov */ | |
117 | 1+31+29+31+30+31+30+31+31+30+31+30, /* Dec */ | |
118 | 1+31+29+31+30+31+30+31+31+30+31+30+31 | |
119 | }; | |
120 | ||
121 | /** The index of 1970 in g_aoffYear */ | |
122 | #define OFF_YEAR_IDX_EPOCH 300 | |
123 | /** The year of the first index. */ | |
124 | #define OFF_YEAR_IDX_0_YEAR 1670 | |
125 | ||
126 | /** | |
127 | * The number of days the 1st of January a year is offseted from 1970-01-01. | |
128 | */ | |
129 | static const int32_t g_aoffYear[] = | |
130 | { | |
131 | /*1670:*/ 365*-300+-72, 365*-299+-72, 365*-298+-72, 365*-297+-71, 365*-296+-71, 365*-295+-71, 365*-294+-71, 365*-293+-70, 365*-292+-70, 365*-291+-70, | |
132 | /*1680:*/ 365*-290+-70, 365*-289+-69, 365*-288+-69, 365*-287+-69, 365*-286+-69, 365*-285+-68, 365*-284+-68, 365*-283+-68, 365*-282+-68, 365*-281+-67, | |
133 | /*1690:*/ 365*-280+-67, 365*-279+-67, 365*-278+-67, 365*-277+-66, 365*-276+-66, 365*-275+-66, 365*-274+-66, 365*-273+-65, 365*-272+-65, 365*-271+-65, | |
134 | /*1700:*/ 365*-270+-65, 365*-269+-65, 365*-268+-65, 365*-267+-65, 365*-266+-65, 365*-265+-64, 365*-264+-64, 365*-263+-64, 365*-262+-64, 365*-261+-63, | |
135 | /*1710:*/ 365*-260+-63, 365*-259+-63, 365*-258+-63, 365*-257+-62, 365*-256+-62, 365*-255+-62, 365*-254+-62, 365*-253+-61, 365*-252+-61, 365*-251+-61, | |
136 | /*1720:*/ 365*-250+-61, 365*-249+-60, 365*-248+-60, 365*-247+-60, 365*-246+-60, 365*-245+-59, 365*-244+-59, 365*-243+-59, 365*-242+-59, 365*-241+-58, | |
137 | /*1730:*/ 365*-240+-58, 365*-239+-58, 365*-238+-58, 365*-237+-57, 365*-236+-57, 365*-235+-57, 365*-234+-57, 365*-233+-56, 365*-232+-56, 365*-231+-56, | |
138 | /*1740:*/ 365*-230+-56, 365*-229+-55, 365*-228+-55, 365*-227+-55, 365*-226+-55, 365*-225+-54, 365*-224+-54, 365*-223+-54, 365*-222+-54, 365*-221+-53, | |
139 | /*1750:*/ 365*-220+-53, 365*-219+-53, 365*-218+-53, 365*-217+-52, 365*-216+-52, 365*-215+-52, 365*-214+-52, 365*-213+-51, 365*-212+-51, 365*-211+-51, | |
140 | /*1760:*/ 365*-210+-51, 365*-209+-50, 365*-208+-50, 365*-207+-50, 365*-206+-50, 365*-205+-49, 365*-204+-49, 365*-203+-49, 365*-202+-49, 365*-201+-48, | |
141 | /*1770:*/ 365*-200+-48, 365*-199+-48, 365*-198+-48, 365*-197+-47, 365*-196+-47, 365*-195+-47, 365*-194+-47, 365*-193+-46, 365*-192+-46, 365*-191+-46, | |
142 | /*1780:*/ 365*-190+-46, 365*-189+-45, 365*-188+-45, 365*-187+-45, 365*-186+-45, 365*-185+-44, 365*-184+-44, 365*-183+-44, 365*-182+-44, 365*-181+-43, | |
143 | /*1790:*/ 365*-180+-43, 365*-179+-43, 365*-178+-43, 365*-177+-42, 365*-176+-42, 365*-175+-42, 365*-174+-42, 365*-173+-41, 365*-172+-41, 365*-171+-41, | |
144 | /*1800:*/ 365*-170+-41, 365*-169+-41, 365*-168+-41, 365*-167+-41, 365*-166+-41, 365*-165+-40, 365*-164+-40, 365*-163+-40, 365*-162+-40, 365*-161+-39, | |
145 | /*1810:*/ 365*-160+-39, 365*-159+-39, 365*-158+-39, 365*-157+-38, 365*-156+-38, 365*-155+-38, 365*-154+-38, 365*-153+-37, 365*-152+-37, 365*-151+-37, | |
146 | /*1820:*/ 365*-150+-37, 365*-149+-36, 365*-148+-36, 365*-147+-36, 365*-146+-36, 365*-145+-35, 365*-144+-35, 365*-143+-35, 365*-142+-35, 365*-141+-34, | |
147 | /*1830:*/ 365*-140+-34, 365*-139+-34, 365*-138+-34, 365*-137+-33, 365*-136+-33, 365*-135+-33, 365*-134+-33, 365*-133+-32, 365*-132+-32, 365*-131+-32, | |
148 | /*1840:*/ 365*-130+-32, 365*-129+-31, 365*-128+-31, 365*-127+-31, 365*-126+-31, 365*-125+-30, 365*-124+-30, 365*-123+-30, 365*-122+-30, 365*-121+-29, | |
149 | /*1850:*/ 365*-120+-29, 365*-119+-29, 365*-118+-29, 365*-117+-28, 365*-116+-28, 365*-115+-28, 365*-114+-28, 365*-113+-27, 365*-112+-27, 365*-111+-27, | |
150 | /*1860:*/ 365*-110+-27, 365*-109+-26, 365*-108+-26, 365*-107+-26, 365*-106+-26, 365*-105+-25, 365*-104+-25, 365*-103+-25, 365*-102+-25, 365*-101+-24, | |
151 | /*1870:*/ 365*-100+-24, 365* -99+-24, 365* -98+-24, 365* -97+-23, 365* -96+-23, 365* -95+-23, 365* -94+-23, 365* -93+-22, 365* -92+-22, 365* -91+-22, | |
152 | /*1880:*/ 365* -90+-22, 365* -89+-21, 365* -88+-21, 365* -87+-21, 365* -86+-21, 365* -85+-20, 365* -84+-20, 365* -83+-20, 365* -82+-20, 365* -81+-19, | |
153 | /*1890:*/ 365* -80+-19, 365* -79+-19, 365* -78+-19, 365* -77+-18, 365* -76+-18, 365* -75+-18, 365* -74+-18, 365* -73+-17, 365* -72+-17, 365* -71+-17, | |
154 | /*1900:*/ 365* -70+-17, 365* -69+-17, 365* -68+-17, 365* -67+-17, 365* -66+-17, 365* -65+-16, 365* -64+-16, 365* -63+-16, 365* -62+-16, 365* -61+-15, | |
155 | /*1910:*/ 365* -60+-15, 365* -59+-15, 365* -58+-15, 365* -57+-14, 365* -56+-14, 365* -55+-14, 365* -54+-14, 365* -53+-13, 365* -52+-13, 365* -51+-13, | |
156 | /*1920:*/ 365* -50+-13, 365* -49+-12, 365* -48+-12, 365* -47+-12, 365* -46+-12, 365* -45+-11, 365* -44+-11, 365* -43+-11, 365* -42+-11, 365* -41+-10, | |
157 | /*1930:*/ 365* -40+-10, 365* -39+-10, 365* -38+-10, 365* -37+-9 , 365* -36+-9 , 365* -35+-9 , 365* -34+-9 , 365* -33+-8 , 365* -32+-8 , 365* -31+-8 , | |
158 | /*1940:*/ 365* -30+-8 , 365* -29+-7 , 365* -28+-7 , 365* -27+-7 , 365* -26+-7 , 365* -25+-6 , 365* -24+-6 , 365* -23+-6 , 365* -22+-6 , 365* -21+-5 , | |
159 | /*1950:*/ 365* -20+-5 , 365* -19+-5 , 365* -18+-5 , 365* -17+-4 , 365* -16+-4 , 365* -15+-4 , 365* -14+-4 , 365* -13+-3 , 365* -12+-3 , 365* -11+-3 , | |
160 | /*1960:*/ 365* -10+-3 , 365* -9+-2 , 365* -8+-2 , 365* -7+-2 , 365* -6+-2 , 365* -5+-1 , 365* -4+-1 , 365* -3+-1 , 365* -2+-1 , 365* -1+0 , | |
161 | /*1970:*/ 365* 0+0 , 365* 1+0 , 365* 2+0 , 365* 3+1 , 365* 4+1 , 365* 5+1 , 365* 6+1 , 365* 7+2 , 365* 8+2 , 365* 9+2 , | |
162 | /*1980:*/ 365* 10+2 , 365* 11+3 , 365* 12+3 , 365* 13+3 , 365* 14+3 , 365* 15+4 , 365* 16+4 , 365* 17+4 , 365* 18+4 , 365* 19+5 , | |
163 | /*1990:*/ 365* 20+5 , 365* 21+5 , 365* 22+5 , 365* 23+6 , 365* 24+6 , 365* 25+6 , 365* 26+6 , 365* 27+7 , 365* 28+7 , 365* 29+7 , | |
164 | /*2000:*/ 365* 30+7 , 365* 31+8 , 365* 32+8 , 365* 33+8 , 365* 34+8 , 365* 35+9 , 365* 36+9 , 365* 37+9 , 365* 38+9 , 365* 39+10 , | |
165 | /*2010:*/ 365* 40+10 , 365* 41+10 , 365* 42+10 , 365* 43+11 , 365* 44+11 , 365* 45+11 , 365* 46+11 , 365* 47+12 , 365* 48+12 , 365* 49+12 , | |
166 | /*2020:*/ 365* 50+12 , 365* 51+13 , 365* 52+13 , 365* 53+13 , 365* 54+13 , 365* 55+14 , 365* 56+14 , 365* 57+14 , 365* 58+14 , 365* 59+15 , | |
167 | /*2030:*/ 365* 60+15 , 365* 61+15 , 365* 62+15 , 365* 63+16 , 365* 64+16 , 365* 65+16 , 365* 66+16 , 365* 67+17 , 365* 68+17 , 365* 69+17 , | |
168 | /*2040:*/ 365* 70+17 , 365* 71+18 , 365* 72+18 , 365* 73+18 , 365* 74+18 , 365* 75+19 , 365* 76+19 , 365* 77+19 , 365* 78+19 , 365* 79+20 , | |
169 | /*2050:*/ 365* 80+20 , 365* 81+20 , 365* 82+20 , 365* 83+21 , 365* 84+21 , 365* 85+21 , 365* 86+21 , 365* 87+22 , 365* 88+22 , 365* 89+22 , | |
170 | /*2060:*/ 365* 90+22 , 365* 91+23 , 365* 92+23 , 365* 93+23 , 365* 94+23 , 365* 95+24 , 365* 96+24 , 365* 97+24 , 365* 98+24 , 365* 99+25 , | |
171 | /*2070:*/ 365* 100+25 , 365* 101+25 , 365* 102+25 , 365* 103+26 , 365* 104+26 , 365* 105+26 , 365* 106+26 , 365* 107+27 , 365* 108+27 , 365* 109+27 , | |
172 | /*2080:*/ 365* 110+27 , 365* 111+28 , 365* 112+28 , 365* 113+28 , 365* 114+28 , 365* 115+29 , 365* 116+29 , 365* 117+29 , 365* 118+29 , 365* 119+30 , | |
173 | /*2090:*/ 365* 120+30 , 365* 121+30 , 365* 122+30 , 365* 123+31 , 365* 124+31 , 365* 125+31 , 365* 126+31 , 365* 127+32 , 365* 128+32 , 365* 129+32 , | |
174 | /*2100:*/ 365* 130+32 , 365* 131+32 , 365* 132+32 , 365* 133+32 , 365* 134+32 , 365* 135+33 , 365* 136+33 , 365* 137+33 , 365* 138+33 , 365* 139+34 , | |
175 | /*2110:*/ 365* 140+34 , 365* 141+34 , 365* 142+34 , 365* 143+35 , 365* 144+35 , 365* 145+35 , 365* 146+35 , 365* 147+36 , 365* 148+36 , 365* 149+36 , | |
176 | /*2120:*/ 365* 150+36 , 365* 151+37 , 365* 152+37 , 365* 153+37 , 365* 154+37 , 365* 155+38 , 365* 156+38 , 365* 157+38 , 365* 158+38 , 365* 159+39 , | |
177 | /*2130:*/ 365* 160+39 , 365* 161+39 , 365* 162+39 , 365* 163+40 , 365* 164+40 , 365* 165+40 , 365* 166+40 , 365* 167+41 , 365* 168+41 , 365* 169+41 , | |
178 | /*2140:*/ 365* 170+41 , 365* 171+42 , 365* 172+42 , 365* 173+42 , 365* 174+42 , 365* 175+43 , 365* 176+43 , 365* 177+43 , 365* 178+43 , 365* 179+44 , | |
179 | /*2150:*/ 365* 180+44 , 365* 181+44 , 365* 182+44 , 365* 183+45 , 365* 184+45 , 365* 185+45 , 365* 186+45 , 365* 187+46 , 365* 188+46 , 365* 189+46 , | |
180 | /*2160:*/ 365* 190+46 , 365* 191+47 , 365* 192+47 , 365* 193+47 , 365* 194+47 , 365* 195+48 , 365* 196+48 , 365* 197+48 , 365* 198+48 , 365* 199+49 , | |
181 | /*2170:*/ 365* 200+49 , 365* 201+49 , 365* 202+49 , 365* 203+50 , 365* 204+50 , 365* 205+50 , 365* 206+50 , 365* 207+51 , 365* 208+51 , 365* 209+51 , | |
182 | /*2180:*/ 365* 210+51 , 365* 211+52 , 365* 212+52 , 365* 213+52 , 365* 214+52 , 365* 215+53 , 365* 216+53 , 365* 217+53 , 365* 218+53 , 365* 219+54 , | |
183 | /*2190:*/ 365* 220+54 , 365* 221+54 , 365* 222+54 , 365* 223+55 , 365* 224+55 , 365* 225+55 , 365* 226+55 , 365* 227+56 , 365* 228+56 , 365* 229+56 , | |
184 | /*2200:*/ 365* 230+56 , 365* 231+56 , 365* 232+56 , 365* 233+56 , 365* 234+56 , 365* 235+57 , 365* 236+57 , 365* 237+57 , 365* 238+57 , 365* 239+58 , | |
185 | /*2210:*/ 365* 240+58 , 365* 241+58 , 365* 242+58 , 365* 243+59 , 365* 244+59 , 365* 245+59 , 365* 246+59 , 365* 247+60 , 365* 248+60 , 365* 249+60 , | |
186 | /*2220:*/ 365* 250+60 , 365* 251+61 , 365* 252+61 , 365* 253+61 , 365* 254+61 , 365* 255+62 , 365* 256+62 , 365* 257+62 , 365* 258+62 , 365* 259+63 , | |
187 | /*2230:*/ 365* 260+63 , 365* 261+63 , 365* 262+63 , 365* 263+64 , 365* 264+64 , 365* 265+64 , 365* 266+64 , 365* 267+65 , 365* 268+65 , 365* 269+65 , | |
188 | /*2240:*/ 365* 270+65 , 365* 271+66 , 365* 272+66 , 365* 273+66 , 365* 274+66 , 365* 275+67 , 365* 276+67 , 365* 277+67 , 365* 278+67 , 365* 279+68 , | |
189 | /*2250:*/ 365* 280+68 , 365* 281+68 , 365* 282+68 , 365* 283+69 , 365* 284+69 , 365* 285+69 , 365* 286+69 , 365* 287+70 , 365* 288+70 , 365* 289+70 , | |
190 | /*2260:*/ 365* 290+70 , 365* 291+71 , 365* 292+71 , 365* 293+71 , 365* 294+71 , 365* 295+72 , 365* 296+72 , 365* 297+72 , 365* 298+72 , 365* 299+73 | |
191 | }; | |
192 | ||
193 | /* generator code: | |
194 | #include <stdio.h> | |
195 | bool isLeapYear(int iYear) | |
196 | { | |
197 | return iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0); | |
198 | } | |
199 | void printYear(int iYear, int iLeap) | |
200 | { | |
201 | if (!(iYear % 10)) | |
202 | printf("\n/" "*%d:*" "/", iYear + 1970); | |
203 | printf(" 365*%4d+%-3d,", iYear, iLeap); | |
204 | } | |
205 | int main() | |
206 | { | |
207 | int iYear = 0; | |
208 | int iLeap = 0; | |
209 | while (iYear > -300) | |
210 | iLeap -= isLeapYear(1970 + --iYear); | |
211 | while (iYear < 300) | |
212 | { | |
213 | printYear(iYear, iLeap); | |
214 | iLeap += isLeapYear(1970 + iYear++); | |
215 | } | |
216 | printf("\n"); | |
217 | return 0; | |
218 | } | |
219 | */ | |
220 | ||
221 | ||
222 | /** | |
223 | * Checks if a year is a leap year or not. | |
224 | * | |
225 | * @returns true if it's a leap year. | |
226 | * @returns false if it's a common year. | |
227 | * @param i32Year The year in question. | |
228 | */ | |
229 | DECLINLINE(bool) rtTimeIsLeapYear(int32_t i32Year) | |
230 | { | |
231 | return i32Year % 4 == 0 | |
232 | && ( i32Year % 100 != 0 | |
233 | || i32Year % 400 == 0); | |
234 | } | |
235 | ||
236 | ||
237 | /** | |
238 | * Checks if a year is a leap year or not. | |
239 | * | |
240 | * @returns true if it's a leap year. | |
241 | * @returns false if it's a common year. | |
242 | * @param i32Year The year in question. | |
243 | */ | |
244 | RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year) | |
245 | { | |
246 | return rtTimeIsLeapYear(i32Year); | |
247 | } | |
248 | RT_EXPORT_SYMBOL(RTTimeIsLeapYear); | |
249 | ||
250 | ||
251 | /** | |
252 | * Explodes a time spec (UTC). | |
253 | * | |
254 | * @returns pTime. | |
255 | * @param pTime Where to store the exploded time. | |
256 | * @param pTimeSpec The time spec to exploded. | |
257 | */ | |
258 | RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec) | |
259 | { | |
260 | int64_t i64Div; | |
261 | int32_t i32Div; | |
262 | int32_t i32Rem; | |
263 | unsigned iYear; | |
264 | const uint16_t *paiDayOfYear; | |
265 | int iMonth; | |
266 | ||
267 | AssertMsg(VALID_PTR(pTime), ("%p\n", pTime)); | |
268 | AssertMsg(VALID_PTR(pTimeSpec), ("%p\n", pTime)); | |
269 | ||
270 | /* | |
271 | * The simple stuff first. | |
272 | */ | |
273 | pTime->fFlags = RTTIME_FLAGS_TYPE_UTC; | |
274 | i64Div = pTimeSpec->i64NanosecondsRelativeToUnixEpoch; | |
275 | i32Rem = (int32_t)(i64Div % 1000000000); | |
276 | i64Div /= 1000000000; | |
277 | if (i32Rem < 0) | |
278 | { | |
279 | i32Rem += 1000000000; | |
280 | i64Div--; | |
281 | } | |
282 | pTime->u32Nanosecond = i32Rem; | |
283 | ||
284 | /* second */ | |
285 | i32Rem = (int32_t)(i64Div % 60); | |
286 | i64Div /= 60; | |
287 | if (i32Rem < 0) | |
288 | { | |
289 | i32Rem += 60; | |
290 | i64Div--; | |
291 | } | |
292 | pTime->u8Second = i32Rem; | |
293 | ||
294 | /* minute */ | |
295 | i32Div = (int32_t)i64Div; /* 60,000,000,000 > 33bit, so 31bit suffices. */ | |
296 | i32Rem = i32Div % 60; | |
297 | i32Div /= 60; | |
298 | if (i32Rem < 0) | |
299 | { | |
300 | i32Rem += 60; | |
301 | i32Div--; | |
302 | } | |
303 | pTime->u8Minute = i32Rem; | |
304 | ||
305 | /* hour */ | |
306 | i32Rem = i32Div % 24; | |
307 | i32Div /= 24; /* days relative to 1970-01-01 */ | |
308 | if (i32Rem < 0) | |
309 | { | |
310 | i32Rem += 24; | |
311 | i32Div--; | |
312 | } | |
313 | pTime->u8Hour = i32Rem; | |
314 | ||
315 | /* weekday - 1970-01-01 was a Thursday (3) */ | |
316 | pTime->u8WeekDay = ((int)(i32Div % 7) + 3 + 7) % 7; | |
317 | ||
318 | /* | |
319 | * We've now got a number of days relative to 1970-01-01. | |
320 | * To get the correct year number we have to mess with leap years. Fortunately, | |
321 | * the representation we've got only supports a few hundred years, so we can | |
322 | * generate a table and perform a simple two way search from the modulus 365 derived. | |
323 | */ | |
324 | iYear = OFF_YEAR_IDX_EPOCH + i32Div / 365; | |
325 | while (g_aoffYear[iYear + 1] <= i32Div) | |
326 | iYear++; | |
327 | while (g_aoffYear[iYear] > i32Div) | |
328 | iYear--; | |
329 | pTime->i32Year = iYear + OFF_YEAR_IDX_0_YEAR; | |
330 | i32Div -= g_aoffYear[iYear]; | |
331 | pTime->u16YearDay = i32Div + 1; | |
332 | ||
333 | /* | |
334 | * Figuring out the month is done in a manner similar to the year, only here we | |
335 | * ensure that the index is matching or too small. | |
336 | */ | |
337 | if (rtTimeIsLeapYear(pTime->i32Year)) | |
338 | { | |
339 | pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR; | |
340 | paiDayOfYear = &g_aiDayOfYearLeap[0]; | |
341 | } | |
342 | else | |
343 | { | |
344 | pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR; | |
345 | paiDayOfYear = &g_aiDayOfYear[0]; | |
346 | } | |
347 | iMonth = i32Div / 32; | |
348 | i32Div++; | |
349 | while (paiDayOfYear[iMonth + 1] <= i32Div) | |
350 | iMonth++; | |
351 | pTime->u8Month = iMonth + 1; | |
352 | i32Div -= paiDayOfYear[iMonth]; | |
353 | pTime->u8MonthDay = i32Div + 1; | |
354 | ||
355 | /* This is for UTC timespecs, so, no offset. */ | |
356 | pTime->offUTC = 0; | |
357 | ||
358 | return pTime; | |
359 | } | |
360 | RT_EXPORT_SYMBOL(RTTimeExplode); | |
361 | ||
362 | ||
363 | /** | |
364 | * Implodes exploded time to a time spec (UTC). | |
365 | * | |
366 | * @returns pTime on success. | |
367 | * @returns NULL if the pTime data is invalid. | |
368 | * @param pTimeSpec Where to store the imploded UTC time. | |
369 | * If pTime specifies a time which outside the range, maximum or | |
370 | * minimum values will be returned. | |
371 | * @param pTime Pointer to the exploded time to implode. | |
372 | * The fields u8Month, u8WeekDay and u8MonthDay are not used, | |
373 | * and all the other fields are expected to be within their | |
374 | * bounds. Use RTTimeNormalize() to calculate u16YearDay and | |
375 | * normalize the ranges of the fields. | |
376 | */ | |
377 | RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime) | |
378 | { | |
379 | int32_t i32Days; | |
380 | uint32_t u32Secs; | |
381 | int64_t i64Nanos; | |
382 | ||
383 | /* | |
384 | * Validate input. | |
385 | */ | |
386 | AssertReturn(VALID_PTR(pTimeSpec), NULL); | |
387 | AssertReturn(VALID_PTR(pTime), NULL); | |
388 | AssertReturn(pTime->u32Nanosecond < 1000000000, NULL); | |
389 | AssertReturn(pTime->u8Second < 60, NULL); | |
390 | AssertReturn(pTime->u8Minute < 60, NULL); | |
391 | AssertReturn(pTime->u8Hour < 24, NULL); | |
392 | AssertReturn(pTime->u16YearDay >= 1, NULL); | |
393 | AssertReturn(pTime->u16YearDay <= (rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365), NULL); | |
394 | AssertMsgReturn(pTime->i32Year <= RTTIME_MAX_YEAR && pTime->i32Year >= RTTIME_MIN_YEAR, ("%RI32\n", pTime->i32Year), NULL); | |
395 | ||
396 | /* | |
397 | * Do the conversion to nanoseconds. | |
398 | */ | |
399 | i32Days = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR] | |
400 | + pTime->u16YearDay - 1; | |
401 | AssertMsgReturn(i32Days <= RTTIME_MAX_DAY && i32Days >= RTTIME_MIN_DAY, ("%RI32\n", i32Days), NULL); | |
402 | ||
403 | u32Secs = pTime->u8Second | |
404 | + pTime->u8Minute * 60 | |
405 | + pTime->u8Hour * 3600; | |
406 | i64Nanos = (uint64_t)pTime->u32Nanosecond | |
407 | + u32Secs * UINT64_C(1000000000); | |
408 | AssertMsgReturn(i32Days != RTTIME_MAX_DAY || i64Nanos <= RTTIME_MAX_DAY_NANO, ("%RI64\n", i64Nanos), NULL); | |
409 | AssertMsgReturn(i32Days != RTTIME_MIN_DAY || i64Nanos >= RTTIME_MIN_DAY_NANO, ("%RI64\n", i64Nanos), NULL); | |
410 | ||
411 | i64Nanos += i32Days * UINT64_C(86400000000000); | |
412 | ||
413 | pTimeSpec->i64NanosecondsRelativeToUnixEpoch = i64Nanos; | |
414 | return pTimeSpec; | |
415 | } | |
416 | RT_EXPORT_SYMBOL(RTTimeImplode); | |
417 | ||
418 | ||
419 | /** | |
420 | * Internal worker for RTTimeNormalize and RTTimeLocalNormalize. | |
421 | * It doesn't adjust the UCT offset but leaves that for RTTimeLocalNormalize. | |
422 | */ | |
423 | static PRTTIME rtTimeNormalizeInternal(PRTTIME pTime) | |
424 | { | |
425 | unsigned uSecond; | |
426 | unsigned uMinute; | |
427 | unsigned uHour; | |
428 | bool fLeapYear; | |
429 | ||
430 | /* | |
431 | * Fix the YearDay and Month/MonthDay. | |
432 | */ | |
433 | fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
434 | if (!pTime->u16YearDay) | |
435 | { | |
436 | /* | |
437 | * The Month+MonthDay must present, overflow adjust them and calc the year day. | |
438 | */ | |
439 | AssertMsgReturn( pTime->u8Month | |
440 | && pTime->u8MonthDay, | |
441 | ("date=%d-%d-%d\n", pTime->i32Year, pTime->u8Month, pTime->u8MonthDay), | |
442 | NULL); | |
443 | while (pTime->u8Month > 12) | |
444 | { | |
445 | pTime->u8Month -= 12; | |
446 | pTime->i32Year++; | |
447 | fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
448 | pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR); | |
449 | } | |
450 | ||
451 | for (;;) | |
452 | { | |
453 | unsigned cDaysInMonth = fLeapYear | |
454 | ? g_acDaysInMonthsLeap[pTime->u8Month - 1] | |
455 | : g_acDaysInMonths[pTime->u8Month - 1]; | |
456 | if (pTime->u8MonthDay <= cDaysInMonth) | |
457 | break; | |
458 | pTime->u8MonthDay -= cDaysInMonth; | |
459 | if (pTime->u8Month != 12) | |
460 | pTime->u8Month++; | |
461 | else | |
462 | { | |
463 | pTime->u8Month = 1; | |
464 | pTime->i32Year++; | |
465 | fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
466 | pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR); | |
467 | } | |
468 | } | |
469 | ||
470 | pTime->u16YearDay = pTime->u8MonthDay - 1 | |
471 | + (fLeapYear | |
472 | ? g_aiDayOfYearLeap[pTime->u8Month - 1] | |
473 | : g_aiDayOfYear[pTime->u8Month - 1]); | |
474 | } | |
475 | else | |
476 | { | |
477 | /* | |
478 | * Are both YearDay and Month/MonthDay valid? | |
479 | * Check that they don't overflow and match, if not use YearDay (simpler). | |
480 | */ | |
481 | bool fRecalc = true; | |
482 | if ( pTime->u8Month | |
483 | && pTime->u8MonthDay) | |
484 | { | |
485 | do | |
486 | { | |
487 | uint16_t u16YearDay; | |
488 | ||
489 | /* If you change one, zero the other to make clear what you mean. */ | |
490 | AssertBreak(pTime->u8Month <= 12); | |
491 | AssertBreak(pTime->u8MonthDay <= (fLeapYear | |
492 | ? g_acDaysInMonthsLeap[pTime->u8Month - 1] | |
493 | : g_acDaysInMonths[pTime->u8Month - 1])); | |
494 | u16YearDay = pTime->u8MonthDay - 1 | |
495 | + (fLeapYear | |
496 | ? g_aiDayOfYearLeap[pTime->u8Month - 1] | |
497 | : g_aiDayOfYear[pTime->u8Month - 1]); | |
498 | AssertBreak(u16YearDay == pTime->u16YearDay); | |
499 | fRecalc = false; | |
500 | } while (0); | |
501 | } | |
502 | if (fRecalc) | |
503 | { | |
504 | const uint16_t *paiDayOfYear; | |
505 | ||
506 | /* overflow adjust YearDay */ | |
507 | while (pTime->u16YearDay > (fLeapYear ? 366 : 365)) | |
508 | { | |
509 | pTime->u16YearDay -= fLeapYear ? 366 : 365; | |
510 | pTime->i32Year++; | |
511 | fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
512 | pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR); | |
513 | } | |
514 | ||
515 | /* calc Month and MonthDay */ | |
516 | paiDayOfYear = fLeapYear | |
517 | ? &g_aiDayOfYearLeap[0] | |
518 | : &g_aiDayOfYear[0]; | |
519 | pTime->u8Month = 1; | |
520 | while (pTime->u16YearDay > paiDayOfYear[pTime->u8Month]) | |
521 | pTime->u8Month++; | |
522 | Assert(pTime->u8Month >= 1 && pTime->u8Month <= 12); | |
523 | pTime->u8MonthDay = pTime->u16YearDay - paiDayOfYear[pTime->u8Month - 1] + 1; | |
524 | } | |
525 | } | |
526 | ||
527 | /* | |
528 | * Fixup time overflows. | |
529 | * Use unsigned int values internally to avoid overflows. | |
530 | */ | |
531 | uSecond = pTime->u8Second; | |
532 | uMinute = pTime->u8Minute; | |
533 | uHour = pTime->u8Hour; | |
534 | ||
535 | while (pTime->u32Nanosecond >= 1000000000) | |
536 | { | |
537 | pTime->u32Nanosecond -= 1000000000; | |
538 | uSecond++; | |
539 | } | |
540 | ||
541 | while (uSecond >= 60) | |
542 | { | |
543 | uSecond -= 60; | |
544 | uMinute++; | |
545 | } | |
546 | ||
547 | while (uMinute >= 60) | |
548 | { | |
549 | uMinute -= 60; | |
550 | uHour++; | |
551 | } | |
552 | ||
553 | while (uHour >= 24) | |
554 | { | |
555 | uHour -= 24; | |
556 | ||
557 | /* This is really a RTTimeIncDay kind of thing... */ | |
558 | if (pTime->u16YearDay + 1 != (fLeapYear ? g_aiDayOfYearLeap[pTime->u8Month] : g_aiDayOfYear[pTime->u8Month])) | |
559 | { | |
560 | pTime->u16YearDay++; | |
561 | pTime->u8MonthDay++; | |
562 | } | |
563 | else if (pTime->u8Month != 12) | |
564 | { | |
565 | pTime->u16YearDay++; | |
566 | pTime->u8Month++; | |
567 | pTime->u8MonthDay = 1; | |
568 | } | |
569 | else | |
570 | { | |
571 | pTime->i32Year++; | |
572 | fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
573 | pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR); | |
574 | pTime->u16YearDay = 1; | |
575 | pTime->u8Month = 1; | |
576 | pTime->u8MonthDay = 1; | |
577 | } | |
578 | } | |
579 | ||
580 | pTime->u8Second = uSecond; | |
581 | pTime->u8Minute = uMinute; | |
582 | pTime->u8Hour = uHour; | |
583 | ||
584 | /* | |
585 | * Correct the leap year flag. | |
586 | * Assert if it's wrong, but ignore if unset. | |
587 | */ | |
588 | if (fLeapYear) | |
589 | { | |
590 | Assert(!(pTime->fFlags & RTTIME_FLAGS_COMMON_YEAR)); | |
591 | pTime->fFlags &= ~RTTIME_FLAGS_COMMON_YEAR; | |
592 | pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR; | |
593 | } | |
594 | else | |
595 | { | |
596 | Assert(!(pTime->fFlags & RTTIME_FLAGS_LEAP_YEAR)); | |
597 | pTime->fFlags &= ~RTTIME_FLAGS_LEAP_YEAR; | |
598 | pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR; | |
599 | } | |
600 | ||
601 | ||
602 | /* | |
603 | * Calc week day. | |
604 | * | |
605 | * 1970-01-01 was a Thursday (3), so find the number of days relative to | |
606 | * that point. We use the table when possible and a slow+stupid+brute-force | |
607 | * algorithm for points outside it. Feel free to optimize the latter by | |
608 | * using some clever formula. | |
609 | */ | |
610 | if ( pTime->i32Year >= OFF_YEAR_IDX_0_YEAR | |
611 | && pTime->i32Year < OFF_YEAR_IDX_0_YEAR + (int32_t)RT_ELEMENTS(g_aoffYear)) | |
612 | { | |
613 | int32_t offDays = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR] | |
614 | + pTime->u16YearDay -1; | |
615 | pTime->u8WeekDay = ((offDays % 7) + 3 + 7) % 7; | |
616 | } | |
617 | else | |
618 | { | |
619 | int32_t i32Year = pTime->i32Year; | |
620 | if (i32Year >= 1970) | |
621 | { | |
622 | uint64_t offDays = pTime->u16YearDay - 1; | |
623 | while (--i32Year >= 1970) | |
624 | offDays += rtTimeIsLeapYear(i32Year) ? 366 : 365; | |
625 | pTime->u8WeekDay = (uint8_t)((offDays + 3) % 7); | |
626 | } | |
627 | else | |
628 | { | |
629 | int64_t offDays = (fLeapYear ? -366 - 1 : -365 - 1) + pTime->u16YearDay; | |
630 | while (++i32Year < 1970) | |
631 | offDays -= rtTimeIsLeapYear(i32Year) ? 366 : 365; | |
632 | pTime->u8WeekDay = ((int)(offDays % 7) + 3 + 7) % 7; | |
633 | } | |
634 | } | |
635 | return pTime; | |
636 | } | |
637 | ||
638 | ||
639 | /** | |
640 | * Normalizes the fields of a time structure. | |
641 | * | |
642 | * It is possible to calculate year-day from month/day and vice | |
643 | * versa. If you adjust any of these, make sure to zero the | |
644 | * other so you make it clear which of the fields to use. If | |
645 | * it's ambiguous, the year-day field is used (and you get | |
646 | * assertions in debug builds). | |
647 | * | |
648 | * All the time fields and the year-day or month/day fields will | |
649 | * be adjusted for overflows. (Since all fields are unsigned, there | |
650 | * is no underflows.) It is possible to exploit this for simple | |
651 | * date math, though the recommended way of doing that to implode | |
652 | * the time into a timespec and do the math on that. | |
653 | * | |
654 | * @returns pTime on success. | |
655 | * @returns NULL if the data is invalid. | |
656 | * | |
657 | * @param pTime The time structure to normalize. | |
658 | * | |
659 | * @remarks This function doesn't work with local time, only with UTC time. | |
660 | */ | |
661 | RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime) | |
662 | { | |
663 | /* | |
664 | * Validate that we've got the minimum of stuff handy. | |
665 | */ | |
666 | AssertReturn(VALID_PTR(pTime), NULL); | |
667 | AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL); | |
668 | AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_LOCAL, ("Use RTTimeLocalNormalize!\n"), NULL); | |
669 | AssertMsgReturn(pTime->offUTC == 0, ("%d; Use RTTimeLocalNormalize!\n", pTime->offUTC), NULL); | |
670 | ||
671 | pTime = rtTimeNormalizeInternal(pTime); | |
672 | if (pTime) | |
673 | pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC; | |
674 | return pTime; | |
675 | } | |
676 | RT_EXPORT_SYMBOL(RTTimeNormalize); | |
677 | ||
678 | ||
679 | /** | |
680 | * Converts a time spec to a ISO date string. | |
681 | * | |
682 | * @returns psz on success. | |
683 | * @returns NULL on buffer underflow. | |
684 | * @param pTime The time. Caller should've normalized this. | |
685 | * @param psz Where to store the string. | |
686 | * @param cb The size of the buffer. | |
687 | */ | |
688 | RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb) | |
689 | { | |
690 | size_t cch; | |
691 | ||
692 | /* (Default to UTC if not specified) */ | |
693 | if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL | |
694 | && pTime->offUTC) | |
695 | { | |
696 | int32_t offUTCHour = pTime->offUTC / 60; | |
697 | int32_t offUTCMinute = pTime->offUTC % 60; | |
698 | char chSign; | |
699 | Assert(pTime->offUTC <= 840 && pTime->offUTC >= -840); | |
700 | if (pTime->offUTC >= 0) | |
701 | chSign = '+'; | |
702 | else | |
703 | { | |
704 | chSign = '-'; | |
705 | offUTCMinute = -offUTCMinute; | |
706 | offUTCHour = -offUTCHour; | |
707 | } | |
708 | cch = RTStrPrintf(psz, cb, | |
709 | "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32%c%02d%02d", | |
710 | pTime->i32Year, pTime->u8Month, pTime->u8MonthDay, | |
711 | pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond, | |
712 | chSign, offUTCHour, offUTCMinute); | |
713 | if ( cch <= 15 | |
714 | || psz[cch - 5] != chSign) | |
715 | return NULL; | |
716 | } | |
717 | else | |
718 | { | |
719 | cch = RTStrPrintf(psz, cb, "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32Z", | |
720 | pTime->i32Year, pTime->u8Month, pTime->u8MonthDay, | |
721 | pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond); | |
722 | if ( cch <= 15 | |
723 | || psz[cch - 1] != 'Z') | |
724 | return NULL; | |
725 | } | |
726 | return psz; | |
727 | } | |
728 | RT_EXPORT_SYMBOL(RTTimeToString); | |
729 | ||
730 | ||
731 | /** | |
732 | * Converts a time spec to a ISO date string. | |
733 | * | |
734 | * @returns psz on success. | |
735 | * @returns NULL on buffer underflow. | |
736 | * @param pTime The time spec. | |
737 | * @param psz Where to store the string. | |
738 | * @param cb The size of the buffer. | |
739 | */ | |
740 | RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb) | |
741 | { | |
742 | RTTIME Time; | |
743 | return RTTimeToString(RTTimeExplode(&Time, pTime), psz, cb); | |
744 | } | |
745 | RT_EXPORT_SYMBOL(RTTimeSpecToString); | |
746 | ||
747 | ||
748 | ||
749 | /** | |
750 | * Attempts to convert an ISO date string to a time structure. | |
751 | * | |
752 | * We're a little forgiving with zero padding, unspecified parts, and leading | |
753 | * and trailing spaces. | |
754 | * | |
755 | * @retval pTime on success, | |
756 | * @retval NULL on failure. | |
757 | * @param pTime Where to store the time on success. | |
758 | * @param pszString The ISO date string to convert. | |
759 | */ | |
760 | RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString) | |
761 | { | |
762 | /* Ignore leading spaces. */ | |
763 | while (RT_C_IS_SPACE(*pszString)) | |
764 | pszString++; | |
765 | ||
766 | /* | |
767 | * Init non date & time parts. | |
768 | */ | |
769 | pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL; | |
770 | pTime->offUTC = 0; | |
771 | ||
772 | /* | |
773 | * The day part. | |
774 | */ | |
775 | ||
776 | /* Year */ | |
777 | int rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year); | |
778 | if (rc != VWRN_TRAILING_CHARS) | |
779 | return NULL; | |
780 | ||
781 | bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year); | |
782 | if (fLeapYear) | |
783 | pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR; | |
784 | ||
785 | if (*pszString++ != '-') | |
786 | return NULL; | |
787 | ||
788 | /* Month of the year. */ | |
789 | rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Month); | |
790 | if (rc != VWRN_TRAILING_CHARS) | |
791 | return NULL; | |
792 | if (pTime->u8Month == 0 || pTime->u8Month > 12) | |
793 | return NULL; | |
794 | if (*pszString++ != '-') | |
795 | return NULL; | |
796 | ||
797 | /* Day of month.*/ | |
798 | rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay); | |
799 | if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS) | |
800 | return NULL; | |
801 | unsigned const cDaysInMonth = fLeapYear | |
802 | ? g_acDaysInMonthsLeap[pTime->u8Month - 1] | |
803 | : g_acDaysInMonths[pTime->u8Month - 1]; | |
804 | if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth) | |
805 | return NULL; | |
806 | ||
807 | /* Calculate year day. */ | |
808 | pTime->u16YearDay = pTime->u8MonthDay - 1 | |
809 | + (fLeapYear | |
810 | ? g_aiDayOfYearLeap[pTime->u8Month - 1] | |
811 | : g_aiDayOfYear[pTime->u8Month - 1]); | |
812 | ||
813 | /* | |
814 | * The time part. | |
815 | */ | |
816 | if (*pszString++ != 'T') | |
817 | return NULL; | |
818 | ||
819 | /* Hour. */ | |
820 | rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour); | |
821 | if (rc != VWRN_TRAILING_CHARS) | |
822 | return NULL; | |
823 | if (pTime->u8Hour > 23) | |
824 | return NULL; | |
825 | if (*pszString++ != ':') | |
826 | return NULL; | |
827 | ||
828 | /* Minute. */ | |
829 | rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute); | |
830 | if (rc != VWRN_TRAILING_CHARS) | |
831 | return NULL; | |
832 | if (pTime->u8Minute > 59) | |
833 | return NULL; | |
834 | if (*pszString++ != ':') | |
835 | return NULL; | |
836 | ||
837 | /* Second. */ | |
838 | rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute); | |
839 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES) | |
840 | return NULL; | |
841 | if (pTime->u8Second > 59) | |
842 | return NULL; | |
843 | ||
844 | /* Nanoseconds is optional and probably non-standard. */ | |
845 | if (*pszString == '.') | |
846 | { | |
847 | rc = RTStrToUInt32Ex(pszString + 1, (char **)&pszString, 10, &pTime->u32Nanosecond); | |
848 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES) | |
849 | return NULL; | |
850 | if (pTime->u32Nanosecond >= 1000000000) | |
851 | return NULL; | |
852 | } | |
853 | else | |
854 | pTime->u32Nanosecond = 0; | |
855 | ||
856 | /* | |
857 | * Time zone. | |
858 | */ | |
859 | if (*pszString == 'Z') | |
860 | { | |
861 | pszString++; | |
862 | pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK; | |
863 | pTime->fFlags |= ~RTTIME_FLAGS_TYPE_UTC; | |
864 | pTime->offUTC = 0; | |
865 | } | |
866 | else if ( *pszString == '+' | |
867 | || *pszString == '-') | |
868 | { | |
869 | rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->offUTC); | |
870 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES) | |
871 | return NULL; | |
872 | } | |
873 | /* else: No time zone given, local with offUTC = 0. */ | |
874 | ||
875 | /* | |
876 | * The rest of the string should be blanks. | |
877 | */ | |
878 | char ch; | |
879 | while ((ch = *pszString++) != '\0') | |
880 | if (!RT_C_IS_BLANK(ch)) | |
881 | return NULL; | |
882 | ||
883 | return pTime; | |
884 | } | |
885 | RT_EXPORT_SYMBOL(RTTimeFromString); | |
886 | ||
887 | ||
888 | /** | |
889 | * Attempts to convert an ISO date string to a time structure. | |
890 | * | |
891 | * We're a little forgiving with zero padding, unspecified parts, and leading | |
892 | * and trailing spaces. | |
893 | * | |
894 | * @retval pTime on success, | |
895 | * @retval NULL on failure. | |
896 | * @param pTime The time spec. | |
897 | * @param pszString The ISO date string to convert. | |
898 | */ | |
899 | RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString) | |
900 | { | |
901 | RTTIME Time; | |
902 | if (RTTimeFromString(&Time, pszString)) | |
903 | return RTTimeImplode(pTime, &Time); | |
904 | return NULL; | |
905 | } | |
906 | RT_EXPORT_SYMBOL(RTTimeSpecFromString); | |
907 |