VirtualBox

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

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

IPRT/time: Extended RTTimeToRfc2822 with a flag for specifying RFC-7231 behaviour. Added simple testcase for it. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 47.2 KB
Line 
1/* $Id: time.cpp 74125 2018-09-06 16:14:13Z 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.
803 *
804 * @returns psz on success.
805 * @returns NULL on buffer underflow.
806 * @param pTime The time spec.
807 * @param psz Where to store the string.
808 * @param cb The size of the buffer.
809 */
810RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb)
811{
812 RTTIME Time;
813 return RTTimeToString(RTTimeExplode(&Time, pTime), psz, cb);
814}
815RT_EXPORT_SYMBOL(RTTimeSpecToString);
816
817
818
819/**
820 * Attempts to convert an ISO date string to a time structure.
821 *
822 * We're a little forgiving with zero padding, unspecified parts, and leading
823 * and trailing spaces.
824 *
825 * @retval pTime on success,
826 * @retval NULL on failure.
827 * @param pTime Where to store the time on success.
828 * @param pszString The ISO date string to convert.
829 */
830RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString)
831{
832 /* Ignore leading spaces. */
833 while (RT_C_IS_SPACE(*pszString))
834 pszString++;
835
836 /*
837 * Init non date & time parts.
838 */
839 pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL;
840 pTime->offUTC = 0;
841
842 /*
843 * The day part.
844 */
845
846 /* Year */
847 int rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year);
848 if (rc != VWRN_TRAILING_CHARS)
849 return NULL;
850
851 bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
852 if (fLeapYear)
853 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
854
855 if (*pszString++ != '-')
856 return NULL;
857
858 /* Month of the year. */
859 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Month);
860 if (rc != VWRN_TRAILING_CHARS)
861 return NULL;
862 if (pTime->u8Month == 0 || pTime->u8Month > 12)
863 return NULL;
864 if (*pszString++ != '-')
865 return NULL;
866
867 /* Day of month.*/
868 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay);
869 if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS)
870 return NULL;
871 unsigned const cDaysInMonth = fLeapYear
872 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
873 : g_acDaysInMonths[pTime->u8Month - 1];
874 if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth)
875 return NULL;
876
877 /* Calculate year day. */
878 pTime->u16YearDay = pTime->u8MonthDay - 1
879 + (fLeapYear
880 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
881 : g_aiDayOfYear[pTime->u8Month - 1]);
882
883 /*
884 * The time part.
885 */
886 if (*pszString++ != 'T')
887 return NULL;
888
889 /* Hour. */
890 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour);
891 if (rc != VWRN_TRAILING_CHARS)
892 return NULL;
893 if (pTime->u8Hour > 23)
894 return NULL;
895 if (*pszString++ != ':')
896 return NULL;
897
898 /* Minute. */
899 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
900 if (rc != VWRN_TRAILING_CHARS)
901 return NULL;
902 if (pTime->u8Minute > 59)
903 return NULL;
904 if (*pszString++ != ':')
905 return NULL;
906
907 /* Second. */
908 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Second);
909 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
910 return NULL;
911 if (pTime->u8Second > 59)
912 return NULL;
913
914 /* Nanoseconds is optional and probably non-standard. */
915 if (*pszString == '.')
916 {
917 rc = RTStrToUInt32Ex(pszString + 1, (char **)&pszString, 10, &pTime->u32Nanosecond);
918 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
919 return NULL;
920 if (pTime->u32Nanosecond >= 1000000000)
921 return NULL;
922 }
923 else
924 pTime->u32Nanosecond = 0;
925
926 /*
927 * Time zone.
928 */
929 if (*pszString == 'Z')
930 {
931 pszString++;
932 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
933 pTime->fFlags |= ~RTTIME_FLAGS_TYPE_UTC;
934 pTime->offUTC = 0;
935 }
936 else if ( *pszString == '+'
937 || *pszString == '-')
938 {
939 int8_t cUtcHours = 0;
940 rc = RTStrToInt8Ex(pszString, (char **)&pszString, 10, &cUtcHours);
941 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
942 return NULL;
943 uint8_t cUtcMin = 0;
944 if (*pszString == ':')
945 {
946 rc = RTStrToUInt8Ex(pszString + 1, (char **)&pszString, 10, &cUtcMin);
947 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
948 return NULL;
949 }
950 else if (*pszString && !RT_C_IS_BLANK(*pszString))
951 return NULL;
952 if (cUtcHours >= 0)
953 pTime->offUTC = cUtcHours * 60 + cUtcMin;
954 else
955 pTime->offUTC = cUtcHours * 60 - cUtcMin;
956 if (RT_ABS(pTime->offUTC) > 840)
957 return NULL;
958 }
959 /* else: No time zone given, local with offUTC = 0. */
960
961 /*
962 * The rest of the string should be blanks.
963 */
964 char ch;
965 while ((ch = *pszString++) != '\0')
966 if (!RT_C_IS_BLANK(ch))
967 return NULL;
968
969 return pTime;
970}
971RT_EXPORT_SYMBOL(RTTimeFromString);
972
973
974/**
975 * Attempts to convert an ISO date string to a time structure.
976 *
977 * We're a little forgiving with zero padding, unspecified parts, and leading
978 * and trailing spaces.
979 *
980 * @retval pTime on success,
981 * @retval NULL on failure.
982 * @param pTime The time spec.
983 * @param pszString The ISO date string to convert.
984 */
985RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString)
986{
987 RTTIME Time;
988 if (RTTimeFromString(&Time, pszString))
989 return RTTimeImplode(pTime, &Time);
990 return NULL;
991}
992RT_EXPORT_SYMBOL(RTTimeSpecFromString);
993
994
995/**
996 * Formats the given time on a RTC-2822 compliant format.
997 *
998 * @returns Output string length on success (positive), VERR_BUFFER_OVERFLOW
999 * (negative) on failure.
1000 * @param pTime The time. Caller should've normalized this.
1001 * @param psz Where to store the string.
1002 * @param cb The size of the buffer.
1003 */
1004RTDECL(ssize_t) RTTimeToRfc2822(PRTTIME pTime, char *psz, size_t cb, uint32_t fFlags)
1005{
1006 Assert(pTime->u8Month > 0 && pTime->u8Month <= 12);
1007 Assert(pTime->u8WeekDay < 7);
1008 Assert(!(fFlags & ~RTTIME_RFC2822_F_GMT));
1009
1010 /* (Default to UTC if not specified) */
1011 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
1012 && pTime->offUTC)
1013 {
1014 Assert(!(fFlags & RTTIME_RFC2822_F_GMT) /* don't call with local time. duh! */ );
1015
1016 /* Calc the UTC offset part. */
1017 int32_t offUtc = pTime->offUTC;
1018 Assert(offUtc <= 840 && offUtc >= -840);
1019 char chSign;
1020 if (offUtc >= 0)
1021 chSign = '+';
1022 else
1023 {
1024 chSign = '-';
1025 offUtc = -offUtc;
1026 }
1027 uint32_t offUtcHour = (uint32_t)offUtc / 60;
1028 uint32_t offUtcMinute = (uint32_t)offUtc % 60;
1029
1030 /* Example: "Mon, 31 Aug 2018 00:00:00 +0200" */
1031 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u %c%02u%02u", g_apszWeekDays[pTime->u8WeekDay],
1032 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1033 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, chSign, offUtcHour, offUtcMinute);
1034 if ( cch >= 27
1035 && psz[cch - 5] == chSign)
1036 return cch;
1037 }
1038 else if (fFlags & RTTIME_RFC2822_F_GMT)
1039 {
1040 /* Example: "Mon, 1 Jan 1971 23:55:59 GMT" */
1041 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u GMT", g_apszWeekDays[pTime->u8WeekDay],
1042 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1043 pTime->u8Hour, pTime->u8Minute, pTime->u8Second);
1044 if ( cch >= 27
1045 && psz[cch - 1] == 'T')
1046 return cch;
1047 }
1048 else
1049 {
1050 /* Example: "Mon, 1 Jan 1971 00:00:00 -0000" */
1051 size_t cch = RTStrPrintf(psz, cb, "%s, %u %s %04RI32 %02u:%02u:%02u -0000", g_apszWeekDays[pTime->u8WeekDay],
1052 pTime->u8MonthDay, g_apszMonths[pTime->u8Month], pTime->i32Year,
1053 pTime->u8Hour, pTime->u8Minute, pTime->u8Second);
1054 if ( cch >= 27
1055 && psz[cch - 5] == '-')
1056 return cch;
1057 }
1058 return VERR_BUFFER_OVERFLOW;
1059}
1060RT_EXPORT_SYMBOL(RTTimeToRfc2822);
1061
1062
1063/**
1064 * Adds one day to @a pTime.
1065 *
1066 * ASSUMES it is zulu time so DST can be ignored.
1067 */
1068static PRTTIME rtTimeAdd1Day(PRTTIME pTime)
1069{
1070 Assert(!pTime->offUTC);
1071 rtTimeNormalizeInternal(pTime);
1072 pTime->u8MonthDay += 1;
1073 pTime->u16YearDay = 0;
1074 return rtTimeNormalizeInternal(pTime);
1075}
1076
1077
1078/**
1079 * Subtracts one day from @a pTime.
1080 *
1081 * ASSUMES it is zulu time so DST can be ignored.
1082 */
1083static PRTTIME rtTimeSub1Day(PRTTIME pTime)
1084{
1085 Assert(!pTime->offUTC);
1086 rtTimeNormalizeInternal(pTime);
1087 if (pTime->u16YearDay > 1)
1088 {
1089 pTime->u16YearDay -= 1;
1090 pTime->u8Month = 0;
1091 pTime->u8MonthDay = 0;
1092 }
1093 else
1094 {
1095 pTime->i32Year -= 1;
1096 pTime->u16YearDay = rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365;
1097 pTime->u8MonthDay = 31;
1098 pTime->u8Month = 12;
1099 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
1100 }
1101 return rtTimeNormalizeInternal(pTime);
1102}
1103
1104
1105/**
1106 * Adds a signed number of minutes to @a pTime.
1107 *
1108 * ASSUMES it is zulu time so DST can be ignored.
1109 *
1110 * @param pTime The time structure to work on.
1111 * @param cAddend Number of minutes to add.
1112 * ASSUMES the value isn't all that high!
1113 */
1114static PRTTIME rtTimeAddMinutes(PRTTIME pTime, int32_t cAddend)
1115{
1116 Assert(RT_ABS(cAddend) < 31 * 24 * 60);
1117
1118 /*
1119 * Work on minutes of the day.
1120 */
1121 int32_t const cMinutesInDay = 24 * 60;
1122 int32_t iDayMinute = (unsigned)pTime->u8Hour * 60 + pTime->u8Minute;
1123 iDayMinute += cAddend;
1124
1125 while (iDayMinute >= cMinutesInDay)
1126 {
1127 rtTimeAdd1Day(pTime);
1128 iDayMinute -= cMinutesInDay;
1129 }
1130
1131 while (iDayMinute < 0)
1132 {
1133 rtTimeSub1Day(pTime);
1134 iDayMinute += cMinutesInDay;
1135 }
1136
1137 pTime->u8Hour = iDayMinute / 60;
1138 pTime->u8Minute = iDayMinute % 60;
1139
1140 return pTime;
1141}
1142
1143
1144/**
1145 * Converts @a pTime to zulu time (UTC) if needed.
1146 *
1147 * @returns pTime.
1148 * @param pTime What to convert (in/out).
1149 */
1150static PRTTIME rtTimeConvertToZulu(PRTTIME pTime)
1151{
1152 RTTIME_ASSERT_NORMALIZED(pTime);
1153 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC)
1154 {
1155 int32_t offUTC = pTime->offUTC;
1156 pTime->offUTC = 0;
1157 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1158 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1159 if (offUTC != 0)
1160 rtTimeAddMinutes(pTime, -offUTC);
1161 }
1162 return pTime;
1163}
1164
1165
1166/**
1167 * Converts a time structure to UTC, relying on UTC offset information if it contains local time.
1168 *
1169 * @returns pTime on success.
1170 * @returns NULL if the data is invalid.
1171 * @param pTime The time structure to convert.
1172 */
1173RTDECL(PRTTIME) RTTimeConvertToZulu(PRTTIME pTime)
1174{
1175 /*
1176 * Validate that we've got the minimum of stuff handy.
1177 */
1178 AssertReturn(VALID_PTR(pTime), NULL);
1179 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
1180
1181 return rtTimeConvertToZulu(rtTimeNormalizeInternal(pTime));
1182}
1183RT_EXPORT_SYMBOL(RTTimeConvertToZulu);
1184
1185
1186/**
1187 * Compares two normalized time structures.
1188 *
1189 * @retval 0 if equal.
1190 * @retval -1 if @a pLeft is earlier than @a pRight.
1191 * @retval 1 if @a pRight is earlier than @a pLeft.
1192 *
1193 * @param pLeft The left side time. NULL is accepted.
1194 * @param pRight The right side time. NULL is accepted.
1195 *
1196 * @note A NULL time is considered smaller than anything else. If both are
1197 * NULL, they are considered equal.
1198 */
1199RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight)
1200{
1201#ifdef RT_STRICT
1202 if (pLeft)
1203 RTTIME_ASSERT_NORMALIZED(pLeft);
1204 if (pRight)
1205 RTTIME_ASSERT_NORMALIZED(pRight);
1206#endif
1207
1208 int iRet;
1209 if (pLeft)
1210 {
1211 if (pRight)
1212 {
1213 /*
1214 * Only work with normalized zulu time.
1215 */
1216 RTTIME TmpLeft;
1217 if ( pLeft->offUTC != 0
1218 || pLeft->u16YearDay == 0
1219 || pLeft->u16YearDay > 366
1220 || pLeft->u8Hour >= 60
1221 || pLeft->u8Minute >= 60
1222 || pLeft->u8Second >= 60)
1223 {
1224 TmpLeft = *pLeft;
1225 pLeft = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpLeft));
1226 }
1227
1228 RTTIME TmpRight;
1229 if ( pRight->offUTC != 0
1230 || pRight->u16YearDay == 0
1231 || pRight->u16YearDay > 366
1232 || pRight->u8Hour >= 60
1233 || pRight->u8Minute >= 60
1234 || pRight->u8Second >= 60)
1235 {
1236 TmpRight = *pRight;
1237 pRight = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpRight));
1238 }
1239
1240 /*
1241 * Do the comparison.
1242 */
1243 if ( pLeft->i32Year != pRight->i32Year)
1244 iRet = pLeft->i32Year < pRight->i32Year ? -1 : 1;
1245 else if ( pLeft->u16YearDay != pRight->u16YearDay)
1246 iRet = pLeft->u16YearDay < pRight->u16YearDay ? -1 : 1;
1247 else if ( pLeft->u8Hour != pRight->u8Hour)
1248 iRet = pLeft->u8Hour < pRight->u8Hour ? -1 : 1;
1249 else if ( pLeft->u8Minute != pRight->u8Minute)
1250 iRet = pLeft->u8Minute < pRight->u8Minute ? -1 : 1;
1251 else if ( pLeft->u8Second != pRight->u8Second)
1252 iRet = pLeft->u8Second < pRight->u8Second ? -1 : 1;
1253 else if ( pLeft->u32Nanosecond != pRight->u32Nanosecond)
1254 iRet = pLeft->u32Nanosecond < pRight->u32Nanosecond ? -1 : 1;
1255 else
1256 iRet = 0;
1257 }
1258 else
1259 iRet = 1;
1260 }
1261 else
1262 iRet = pRight ? -1 : 0;
1263 return iRet;
1264}
1265RT_EXPORT_SYMBOL(RTTimeCompare);
1266
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