VirtualBox

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

Last change on this file since 74158 was 74150, checked in by vboxsync, 6 years ago

IPRT/time: Added a few RTTimeFromXxxx tests and fixed bugs found. bugref:9167

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