VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/time/time.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 61.1 KB
Line 
1/* $Id: time.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Time.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_TIME
42#include <iprt/time.h>
43#include "internal/iprt.h"
44
45#include <iprt/assert.h>
46#include <iprt/ctype.h>
47#include <iprt/errcore.h>
48#include <iprt/string.h>
49#include "internal/time.h"
50
51
52/*********************************************************************************************************************************
53* Defined Constants And Macros *
54*********************************************************************************************************************************/
55/** The max year we possibly could implode. */
56#define RTTIME_MAX_YEAR (292 + 1970)
57/** The min year we possibly could implode. */
58#define RTTIME_MIN_YEAR (-293 + 1970)
59
60/** The max day supported by our time representation. (2262-04-11T23-47-16.854775807) */
61#define RTTIME_MAX_DAY (365*292+71 + 101-1)
62/** The min day supported by our time representation. (1677-09-21T00-12-43.145224192) */
63#define RTTIME_MIN_DAY (365*-293-70 + 264-1)
64
65/** The max nano second into the max day. (2262-04-11T23-47-16.854775807) */
66#define RTTIME_MAX_DAY_NANO ( INT64_C(1000000000) * (23*3600 + 47*60 + 16) + 854775807 )
67/** The min nano second into the min day. (1677-09-21T00-12-43.145224192) */
68#define RTTIME_MIN_DAY_NANO ( INT64_C(1000000000) * (00*3600 + 12*60 + 43) + 145224192 )
69
70/**
71 * Asserts that a_pTime is normalized.
72 */
73#define RTTIME_ASSERT_NORMALIZED(a_pTime) \
74 do \
75 { \
76 Assert(RT_ABS((a_pTime)->offUTC) <= 840); \
77 Assert((a_pTime)->u32Nanosecond < 1000000000); \
78 Assert((a_pTime)->u8Second < 60); \
79 Assert((a_pTime)->u8Minute < 60); \
80 Assert((a_pTime)->u8Hour < 24); \
81 Assert((a_pTime)->u8Month >= 1 && (a_pTime)->u8Month <= 12); \
82 Assert((a_pTime)->u8WeekDay < 7); \
83 Assert((a_pTime)->u16YearDay >= 1); \
84 Assert((a_pTime)->u16YearDay <= (rtTimeIsLeapYear((a_pTime)->i32Year) ? 366 : 365)); \
85 Assert((a_pTime)->u8MonthDay >= 1 && (a_pTime)->u8MonthDay <= 31); \
86 } while (0)
87
88
89/*********************************************************************************************************************************
90* Global Variables *
91*********************************************************************************************************************************/
92/**
93 * Days per month in a common year.
94 */
95static const uint8_t g_acDaysInMonths[12] =
96{
97 /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */
98 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
99};
100
101/**
102 * Days per month in a leap year.
103 */
104static const uint8_t g_acDaysInMonthsLeap[12] =
105{
106 /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */
107 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
108};
109
110/**
111 * The day of year for each month in a common year.
112 */
113static const uint16_t g_aiDayOfYear[12 + 1] =
114{
115 1, /* Jan */
116 1+31, /* Feb */
117 1+31+28, /* Mar */
118 1+31+28+31, /* Apr */
119 1+31+28+31+30, /* May */
120 1+31+28+31+30+31, /* Jun */
121 1+31+28+31+30+31+30, /* Jul */
122 1+31+28+31+30+31+30+31, /* Aug */
123 1+31+28+31+30+31+30+31+31, /* Sep */
124 1+31+28+31+30+31+30+31+31+30, /* Oct */
125 1+31+28+31+30+31+30+31+31+30+31, /* Nov */
126 1+31+28+31+30+31+30+31+31+30+31+30, /* Dec */
127 1+31+28+31+30+31+30+31+31+30+31+30+31
128};
129
130/**
131 * The day of year for each month in a leap year.
132 */
133static const uint16_t g_aiDayOfYearLeap[12 + 1] =
134{
135 1, /* Jan */
136 1+31, /* Feb */
137 1+31+29, /* Mar */
138 1+31+29+31, /* Apr */
139 1+31+29+31+30, /* May */
140 1+31+29+31+30+31, /* Jun */
141 1+31+29+31+30+31+30, /* Jul */
142 1+31+29+31+30+31+30+31, /* Aug */
143 1+31+29+31+30+31+30+31+31, /* Sep */
144 1+31+29+31+30+31+30+31+31+30, /* Oct */
145 1+31+29+31+30+31+30+31+31+30+31, /* Nov */
146 1+31+29+31+30+31+30+31+31+30+31+30, /* Dec */
147 1+31+29+31+30+31+30+31+31+30+31+30+31
148};
149
150/** The index of 1970 in g_aoffYear */
151#define OFF_YEAR_IDX_EPOCH 300
152/** The year of the first index. */
153#define OFF_YEAR_IDX_0_YEAR 1670
154
155/**
156 * The number of days the 1st of January a year is offseted from 1970-01-01.
157 */
158static const int32_t g_aoffYear[] =
159{
160/*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,
161/*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,
162/*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,
163/*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,
164/*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,
165/*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,
166/*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,
167/*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,
168/*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,
169/*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,
170/*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,
171/*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,
172/*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,
173/*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,
174/*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,
175/*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,
176/*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,
177/*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,
178/*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,
179/*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,
180/*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,
181/*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,
182/*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,
183/*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,
184/*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,
185/*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,
186/*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 ,
187/*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 ,
188/*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 ,
189/*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 ,
190/*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 ,
191/*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 ,
192/*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 ,
193/*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 ,
194/*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 ,
195/*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 ,
196/*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 ,
197/*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 ,
198/*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 ,
199/*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 ,
200/*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 ,
201/*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 ,
202/*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 ,
203/*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 ,
204/*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 ,
205/*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 ,
206/*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 ,
207/*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 ,
208/*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 ,
209/*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 ,
210/*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 ,
211/*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 ,
212/*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 ,
213/*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 ,
214/*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 ,
215/*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 ,
216/*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 ,
217/*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 ,
218/*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 ,
219/*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
220};
221
222/* generator code:
223#include <stdio.h>
224bool isLeapYear(int iYear)
225{
226 return iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0);
227}
228void printYear(int iYear, int iLeap)
229{
230 if (!(iYear % 10))
231 printf("\n/" "*%d:*" "/", iYear + 1970);
232 printf(" 365*%4d+%-3d,", iYear, iLeap);
233}
234int main()
235{
236 int iYear = 0;
237 int iLeap = 0;
238 while (iYear > -300)
239 iLeap -= isLeapYear(1970 + --iYear);
240 while (iYear < 300)
241 {
242 printYear(iYear, iLeap);
243 iLeap += isLeapYear(1970 + iYear++);
244 }
245 printf("\n");
246 return 0;
247}
248*/
249
250/** RFC-1123 week day names. */
251static const char * const g_apszWeekDays[7] =
252{
253 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
254};
255/** RFC-1123 month of the year names. */
256static const char * const g_apszMonths[1+12] =
257{
258 "000", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
259};
260
261
262/**
263 * Checks if a year is a leap year or not.
264 *
265 * @returns true if it's a leap year.
266 * @returns false if it's a common year.
267 * @param i32Year The year in question.
268 */
269DECLINLINE(bool) rtTimeIsLeapYear(int32_t i32Year)
270{
271 return i32Year % 4 == 0
272 && ( i32Year % 100 != 0
273 || i32Year % 400 == 0);
274}
275
276
277/**
278 * Checks if a year is a leap year or not.
279 *
280 * @returns true if it's a leap year.
281 * @returns false if it's a common year.
282 * @param i32Year The year in question.
283 */
284RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year)
285{
286 return rtTimeIsLeapYear(i32Year);
287}
288RT_EXPORT_SYMBOL(RTTimeIsLeapYear);
289
290
291/**
292 * Explodes a time spec (UTC).
293 *
294 * @returns pTime.
295 * @param pTime Where to store the exploded time.
296 * @param pTimeSpec The time spec to exploded.
297 */
298RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
299{
300 int64_t i64Div;
301 int32_t i32Div;
302 int32_t i32Rem;
303 unsigned iYear;
304 const uint16_t *paiDayOfYear;
305 int iMonth;
306
307 AssertPtr(pTime);
308 AssertPtr(pTimeSpec);
309
310 /*
311 * The simple stuff first.
312 */
313 pTime->fFlags = RTTIME_FLAGS_TYPE_UTC;
314 i64Div = pTimeSpec->i64NanosecondsRelativeToUnixEpoch;
315 i32Rem = (int32_t)(i64Div % 1000000000);
316 i64Div /= 1000000000;
317 if (i32Rem < 0)
318 {
319 i32Rem += 1000000000;
320 i64Div--;
321 }
322 pTime->u32Nanosecond = i32Rem;
323
324 /* second */
325 i32Rem = (int32_t)(i64Div % 60);
326 i64Div /= 60;
327 if (i32Rem < 0)
328 {
329 i32Rem += 60;
330 i64Div--;
331 }
332 pTime->u8Second = i32Rem;
333
334 /* minute */
335 i32Div = (int32_t)i64Div; /* 60,000,000,000 > 33bit, so 31bit suffices. */
336 i32Rem = i32Div % 60;
337 i32Div /= 60;
338 if (i32Rem < 0)
339 {
340 i32Rem += 60;
341 i32Div--;
342 }
343 pTime->u8Minute = i32Rem;
344
345 /* hour */
346 i32Rem = i32Div % 24;
347 i32Div /= 24; /* days relative to 1970-01-01 */
348 if (i32Rem < 0)
349 {
350 i32Rem += 24;
351 i32Div--;
352 }
353 pTime->u8Hour = i32Rem;
354
355 /* weekday - 1970-01-01 was a Thursday (3) */
356 pTime->u8WeekDay = ((int)(i32Div % 7) + 3 + 7) % 7;
357
358 /*
359 * We've now got a number of days relative to 1970-01-01.
360 * To get the correct year number we have to mess with leap years. Fortunately,
361 * the representation we've got only supports a few hundred years, so we can
362 * generate a table and perform a simple two way search from the modulus 365 derived.
363 */
364 iYear = OFF_YEAR_IDX_EPOCH + i32Div / 365;
365 while (g_aoffYear[iYear + 1] <= i32Div)
366 iYear++;
367 while (g_aoffYear[iYear] > i32Div)
368 iYear--;
369 pTime->i32Year = iYear + OFF_YEAR_IDX_0_YEAR;
370 i32Div -= g_aoffYear[iYear];
371 pTime->u16YearDay = i32Div + 1;
372
373 /*
374 * Figuring out the month is done in a manner similar to the year, only here we
375 * ensure that the index is matching or too small.
376 */
377 if (rtTimeIsLeapYear(pTime->i32Year))
378 {
379 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
380 paiDayOfYear = &g_aiDayOfYearLeap[0];
381 }
382 else
383 {
384 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
385 paiDayOfYear = &g_aiDayOfYear[0];
386 }
387 iMonth = i32Div / 32;
388 i32Div++;
389 while (paiDayOfYear[iMonth + 1] <= i32Div)
390 iMonth++;
391 pTime->u8Month = iMonth + 1;
392 i32Div -= paiDayOfYear[iMonth];
393 pTime->u8MonthDay = i32Div + 1;
394
395 /* This is for UTC timespecs, so, no offset. */
396 pTime->offUTC = 0;
397
398 return pTime;
399}
400RT_EXPORT_SYMBOL(RTTimeExplode);
401
402
403/**
404 * Implodes exploded time to a time spec (UTC).
405 *
406 * @returns pTime on success.
407 * @returns NULL if the pTime data is invalid.
408 * @param pTimeSpec Where to store the imploded UTC time.
409 * If pTime specifies a time which outside the range, maximum or
410 * minimum values will be returned.
411 * @param pTime Pointer to the exploded time to implode.
412 * The fields u8Month, u8WeekDay and u8MonthDay are not used,
413 * and all the other fields are expected to be within their
414 * bounds. Use RTTimeNormalize() or RTTimeLocalNormalize() to
415 * calculate u16YearDay and normalize the ranges of the fields.
416 */
417RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime)
418{
419 int32_t i32Days;
420 uint32_t u32Secs;
421 int64_t i64Nanos;
422
423 /*
424 * Validate input.
425 */
426 AssertPtrReturn(pTimeSpec, NULL);
427 AssertPtrReturn(pTime, NULL);
428 AssertReturn(pTime->u32Nanosecond < 1000000000, NULL);
429 AssertReturn(pTime->u8Second < 60, NULL);
430 AssertReturn(pTime->u8Minute < 60, NULL);
431 AssertReturn(pTime->u8Hour < 24, NULL);
432 AssertReturn(pTime->u16YearDay >= 1, NULL);
433 AssertReturn(pTime->u16YearDay <= (rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365), NULL);
434 AssertMsgReturn(pTime->i32Year <= RTTIME_MAX_YEAR && pTime->i32Year >= RTTIME_MIN_YEAR, ("%RI32\n", pTime->i32Year), NULL);
435 Assert(pTime->offUTC >= -840 && pTime->offUTC <= 840);
436
437 /*
438 * Do the conversion to nanoseconds.
439 */
440 i32Days = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
441 + pTime->u16YearDay - 1;
442 AssertMsgReturn(i32Days <= RTTIME_MAX_DAY && i32Days >= RTTIME_MIN_DAY, ("%RI32\n", i32Days), NULL);
443
444 u32Secs = pTime->u8Second
445 + pTime->u8Minute * 60
446 + pTime->u8Hour * 3600;
447 i64Nanos = (uint64_t)pTime->u32Nanosecond
448 + u32Secs * UINT64_C(1000000000);
449 AssertMsgReturn(i32Days != RTTIME_MAX_DAY || i64Nanos <= RTTIME_MAX_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
450 AssertMsgReturn(i32Days != RTTIME_MIN_DAY || i64Nanos >= RTTIME_MIN_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
451
452 i64Nanos += i32Days * UINT64_C(86400000000000);
453 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL)
454 i64Nanos -= pTime->offUTC * RT_NS_1MIN;
455
456 pTimeSpec->i64NanosecondsRelativeToUnixEpoch = i64Nanos;
457 return pTimeSpec;
458}
459RT_EXPORT_SYMBOL(RTTimeImplode);
460
461
462/**
463 * Internal worker for RTTimeNormalize and RTTimeLocalNormalize.
464 */
465static PRTTIME rtTimeNormalizeInternal(PRTTIME pTime)
466{
467 unsigned uSecond;
468 unsigned uMinute;
469 unsigned uHour;
470 bool fLeapYear;
471
472 /*
473 * Fix the YearDay and Month/MonthDay.
474 */
475 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
476 if (!pTime->u16YearDay)
477 {
478 /*
479 * The Month+MonthDay must present, overflow adjust them and calc the year day.
480 */
481 AssertMsgReturn( pTime->u8Month
482 && pTime->u8MonthDay,
483 ("date=%d-%d-%d\n", pTime->i32Year, pTime->u8Month, pTime->u8MonthDay),
484 NULL);
485 while (pTime->u8Month > 12)
486 {
487 pTime->u8Month -= 12;
488 pTime->i32Year++;
489 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
490 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
491 }
492
493 for (;;)
494 {
495 unsigned cDaysInMonth = fLeapYear
496 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
497 : g_acDaysInMonths[pTime->u8Month - 1];
498 if (pTime->u8MonthDay <= cDaysInMonth)
499 break;
500 pTime->u8MonthDay -= cDaysInMonth;
501 if (pTime->u8Month != 12)
502 pTime->u8Month++;
503 else
504 {
505 pTime->u8Month = 1;
506 pTime->i32Year++;
507 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
508 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
509 }
510 }
511
512 pTime->u16YearDay = pTime->u8MonthDay - 1
513 + (fLeapYear
514 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
515 : g_aiDayOfYear[pTime->u8Month - 1]);
516 }
517 else
518 {
519 /*
520 * Are both YearDay and Month/MonthDay valid?
521 * Check that they don't overflow and match, if not use YearDay (simpler).
522 */
523 bool fRecalc = true;
524 if ( pTime->u8Month
525 && pTime->u8MonthDay)
526 {
527 do
528 {
529 uint16_t u16YearDay;
530
531 /* If you change one, zero the other to make clear what you mean. */
532 AssertBreak(pTime->u8Month <= 12);
533 AssertBreak(pTime->u8MonthDay <= (fLeapYear
534 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
535 : g_acDaysInMonths[pTime->u8Month - 1]));
536 u16YearDay = pTime->u8MonthDay - 1
537 + (fLeapYear
538 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
539 : g_aiDayOfYear[pTime->u8Month - 1]);
540 AssertBreak(u16YearDay == pTime->u16YearDay);
541 fRecalc = false;
542 } while (0);
543 }
544 if (fRecalc)
545 {
546 const uint16_t *paiDayOfYear;
547
548 /* overflow adjust YearDay */
549 while (pTime->u16YearDay > (fLeapYear ? 366 : 365))
550 {
551 pTime->u16YearDay -= fLeapYear ? 366 : 365;
552 pTime->i32Year++;
553 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
554 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
555 }
556
557 /* calc Month and MonthDay */
558 paiDayOfYear = fLeapYear
559 ? &g_aiDayOfYearLeap[0]
560 : &g_aiDayOfYear[0];
561 pTime->u8Month = 1;
562 while (pTime->u16YearDay >= paiDayOfYear[pTime->u8Month])
563 pTime->u8Month++;
564 Assert(pTime->u8Month >= 1 && pTime->u8Month <= 12);
565 pTime->u8MonthDay = pTime->u16YearDay - paiDayOfYear[pTime->u8Month - 1] + 1;
566 }
567 }
568
569 /*
570 * Fixup time overflows.
571 * Use unsigned int values internally to avoid overflows.
572 */
573 uSecond = pTime->u8Second;
574 uMinute = pTime->u8Minute;
575 uHour = pTime->u8Hour;
576
577 while (pTime->u32Nanosecond >= 1000000000)
578 {
579 pTime->u32Nanosecond -= 1000000000;
580 uSecond++;
581 }
582
583 while (uSecond >= 60)
584 {
585 uSecond -= 60;
586 uMinute++;
587 }
588
589 while (uMinute >= 60)
590 {
591 uMinute -= 60;
592 uHour++;
593 }
594
595 while (uHour >= 24)
596 {
597 uHour -= 24;
598
599 /* This is really a RTTimeIncDay kind of thing... */
600 if (pTime->u16YearDay + 1 != (fLeapYear ? g_aiDayOfYearLeap[pTime->u8Month] : g_aiDayOfYear[pTime->u8Month]))
601 {
602 pTime->u16YearDay++;
603 pTime->u8MonthDay++;
604 }
605 else if (pTime->u8Month != 12)
606 {
607 pTime->u16YearDay++;
608 pTime->u8Month++;
609 pTime->u8MonthDay = 1;
610 }
611 else
612 {
613 pTime->i32Year++;
614 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
615 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
616 pTime->u16YearDay = 1;
617 pTime->u8Month = 1;
618 pTime->u8MonthDay = 1;
619 }
620 }
621
622 pTime->u8Second = uSecond;
623 pTime->u8Minute = uMinute;
624 pTime->u8Hour = uHour;
625
626 /*
627 * Correct the leap year flag.
628 * Assert if it's wrong, but ignore if unset.
629 */
630 if (fLeapYear)
631 {
632 Assert(!(pTime->fFlags & RTTIME_FLAGS_COMMON_YEAR));
633 pTime->fFlags &= ~RTTIME_FLAGS_COMMON_YEAR;
634 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
635 }
636 else
637 {
638 Assert(!(pTime->fFlags & RTTIME_FLAGS_LEAP_YEAR));
639 pTime->fFlags &= ~RTTIME_FLAGS_LEAP_YEAR;
640 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
641 }
642
643
644 /*
645 * Calc week day.
646 *
647 * 1970-01-01 was a Thursday (3), so find the number of days relative to
648 * that point. We use the table when possible and a slow+stupid+brute-force
649 * algorithm for points outside it. Feel free to optimize the latter by
650 * using some clever formula.
651 */
652 if ( pTime->i32Year >= OFF_YEAR_IDX_0_YEAR
653 && pTime->i32Year < OFF_YEAR_IDX_0_YEAR + (int32_t)RT_ELEMENTS(g_aoffYear))
654 {
655 int32_t offDays = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
656 + pTime->u16YearDay -1;
657 pTime->u8WeekDay = ((offDays % 7) + 3 + 7) % 7;
658 }
659 else
660 {
661 int32_t i32Year = pTime->i32Year;
662 if (i32Year >= 1970)
663 {
664 uint64_t offDays = pTime->u16YearDay - 1;
665 while (--i32Year >= 1970)
666 offDays += rtTimeIsLeapYear(i32Year) ? 366 : 365;
667 pTime->u8WeekDay = (uint8_t)((offDays + 3) % 7);
668 }
669 else
670 {
671 int64_t offDays = (fLeapYear ? -366 - 1 : -365 - 1) + pTime->u16YearDay;
672 while (++i32Year < 1970)
673 offDays -= rtTimeIsLeapYear(i32Year) ? 366 : 365;
674 pTime->u8WeekDay = ((int)(offDays % 7) + 3 + 7) % 7;
675 }
676 }
677 return pTime;
678}
679
680
681/**
682 * Normalizes the fields of a time structure.
683 *
684 * It is possible to calculate year-day from month/day and vice
685 * versa. If you adjust any of these, make sure to zero the
686 * other so you make it clear which of the fields to use. If
687 * it's ambiguous, the year-day field is used (and you get
688 * assertions in debug builds).
689 *
690 * All the time fields and the year-day or month/day fields will
691 * be adjusted for overflows. (Since all fields are unsigned, there
692 * is no underflows.) It is possible to exploit this for simple
693 * date math, though the recommended way of doing that to implode
694 * the time into a timespec and do the math on that.
695 *
696 * @returns pTime on success.
697 * @returns NULL if the data is invalid.
698 *
699 * @param pTime The time structure to normalize.
700 *
701 * @remarks This function doesn't work with local time, only with UTC time.
702 */
703RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime)
704{
705 /*
706 * Validate that we've got the minimum of stuff handy.
707 */
708 AssertPtrReturn(pTime, NULL);
709 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
710 AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_LOCAL, ("Use RTTimeLocalNormalize!\n"), NULL);
711 AssertMsgReturn(pTime->offUTC == 0, ("%d; Use RTTimeLocalNormalize!\n", pTime->offUTC), NULL);
712
713 pTime = rtTimeNormalizeInternal(pTime);
714 if (pTime)
715 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
716 return pTime;
717}
718RT_EXPORT_SYMBOL(RTTimeNormalize);
719
720
721/**
722 * Normalizes the fields of a time structure, assuming local time.
723 *
724 * It is possible to calculate year-day from month/day and vice
725 * versa. If you adjust any of these, make sure to zero the
726 * other so you make it clear which of the fields to use. If
727 * it's ambiguous, the year-day field is used (and you get
728 * assertions in debug builds).
729 *
730 * All the time fields and the year-day or month/day fields will
731 * be adjusted for overflows. (Since all fields are unsigned, there
732 * is no underflows.) It is possible to exploit this for simple
733 * date math, though the recommended way of doing that to implode
734 * the time into a timespec and do the math on that.
735 *
736 * @returns pTime on success.
737 * @returns NULL if the data is invalid.
738 *
739 * @param pTime The time structure to normalize.
740 *
741 * @remarks This function doesn't work with UTC time, only with local time.
742 */
743RTDECL(PRTTIME) RTTimeLocalNormalize(PRTTIME pTime)
744{
745 /*
746 * Validate that we've got the minimum of stuff handy.
747 */
748 AssertPtrReturn(pTime, NULL);
749 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
750 AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC, ("Use RTTimeNormalize!\n"), NULL);
751
752 pTime = rtTimeNormalizeInternal(pTime);
753 if (pTime)
754 pTime->fFlags |= RTTIME_FLAGS_TYPE_LOCAL;
755 return pTime;
756}
757RT_EXPORT_SYMBOL(RTTimeLocalNormalize);
758
759
760/**
761 * Converts a time spec to a ISO date string.
762 *
763 * @returns psz on success.
764 * @returns NULL on buffer underflow.
765 * @param pTime The time. Caller should've normalized this.
766 * @param psz Where to store the string.
767 * @param cb The size of the buffer.
768 */
769RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb)
770{
771 size_t cch;
772
773 /* (Default to UTC if not specified) */
774 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
775 && pTime->offUTC)
776 {
777 int32_t offUTC = pTime->offUTC;
778 Assert(offUTC <= 840 && offUTC >= -840);
779 char chSign;
780 if (offUTC >= 0)
781 chSign = '+';
782 else
783 {
784 chSign = '-';
785 offUTC = -offUTC;
786 }
787 uint32_t offUTCHour = (uint32_t)offUTC / 60;
788 uint32_t offUTCMinute = (uint32_t)offUTC % 60;
789 cch = RTStrPrintf(psz, cb,
790 "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32%c%02d%:02d",
791 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
792 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond,
793 chSign, offUTCHour, offUTCMinute);
794 if ( cch <= 15
795 || psz[cch - 6] != chSign)
796 return NULL;
797 }
798 else
799 {
800 cch = RTStrPrintf(psz, cb, "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32Z",
801 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
802 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond);
803 if ( cch <= 15
804 || psz[cch - 1] != 'Z')
805 return NULL;
806 }
807 return psz;
808}
809RT_EXPORT_SYMBOL(RTTimeToString);
810
811
812/**
813 * Converts a time spec to a ISO date string, extended version.
814 *
815 * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
816 * (negative) or VERR_OUT_OF_RANGE (negative) on failure.
817 * @param pTime The time. Caller should've normalized this.
818 * @param psz Where to store the string.
819 * @param cb The size of the buffer.
820 * @param cFractionDigits Number of digits in the fraction. Max is 9.
821 */
822RTDECL(ssize_t) RTTimeToStringEx(PCRTTIME pTime, char *psz, size_t cb, unsigned cFractionDigits)
823{
824 size_t cch;
825
826 /* Format the fraction. */
827 char szFraction[16];
828 if (!cFractionDigits)
829 szFraction[0] = '\0';
830 else
831 {
832 AssertReturn(cFractionDigits <= 9, VERR_OUT_OF_RANGE);
833 Assert(pTime->u32Nanosecond <= 999999999);
834 RTStrPrintf(szFraction, sizeof(szFraction), ".%09RU32", pTime->u32Nanosecond);
835 szFraction[cFractionDigits + 1] = '\0';
836 }
837
838 /* (Default to UTC if not specified) */
839 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
840 && pTime->offUTC)
841 {
842 int32_t offUTC = pTime->offUTC;
843 Assert(offUTC <= 840 && offUTC >= -840);
844 char chSign;
845 if (offUTC >= 0)
846 chSign = '+';
847 else
848 {
849 chSign = '-';
850 offUTC = -offUTC;
851 }
852 uint32_t offUTCHour = (uint32_t)offUTC / 60;
853 uint32_t offUTCMinute = (uint32_t)offUTC % 60;
854
855 /* Examples: 2018-09-07T16:12:00+02:00 2018-09-07T16:12:00.123456789+02:00 */
856 cch = RTStrPrintf(psz, cb,
857 "%04RI32-%02u-%02uT%02u:%02u:%02u%s%c%02d%:02d",
858 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
859 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, szFraction,
860 chSign, offUTCHour, offUTCMinute);
861 if ( cch >= 24
862 && psz[cch - 6] == chSign)
863 return cch;
864 }
865 else
866 {
867 /* Examples: 2018-09-07T16:12:00Z 2018-09-07T16:12:00.123456789Z */
868 cch = RTStrPrintf(psz, cb, "%04RI32-%02u-%02uT%02u:%02u:%02u%sZ",
869 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
870 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, szFraction);
871 if ( cch >= 19
872 && psz[cch - 1] == 'Z')
873 return cch;
874 }
875 return VERR_BUFFER_OVERFLOW;
876}
877RT_EXPORT_SYMBOL(RTTimeToStringEx);
878
879
880/**
881 * Converts a time spec to a ISO date string.
882 *
883 * @returns psz on success.
884 * @returns NULL on buffer underflow.
885 * @param pTime The time spec.
886 * @param psz Where to store the string.
887 * @param cb The size of the buffer.
888 */
889RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb)
890{
891 RTTIME Time;
892 return RTTimeToString(RTTimeExplode(&Time, pTime), psz, cb);
893}
894RT_EXPORT_SYMBOL(RTTimeSpecToString);
895
896
897
898/**
899 * Attempts to convert an ISO date string to a time structure.
900 *
901 * We're a little forgiving with zero padding, unspecified parts, and leading
902 * and trailing spaces.
903 *
904 * @retval pTime on success,
905 * @retval NULL on failure.
906 * @param pTime Where to store the time on success.
907 * @param pszString The ISO date string to convert.
908 */
909RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString)
910{
911 /* Ignore leading spaces. */
912 while (RT_C_IS_SPACE(*pszString))
913 pszString++;
914
915 /*
916 * Init non date & time parts.
917 */
918 pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL;
919 pTime->offUTC = 0;
920
921 /*
922 * The date part.
923 */
924
925 /* Year */
926 int rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year);
927 if (rc != VWRN_TRAILING_CHARS)
928 return NULL;
929
930 bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
931 if (fLeapYear)
932 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
933
934 if (*pszString++ != '-')
935 return NULL;
936
937 /* Month of the year. */
938 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Month);
939 if (rc != VWRN_TRAILING_CHARS)
940 return NULL;
941 if (pTime->u8Month == 0 || pTime->u8Month > 12)
942 return NULL;
943 if (*pszString++ != '-')
944 return NULL;
945
946 /* Day of month.*/
947 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay);
948 if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS)
949 return NULL;
950 unsigned const cDaysInMonth = fLeapYear
951 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
952 : g_acDaysInMonths[pTime->u8Month - 1];
953 if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth)
954 return NULL;
955
956 /* Calculate year day. */
957 pTime->u16YearDay = pTime->u8MonthDay - 1
958 + (fLeapYear
959 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
960 : g_aiDayOfYear[pTime->u8Month - 1]);
961
962 pTime->u8WeekDay = UINT8_MAX; /* later */
963
964 /*
965 * The time part.
966 */
967 if (*pszString++ != 'T')
968 return NULL;
969
970 /* Hour. */
971 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour);
972 if (rc != VWRN_TRAILING_CHARS)
973 return NULL;
974 if (pTime->u8Hour > 23)
975 return NULL;
976 if (*pszString++ != ':')
977 return NULL;
978
979 /* Minute. */
980 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
981 if (rc != VWRN_TRAILING_CHARS)
982 return NULL;
983 if (pTime->u8Minute > 59)
984 return NULL;
985 if (*pszString++ != ':')
986 return NULL;
987
988 /* Second. */
989 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Second);
990 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
991 return NULL;
992 if (pTime->u8Second > 59)
993 return NULL;
994
995 /* We generally put a 9 digit fraction here, but it's entirely optional. */
996 if (*pszString == '.')
997 {
998 const char * const pszStart = ++pszString;
999 rc = RTStrToUInt32Ex(pszString, (char **)&pszString, 10, &pTime->u32Nanosecond);
1000 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
1001 return NULL;
1002 if (pTime->u32Nanosecond >= 1000000000)
1003 return NULL;
1004 switch (pszString - pszStart)
1005 {
1006 case 1: pTime->u32Nanosecond *= 100000000; break;
1007 case 2: pTime->u32Nanosecond *= 10000000; break;
1008 case 3: pTime->u32Nanosecond *= 1000000; break;
1009 case 4: pTime->u32Nanosecond *= 100000; break;
1010 case 5: pTime->u32Nanosecond *= 10000; break;
1011 case 6: pTime->u32Nanosecond *= 1000; break;
1012 case 7: pTime->u32Nanosecond *= 100; break;
1013 case 8: pTime->u32Nanosecond *= 10; break;
1014 case 9: break;
1015 default:
1016 return NULL;
1017 }
1018 if (pTime->u32Nanosecond >= 1000000000)
1019 return NULL;
1020 }
1021 else
1022 pTime->u32Nanosecond = 0;
1023
1024 /*
1025 * Time zone.
1026 */
1027 if (*pszString == 'Z')
1028 {
1029 pszString++;
1030 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1031 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1032 pTime->offUTC = 0;
1033 }
1034 else if ( *pszString == '+'
1035 || *pszString == '-')
1036 {
1037 int8_t cUtcHours = 0;
1038 rc = RTStrToInt8Ex(pszString, (char **)&pszString, 10, &cUtcHours);
1039 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
1040 return NULL;
1041 uint8_t cUtcMin = 0;
1042 if (*pszString == ':')
1043 {
1044 rc = RTStrToUInt8Ex(pszString + 1, (char **)&pszString, 10, &cUtcMin);
1045 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
1046 return NULL;
1047 }
1048 else if (*pszString && !RT_C_IS_BLANK(*pszString))
1049 return NULL;
1050 if (cUtcHours >= 0)
1051 pTime->offUTC = cUtcHours * 60 + cUtcMin;
1052 else
1053 pTime->offUTC = cUtcHours * 60 - cUtcMin;
1054 if (RT_ABS(pTime->offUTC) > 840)
1055 return NULL;
1056 }
1057 /* else: No time zone given, local with offUTC = 0. */
1058
1059 /*
1060 * The rest of the string should be blanks.
1061 */
1062 char ch;
1063 while ((ch = *pszString++) != '\0')
1064 if (!RT_C_IS_BLANK(ch))
1065 return NULL;
1066
1067 /* Calc week day. */
1068 rtTimeNormalizeInternal(pTime);
1069 return pTime;
1070}
1071RT_EXPORT_SYMBOL(RTTimeFromString);
1072
1073
1074/**
1075 * Attempts to convert an ISO date string to a time structure.
1076 *
1077 * We're a little forgiving with zero padding, unspecified parts, and leading
1078 * and trailing spaces.
1079 *
1080 * @retval pTime on success,
1081 * @retval NULL on failure.
1082 * @param pTime The time spec.
1083 * @param pszString The ISO date string to convert.
1084 */
1085RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString)
1086{
1087 RTTIME Time;
1088 if (RTTimeFromString(&Time, pszString))
1089 return RTTimeImplode(pTime, &Time);
1090 return NULL;
1091}
1092RT_EXPORT_SYMBOL(RTTimeSpecFromString);
1093
1094
1095/**
1096 * Formats the given time on a RTC-2822 compliant format.
1097 *
1098 * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
1099 * (negative) on failure.
1100 * @param pTime The time. Caller should've normalized this.
1101 * @param psz Where to store the string.
1102 * @param cb The size of the buffer.
1103 */
1104RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb, uint32_t fFlags)
1105{
1106 Assert(pTime->u8Month > 0 && pTime->u8Month <= 12);
1107 Assert(pTime->u8WeekDay < 7);
1108 Assert(!(fFlags & ~RTTIME_RFC2822_F_GMT));
1109
1110 /* (Default to UTC if not specified) */
1111 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
1112 && pTime->offUTC)
1113 {
1114 Assert(!(fFlags & RTTIME_RFC2822_F_GMT) /* don't call with local time. duh! */ );
1115
1116 /* Calc the UTC offset part. */
1117 int32_t offUtc = pTime->offUTC;
1118 Assert(offUtc <= 840 && offUtc >= -840);
1119 char chSign;
1120 if (offUtc >= 0)
1121 chSign = '+';
1122 else
1123 {
1124 chSign = '-';
1125 offUtc = -offUtc;
1126 }
1127 uint32_t offUtcHour = (uint32_t)offUtc / 60;
1128 uint32_t offUtcMinute = (uint32_t)offUtc % 60;
1129
1130 /* Example: "Mon, 31 Aug 2018 00:00:00 +0200" */
1131 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u %c%02u%02u", g_apszWeekDays[pTime->u8WeekDay],
1132 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1133 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, chSign, offUtcHour, offUtcMinute);
1134 if ( cch >= 27
1135 && psz[cch - 5] == chSign)
1136 return cch;
1137 }
1138 else if (fFlags & RTTIME_RFC2822_F_GMT)
1139 {
1140 /* Example: "Mon, 1 Jan 1971 23:55:59 GMT" */
1141 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u GMT", g_apszWeekDays[pTime->u8WeekDay],
1142 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1143 pTime->u8Hour, pTime->u8Minute, pTime->u8Second);
1144 if ( cch >= 27
1145 && psz[cch - 1] == 'T')
1146 return cch;
1147 }
1148 else
1149 {
1150 /* Example: "Mon, 1 Jan 1971 00:00:00 -0000" */
1151 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u -0000", g_apszWeekDays[pTime->u8WeekDay],
1152 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1153 pTime->u8Hour, pTime->u8Minute, pTime->u8Second);
1154 if ( cch >= 27
1155 && psz[cch - 5] == '-')
1156 return cch;
1157 }
1158 return VERR_BUFFER_OVERFLOW;
1159}
1160RT_EXPORT_SYMBOL(RTTimeToRfc2822);
1161
1162
1163/**
1164 * Attempts to convert an RFC-2822 date string to a time structure.
1165 *
1166 * We're a little forgiving with zero padding, unspecified parts, and leading
1167 * and trailing spaces.
1168 *
1169 * @retval pTime on success,
1170 * @retval NULL on failure.
1171 * @param pTime Where to store the time on success.
1172 * @param pszString The ISO date string to convert.
1173 */
1174RTDECL(PRTTIME) RTTimeFromRfc2822(PRTTIME pTime, const char *pszString)
1175{
1176 /*
1177 * Fri, 31 Aug 2018 00:00:00 +0200
1178 * Mon, 3 Sep 2018 00:00:00 GMT
1179 * Mon, 3 Sep 2018 00:00:00 -0000
1180 * 3 Sep 2018 00:00:00 -0000 (?)
1181 * 3 Sep 2018 00:00:00 GMT (?)
1182 *
1183 */
1184
1185 /* Ignore leading spaces. */
1186 while (RT_C_IS_SPACE(*pszString))
1187 pszString++;
1188
1189 /*
1190 * Init non date & time parts.
1191 */
1192 pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL;
1193 pTime->offUTC = 0;
1194
1195 /*
1196 * The date part.
1197 */
1198
1199 /* Optional day of week: */
1200 if (RT_C_IS_ALPHA(pszString[0]) && pszString[1] != '\0')
1201 {
1202 uint32_t uWeekDay = RT_MAKE_U32_FROM_U8(RT_C_TO_LOWER(pszString[0]), RT_C_TO_LOWER(pszString[1]),
1203 RT_C_TO_LOWER(pszString[2]), 0);
1204 if ( uWeekDay == RT_MAKE_U32_FROM_U8('m', 'o', 'n', 0)) pTime->u8WeekDay = 0;
1205 else if (uWeekDay == RT_MAKE_U32_FROM_U8('t', 'u', 'e', 0)) pTime->u8WeekDay = 1;
1206 else if (uWeekDay == RT_MAKE_U32_FROM_U8('w', 'e', 'd', 0)) pTime->u8WeekDay = 2;
1207 else if (uWeekDay == RT_MAKE_U32_FROM_U8('t', 'h', 'u', 0)) pTime->u8WeekDay = 3;
1208 else if (uWeekDay == RT_MAKE_U32_FROM_U8('f', 'r', 'i', 0)) pTime->u8WeekDay = 4;
1209 else if (uWeekDay == RT_MAKE_U32_FROM_U8('s', 'a', 't', 0)) pTime->u8WeekDay = 5;
1210 else if (uWeekDay == RT_MAKE_U32_FROM_U8('s', 'u', 'n', 0)) pTime->u8WeekDay = 6;
1211 else
1212 return NULL;
1213 pszString += 3;
1214 while (RT_C_IS_ALPHA(*pszString))
1215 pszString++;
1216 if (*pszString == ',')
1217 pszString++;
1218 while (RT_C_IS_SPACE(*pszString))
1219 pszString++;
1220 if (!RT_C_IS_DIGIT(pszString[0]))
1221 return NULL;
1222 }
1223 else if (RT_C_IS_DIGIT(pszString[0]))
1224 pTime->u8WeekDay = UINT8_MAX;
1225 else
1226 return NULL;
1227
1228 /* Day of month.*/
1229 int rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay);
1230 if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS)
1231 return NULL;
1232 while (RT_C_IS_SPACE(*pszString))
1233 pszString++;
1234
1235 /* Month of the year. */
1236 if (pszString[0] == '\0' || pszString[1] == '\0' || pszString[2] == '\0')
1237 return NULL;
1238 uint32_t uMonth = RT_MAKE_U32_FROM_U8(RT_C_TO_LOWER(pszString[0]), RT_C_TO_LOWER(pszString[1]),
1239 RT_C_TO_LOWER(pszString[2]), 0);
1240 if ( uMonth == RT_MAKE_U32_FROM_U8('j', 'a', 'n', 0)) pTime->u8Month = 1;
1241 else if (uMonth == RT_MAKE_U32_FROM_U8('f', 'e', 'b', 0)) pTime->u8Month = 2;
1242 else if (uMonth == RT_MAKE_U32_FROM_U8('m', 'a', 'r', 0)) pTime->u8Month = 3;
1243 else if (uMonth == RT_MAKE_U32_FROM_U8('a', 'p', 'r', 0)) pTime->u8Month = 4;
1244 else if (uMonth == RT_MAKE_U32_FROM_U8('m', 'a', 'y', 0)) pTime->u8Month = 5;
1245 else if (uMonth == RT_MAKE_U32_FROM_U8('j', 'u', 'n', 0)) pTime->u8Month = 6;
1246 else if (uMonth == RT_MAKE_U32_FROM_U8('j', 'u', 'l', 0)) pTime->u8Month = 7;
1247 else if (uMonth == RT_MAKE_U32_FROM_U8('a', 'u', 'g', 0)) pTime->u8Month = 8;
1248 else if (uMonth == RT_MAKE_U32_FROM_U8('s', 'e', 'p', 0)) pTime->u8Month = 9;
1249 else if (uMonth == RT_MAKE_U32_FROM_U8('o', 'c', 't', 0)) pTime->u8Month = 10;
1250 else if (uMonth == RT_MAKE_U32_FROM_U8('n', 'o', 'v', 0)) pTime->u8Month = 11;
1251 else if (uMonth == RT_MAKE_U32_FROM_U8('d', 'e', 'c', 0)) pTime->u8Month = 12;
1252 else
1253 return NULL;
1254 pszString += 3;
1255 while (RT_C_IS_ALPHA(*pszString))
1256 pszString++;
1257 while (RT_C_IS_SPACE(*pszString))
1258 pszString++;
1259
1260 /* Year */
1261 const char * const pszStartYear = pszString;
1262 rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year);
1263 if (rc != VWRN_TRAILING_CHARS)
1264 return NULL;
1265 if (pszString - pszStartYear >= 4 )
1266 { /* likely */ }
1267 else if (pszString - pszStartYear == 3)
1268 pTime->i32Year += 1900;
1269 else if (pszString - pszStartYear == 2)
1270 pTime->i32Year += pTime->i32Year >= 50 ? 1900 : 2000;
1271 else
1272 return NULL;
1273
1274 bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
1275 if (fLeapYear)
1276 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
1277
1278 while (RT_C_IS_SPACE(*pszString))
1279 pszString++;
1280
1281
1282 /* Calculate year day. */
1283 unsigned const cDaysInMonth = fLeapYear
1284 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
1285 : g_acDaysInMonths[pTime->u8Month - 1];
1286 if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth)
1287 return NULL;
1288
1289 pTime->u16YearDay = pTime->u8MonthDay - 1
1290 + (fLeapYear
1291 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
1292 : g_aiDayOfYear[pTime->u8Month - 1]);
1293
1294 /*
1295 * The time part.
1296 */
1297 /* Hour. */
1298 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour);
1299 if (rc != VWRN_TRAILING_CHARS)
1300 return NULL;
1301 if (pTime->u8Hour > 23)
1302 return NULL;
1303 if (*pszString++ != ':')
1304 return NULL;
1305
1306 /* Minute. */
1307 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
1308 if (rc != VWRN_TRAILING_CHARS)
1309 return NULL;
1310 if (pTime->u8Minute > 59)
1311 return NULL;
1312 if (*pszString++ != ':')
1313 return NULL;
1314
1315 /* Second. */
1316 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Second);
1317 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
1318 return NULL;
1319 if (pTime->u8Second > 59)
1320 return NULL;
1321
1322 /* Non-standard fraction. Handy for testing, though. */
1323 if (*pszString == '.')
1324 {
1325 const char * const pszStart = ++pszString;
1326 rc = RTStrToUInt32Ex(pszString, (char **)&pszString, 10, &pTime->u32Nanosecond);
1327 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
1328 return NULL;
1329 if (pTime->u32Nanosecond >= 1000000000)
1330 return NULL;
1331 switch (pszString - pszStart)
1332 {
1333 case 1: pTime->u32Nanosecond *= 100000000; break;
1334 case 2: pTime->u32Nanosecond *= 10000000; break;
1335 case 3: pTime->u32Nanosecond *= 1000000; break;
1336 case 4: pTime->u32Nanosecond *= 100000; break;
1337 case 5: pTime->u32Nanosecond *= 10000; break;
1338 case 6: pTime->u32Nanosecond *= 1000; break;
1339 case 7: pTime->u32Nanosecond *= 100; break;
1340 case 8: pTime->u32Nanosecond *= 10; break;
1341 case 9: break;
1342 default:
1343 return NULL;
1344 }
1345 if (pTime->u32Nanosecond >= 1000000000)
1346 return NULL;
1347 }
1348 else
1349 pTime->u32Nanosecond = 0;
1350 while (RT_C_IS_SPACE(*pszString))
1351 pszString++;
1352
1353 /*
1354 * Time zone.
1355 */
1356 if ( *pszString == '+'
1357 || *pszString == '-')
1358 {
1359 if ( !RT_C_IS_DIGIT(pszString[1])
1360 || !RT_C_IS_DIGIT(pszString[2]))
1361 return NULL;
1362 int8_t cUtcHours = (pszString[1] - '0') * 10 + (pszString[2] - '0');
1363 char chSign = *pszString;
1364 if (chSign == '-')
1365 cUtcHours = -cUtcHours;
1366 pszString += 3;
1367
1368 uint8_t cUtcMin = 0;
1369 if (RT_C_IS_DIGIT(pszString[0]))
1370 {
1371 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &cUtcMin);
1372 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
1373 return NULL;
1374 }
1375 else if (*pszString && !RT_C_IS_BLANK(*pszString))
1376 return NULL;
1377 if (cUtcHours >= 0)
1378 pTime->offUTC = cUtcHours * 60 + cUtcMin;
1379 else
1380 pTime->offUTC = cUtcHours * 60 - cUtcMin;
1381 if (RT_ABS(pTime->offUTC) > 840)
1382 return NULL;
1383
1384 /* -0000: GMT isn't necessarily the local time zone, so change flags from local to UTC. */
1385 if (pTime->offUTC == 0 && chSign == '-')
1386 {
1387 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1388 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1389 }
1390 }
1391 else if (RT_C_IS_ALPHA(*pszString))
1392 {
1393 uint32_t uTimeZone = RT_MAKE_U32_FROM_U8(RT_C_TO_LOWER(pszString[0]), RT_C_TO_LOWER(pszString[1]),
1394 RT_C_TO_LOWER(pszString[2]), 0);
1395 if (uTimeZone == RT_MAKE_U32_FROM_U8('g', 'm', 't', 0))
1396 {
1397 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1398 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1399 pTime->offUTC = 0;
1400 pszString += 3;
1401 }
1402 else if ((uint16_t)uTimeZone == RT_MAKE_U16('u', 't'))
1403 {
1404 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1405 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1406 pTime->offUTC = 0;
1407 pszString += 2;
1408 }
1409 else
1410 {
1411 static const struct { uint32_t uTimeZone; int32_t offUtc; } s_aLegacyTimeZones[] =
1412 {
1413 { RT_MAKE_U32_FROM_U8('e', 'd', 't', 0), -4*60 },
1414 { RT_MAKE_U32_FROM_U8('e', 's', 't', 0), -5*60 },
1415 { RT_MAKE_U32_FROM_U8('c', 'd', 't', 0), -5*60 },
1416 { RT_MAKE_U32_FROM_U8('c', 's', 't', 0), -6*60 },
1417 { RT_MAKE_U32_FROM_U8('m', 'd', 't', 0), -6*60 },
1418 { RT_MAKE_U32_FROM_U8('m', 's', 't', 0), -7*60 },
1419 { RT_MAKE_U32_FROM_U8('p', 'd', 't', 0), -7*60 },
1420 { RT_MAKE_U32_FROM_U8('p', 's', 't', 0), -8*60 },
1421 };
1422 size_t i = RT_ELEMENTS(s_aLegacyTimeZones);
1423 while (i-- > 0)
1424 if (s_aLegacyTimeZones[i].uTimeZone == uTimeZone)
1425 {
1426 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1427 pTime->fFlags |= RTTIME_FLAGS_TYPE_LOCAL;
1428 pTime->offUTC = s_aLegacyTimeZones[i].offUtc;
1429 pszString += 3;
1430 break;
1431 }
1432 }
1433
1434 }
1435 /* else: No time zone given, local with offUTC = 0. */
1436
1437 /*
1438 * The rest of the string should be blanks.
1439 */
1440 char ch;
1441 while ((ch = *pszString++) != '\0')
1442 if (!RT_C_IS_BLANK(ch))
1443 return NULL;
1444
1445 rtTimeNormalizeInternal(pTime);
1446 return pTime;
1447}
1448RT_EXPORT_SYMBOL(RTTimeFromRfc2822);
1449
1450
1451/**
1452 * Adds one day to @a pTime.
1453 *
1454 * ASSUMES it is zulu time so DST can be ignored.
1455 */
1456static PRTTIME rtTimeAdd1Day(PRTTIME pTime)
1457{
1458 Assert(!pTime->offUTC);
1459 rtTimeNormalizeInternal(pTime);
1460 pTime->u8MonthDay += 1;
1461 pTime->u16YearDay = 0;
1462 return rtTimeNormalizeInternal(pTime);
1463}
1464
1465
1466/**
1467 * Subtracts one day from @a pTime.
1468 *
1469 * ASSUMES it is zulu time so DST can be ignored.
1470 */
1471static PRTTIME rtTimeSub1Day(PRTTIME pTime)
1472{
1473 Assert(!pTime->offUTC);
1474 rtTimeNormalizeInternal(pTime);
1475 if (pTime->u16YearDay > 1)
1476 {
1477 pTime->u16YearDay -= 1;
1478 pTime->u8Month = 0;
1479 pTime->u8MonthDay = 0;
1480 }
1481 else
1482 {
1483 pTime->i32Year -= 1;
1484 pTime->u16YearDay = rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365;
1485 pTime->u8MonthDay = 31;
1486 pTime->u8Month = 12;
1487 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
1488 }
1489 return rtTimeNormalizeInternal(pTime);
1490}
1491
1492
1493/**
1494 * Adds a signed number of minutes to @a pTime.
1495 *
1496 * ASSUMES it is zulu time so DST can be ignored.
1497 *
1498 * @param pTime The time structure to work on.
1499 * @param cAddend Number of minutes to add.
1500 * ASSUMES the value isn't all that high!
1501 */
1502static PRTTIME rtTimeAddMinutes(PRTTIME pTime, int32_t cAddend)
1503{
1504 Assert(RT_ABS(cAddend) < 31 * 24 * 60);
1505
1506 /*
1507 * Work on minutes of the day.
1508 */
1509 int32_t const cMinutesInDay = 24 * 60;
1510 int32_t iDayMinute = (unsigned)pTime->u8Hour * 60 + pTime->u8Minute;
1511 iDayMinute += cAddend;
1512
1513 while (iDayMinute >= cMinutesInDay)
1514 {
1515 rtTimeAdd1Day(pTime);
1516 iDayMinute -= cMinutesInDay;
1517 }
1518
1519 while (iDayMinute < 0)
1520 {
1521 rtTimeSub1Day(pTime);
1522 iDayMinute += cMinutesInDay;
1523 }
1524
1525 pTime->u8Hour = iDayMinute / 60;
1526 pTime->u8Minute = iDayMinute % 60;
1527
1528 return pTime;
1529}
1530
1531
1532/**
1533 * Converts @a pTime to zulu time (UTC) if needed.
1534 *
1535 * @returns pTime.
1536 * @param pTime What to convert (in/out).
1537 */
1538static PRTTIME rtTimeConvertToZulu(PRTTIME pTime)
1539{
1540 RTTIME_ASSERT_NORMALIZED(pTime);
1541 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC)
1542 {
1543 int32_t offUTC = pTime->offUTC;
1544 pTime->offUTC = 0;
1545 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1546 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1547 if (offUTC != 0)
1548 rtTimeAddMinutes(pTime, -offUTC);
1549 }
1550 return pTime;
1551}
1552
1553
1554/**
1555 * Converts a time structure to UTC, relying on UTC offset information if it contains local time.
1556 *
1557 * @returns pTime on success.
1558 * @returns NULL if the data is invalid.
1559 * @param pTime The time structure to convert.
1560 */
1561RTDECL(PRTTIME) RTTimeConvertToZulu(PRTTIME pTime)
1562{
1563 /*
1564 * Validate that we've got the minimum of stuff handy.
1565 */
1566 AssertPtrReturn(pTime, NULL);
1567 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
1568
1569 return rtTimeConvertToZulu(rtTimeNormalizeInternal(pTime));
1570}
1571RT_EXPORT_SYMBOL(RTTimeConvertToZulu);
1572
1573
1574/**
1575 * Compares two normalized time structures.
1576 *
1577 * @retval 0 if equal.
1578 * @retval -1 if @a pLeft is earlier than @a pRight.
1579 * @retval 1 if @a pRight is earlier than @a pLeft.
1580 *
1581 * @param pLeft The left side time. NULL is accepted.
1582 * @param pRight The right side time. NULL is accepted.
1583 *
1584 * @note A NULL time is considered smaller than anything else. If both are
1585 * NULL, they are considered equal.
1586 */
1587RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight)
1588{
1589#ifdef RT_STRICT
1590 if (pLeft)
1591 RTTIME_ASSERT_NORMALIZED(pLeft);
1592 if (pRight)
1593 RTTIME_ASSERT_NORMALIZED(pRight);
1594#endif
1595
1596 int iRet;
1597 if (pLeft)
1598 {
1599 if (pRight)
1600 {
1601 /*
1602 * Only work with normalized zulu time.
1603 */
1604 RTTIME TmpLeft;
1605 if ( pLeft->offUTC != 0
1606 || pLeft->u16YearDay == 0
1607 || pLeft->u16YearDay > 366
1608 || pLeft->u8Hour >= 60
1609 || pLeft->u8Minute >= 60
1610 || pLeft->u8Second >= 60)
1611 {
1612 TmpLeft = *pLeft;
1613 pLeft = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpLeft));
1614 }
1615
1616 RTTIME TmpRight;
1617 if ( pRight->offUTC != 0
1618 || pRight->u16YearDay == 0
1619 || pRight->u16YearDay > 366
1620 || pRight->u8Hour >= 60
1621 || pRight->u8Minute >= 60
1622 || pRight->u8Second >= 60)
1623 {
1624 TmpRight = *pRight;
1625 pRight = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpRight));
1626 }
1627
1628 /*
1629 * Do the comparison.
1630 */
1631 if ( pLeft->i32Year != pRight->i32Year)
1632 iRet = pLeft->i32Year < pRight->i32Year ? -1 : 1;
1633 else if ( pLeft->u16YearDay != pRight->u16YearDay)
1634 iRet = pLeft->u16YearDay < pRight->u16YearDay ? -1 : 1;
1635 else if ( pLeft->u8Hour != pRight->u8Hour)
1636 iRet = pLeft->u8Hour < pRight->u8Hour ? -1 : 1;
1637 else if ( pLeft->u8Minute != pRight->u8Minute)
1638 iRet = pLeft->u8Minute < pRight->u8Minute ? -1 : 1;
1639 else if ( pLeft->u8Second != pRight->u8Second)
1640 iRet = pLeft->u8Second < pRight->u8Second ? -1 : 1;
1641 else if ( pLeft->u32Nanosecond != pRight->u32Nanosecond)
1642 iRet = pLeft->u32Nanosecond < pRight->u32Nanosecond ? -1 : 1;
1643 else
1644 iRet = 0;
1645 }
1646 else
1647 iRet = 1;
1648 }
1649 else
1650 iRet = pRight ? -1 : 0;
1651 return iRet;
1652}
1653RT_EXPORT_SYMBOL(RTTimeCompare);
1654
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette