VirtualBox

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

Last change on this file since 72171 was 72171, checked in by vboxsync, 7 years ago

make scm happy

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 44.5 KB
Line 
1/* $Id: time.cpp 72171 2018-05-08 17:57:55Z 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
240/*********************************************************************************************************************************
241* Internal Functions *
242*********************************************************************************************************************************/
243static PRTTIME rtTimeConvertToZulu(PRTTIME pTime);
244
245
246/**
247 * Checks if a year is a leap year or not.
248 *
249 * @returns true if it's a leap year.
250 * @returns false if it's a common year.
251 * @param i32Year The year in question.
252 */
253DECLINLINE(bool) rtTimeIsLeapYear(int32_t i32Year)
254{
255 return i32Year % 4 == 0
256 && ( i32Year % 100 != 0
257 || i32Year % 400 == 0);
258}
259
260
261/**
262 * Checks if a year is a leap year or not.
263 *
264 * @returns true if it's a leap year.
265 * @returns false if it's a common year.
266 * @param i32Year The year in question.
267 */
268RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year)
269{
270 return rtTimeIsLeapYear(i32Year);
271}
272RT_EXPORT_SYMBOL(RTTimeIsLeapYear);
273
274
275/**
276 * Explodes a time spec (UTC).
277 *
278 * @returns pTime.
279 * @param pTime Where to store the exploded time.
280 * @param pTimeSpec The time spec to exploded.
281 */
282RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
283{
284 int64_t i64Div;
285 int32_t i32Div;
286 int32_t i32Rem;
287 unsigned iYear;
288 const uint16_t *paiDayOfYear;
289 int iMonth;
290
291 AssertMsg(VALID_PTR(pTime), ("%p\n", pTime));
292 AssertMsg(VALID_PTR(pTimeSpec), ("%p\n", pTime));
293
294 /*
295 * The simple stuff first.
296 */
297 pTime->fFlags = RTTIME_FLAGS_TYPE_UTC;
298 i64Div = pTimeSpec->i64NanosecondsRelativeToUnixEpoch;
299 i32Rem = (int32_t)(i64Div % 1000000000);
300 i64Div /= 1000000000;
301 if (i32Rem < 0)
302 {
303 i32Rem += 1000000000;
304 i64Div--;
305 }
306 pTime->u32Nanosecond = i32Rem;
307
308 /* second */
309 i32Rem = (int32_t)(i64Div % 60);
310 i64Div /= 60;
311 if (i32Rem < 0)
312 {
313 i32Rem += 60;
314 i64Div--;
315 }
316 pTime->u8Second = i32Rem;
317
318 /* minute */
319 i32Div = (int32_t)i64Div; /* 60,000,000,000 > 33bit, so 31bit suffices. */
320 i32Rem = i32Div % 60;
321 i32Div /= 60;
322 if (i32Rem < 0)
323 {
324 i32Rem += 60;
325 i32Div--;
326 }
327 pTime->u8Minute = i32Rem;
328
329 /* hour */
330 i32Rem = i32Div % 24;
331 i32Div /= 24; /* days relative to 1970-01-01 */
332 if (i32Rem < 0)
333 {
334 i32Rem += 24;
335 i32Div--;
336 }
337 pTime->u8Hour = i32Rem;
338
339 /* weekday - 1970-01-01 was a Thursday (3) */
340 pTime->u8WeekDay = ((int)(i32Div % 7) + 3 + 7) % 7;
341
342 /*
343 * We've now got a number of days relative to 1970-01-01.
344 * To get the correct year number we have to mess with leap years. Fortunately,
345 * the representation we've got only supports a few hundred years, so we can
346 * generate a table and perform a simple two way search from the modulus 365 derived.
347 */
348 iYear = OFF_YEAR_IDX_EPOCH + i32Div / 365;
349 while (g_aoffYear[iYear + 1] <= i32Div)
350 iYear++;
351 while (g_aoffYear[iYear] > i32Div)
352 iYear--;
353 pTime->i32Year = iYear + OFF_YEAR_IDX_0_YEAR;
354 i32Div -= g_aoffYear[iYear];
355 pTime->u16YearDay = i32Div + 1;
356
357 /*
358 * Figuring out the month is done in a manner similar to the year, only here we
359 * ensure that the index is matching or too small.
360 */
361 if (rtTimeIsLeapYear(pTime->i32Year))
362 {
363 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
364 paiDayOfYear = &g_aiDayOfYearLeap[0];
365 }
366 else
367 {
368 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
369 paiDayOfYear = &g_aiDayOfYear[0];
370 }
371 iMonth = i32Div / 32;
372 i32Div++;
373 while (paiDayOfYear[iMonth + 1] <= i32Div)
374 iMonth++;
375 pTime->u8Month = iMonth + 1;
376 i32Div -= paiDayOfYear[iMonth];
377 pTime->u8MonthDay = i32Div + 1;
378
379 /* This is for UTC timespecs, so, no offset. */
380 pTime->offUTC = 0;
381
382 return pTime;
383}
384RT_EXPORT_SYMBOL(RTTimeExplode);
385
386
387/**
388 * Implodes exploded time to a time spec (UTC).
389 *
390 * @returns pTime on success.
391 * @returns NULL if the pTime data is invalid.
392 * @param pTimeSpec Where to store the imploded UTC time.
393 * If pTime specifies a time which outside the range, maximum or
394 * minimum values will be returned.
395 * @param pTime Pointer to the exploded time to implode.
396 * The fields u8Month, u8WeekDay and u8MonthDay are not used,
397 * and all the other fields are expected to be within their
398 * bounds. Use RTTimeNormalize() or RTTimeLocalNormalize() to
399 * calculate u16YearDay and normalize the ranges of the fields.
400 */
401RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime)
402{
403 int32_t i32Days;
404 uint32_t u32Secs;
405 int64_t i64Nanos;
406
407 /*
408 * Validate input.
409 */
410 AssertReturn(VALID_PTR(pTimeSpec), NULL);
411 AssertReturn(VALID_PTR(pTime), NULL);
412 AssertReturn(pTime->u32Nanosecond < 1000000000, NULL);
413 AssertReturn(pTime->u8Second < 60, NULL);
414 AssertReturn(pTime->u8Minute < 60, NULL);
415 AssertReturn(pTime->u8Hour < 24, NULL);
416 AssertReturn(pTime->u16YearDay >= 1, NULL);
417 AssertReturn(pTime->u16YearDay <= (rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365), NULL);
418 AssertMsgReturn(pTime->i32Year <= RTTIME_MAX_YEAR && pTime->i32Year >= RTTIME_MIN_YEAR, ("%RI32\n", pTime->i32Year), NULL);
419 Assert(pTime->offUTC >= -840 && pTime->offUTC <= 840);
420
421 /*
422 * Do the conversion to nanoseconds.
423 */
424 i32Days = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
425 + pTime->u16YearDay - 1;
426 AssertMsgReturn(i32Days <= RTTIME_MAX_DAY && i32Days >= RTTIME_MIN_DAY, ("%RI32\n", i32Days), NULL);
427
428 u32Secs = pTime->u8Second
429 + pTime->u8Minute * 60
430 + pTime->u8Hour * 3600;
431 i64Nanos = (uint64_t)pTime->u32Nanosecond
432 + u32Secs * UINT64_C(1000000000);
433 AssertMsgReturn(i32Days != RTTIME_MAX_DAY || i64Nanos <= RTTIME_MAX_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
434 AssertMsgReturn(i32Days != RTTIME_MIN_DAY || i64Nanos >= RTTIME_MIN_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
435
436 i64Nanos += i32Days * UINT64_C(86400000000000);
437 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL)
438 i64Nanos -= pTime->offUTC * RT_NS_1MIN;
439
440 pTimeSpec->i64NanosecondsRelativeToUnixEpoch = i64Nanos;
441 return pTimeSpec;
442}
443RT_EXPORT_SYMBOL(RTTimeImplode);
444
445
446/**
447 * Internal worker for RTTimeNormalize and RTTimeLocalNormalize.
448 */
449static PRTTIME rtTimeNormalizeInternal(PRTTIME pTime)
450{
451 unsigned uSecond;
452 unsigned uMinute;
453 unsigned uHour;
454 bool fLeapYear;
455
456 /*
457 * Fix the YearDay and Month/MonthDay.
458 */
459 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
460 if (!pTime->u16YearDay)
461 {
462 /*
463 * The Month+MonthDay must present, overflow adjust them and calc the year day.
464 */
465 AssertMsgReturn( pTime->u8Month
466 && pTime->u8MonthDay,
467 ("date=%d-%d-%d\n", pTime->i32Year, pTime->u8Month, pTime->u8MonthDay),
468 NULL);
469 while (pTime->u8Month > 12)
470 {
471 pTime->u8Month -= 12;
472 pTime->i32Year++;
473 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
474 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
475 }
476
477 for (;;)
478 {
479 unsigned cDaysInMonth = fLeapYear
480 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
481 : g_acDaysInMonths[pTime->u8Month - 1];
482 if (pTime->u8MonthDay <= cDaysInMonth)
483 break;
484 pTime->u8MonthDay -= cDaysInMonth;
485 if (pTime->u8Month != 12)
486 pTime->u8Month++;
487 else
488 {
489 pTime->u8Month = 1;
490 pTime->i32Year++;
491 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
492 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
493 }
494 }
495
496 pTime->u16YearDay = pTime->u8MonthDay - 1
497 + (fLeapYear
498 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
499 : g_aiDayOfYear[pTime->u8Month - 1]);
500 }
501 else
502 {
503 /*
504 * Are both YearDay and Month/MonthDay valid?
505 * Check that they don't overflow and match, if not use YearDay (simpler).
506 */
507 bool fRecalc = true;
508 if ( pTime->u8Month
509 && pTime->u8MonthDay)
510 {
511 do
512 {
513 uint16_t u16YearDay;
514
515 /* If you change one, zero the other to make clear what you mean. */
516 AssertBreak(pTime->u8Month <= 12);
517 AssertBreak(pTime->u8MonthDay <= (fLeapYear
518 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
519 : g_acDaysInMonths[pTime->u8Month - 1]));
520 u16YearDay = pTime->u8MonthDay - 1
521 + (fLeapYear
522 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
523 : g_aiDayOfYear[pTime->u8Month - 1]);
524 AssertBreak(u16YearDay == pTime->u16YearDay);
525 fRecalc = false;
526 } while (0);
527 }
528 if (fRecalc)
529 {
530 const uint16_t *paiDayOfYear;
531
532 /* overflow adjust YearDay */
533 while (pTime->u16YearDay > (fLeapYear ? 366 : 365))
534 {
535 pTime->u16YearDay -= fLeapYear ? 366 : 365;
536 pTime->i32Year++;
537 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
538 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
539 }
540
541 /* calc Month and MonthDay */
542 paiDayOfYear = fLeapYear
543 ? &g_aiDayOfYearLeap[0]
544 : &g_aiDayOfYear[0];
545 pTime->u8Month = 1;
546 while (pTime->u16YearDay >= paiDayOfYear[pTime->u8Month])
547 pTime->u8Month++;
548 Assert(pTime->u8Month >= 1 && pTime->u8Month <= 12);
549 pTime->u8MonthDay = pTime->u16YearDay - paiDayOfYear[pTime->u8Month - 1] + 1;
550 }
551 }
552
553 /*
554 * Fixup time overflows.
555 * Use unsigned int values internally to avoid overflows.
556 */
557 uSecond = pTime->u8Second;
558 uMinute = pTime->u8Minute;
559 uHour = pTime->u8Hour;
560
561 while (pTime->u32Nanosecond >= 1000000000)
562 {
563 pTime->u32Nanosecond -= 1000000000;
564 uSecond++;
565 }
566
567 while (uSecond >= 60)
568 {
569 uSecond -= 60;
570 uMinute++;
571 }
572
573 while (uMinute >= 60)
574 {
575 uMinute -= 60;
576 uHour++;
577 }
578
579 while (uHour >= 24)
580 {
581 uHour -= 24;
582
583 /* This is really a RTTimeIncDay kind of thing... */
584 if (pTime->u16YearDay + 1 != (fLeapYear ? g_aiDayOfYearLeap[pTime->u8Month] : g_aiDayOfYear[pTime->u8Month]))
585 {
586 pTime->u16YearDay++;
587 pTime->u8MonthDay++;
588 }
589 else if (pTime->u8Month != 12)
590 {
591 pTime->u16YearDay++;
592 pTime->u8Month++;
593 pTime->u8MonthDay = 1;
594 }
595 else
596 {
597 pTime->i32Year++;
598 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
599 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
600 pTime->u16YearDay = 1;
601 pTime->u8Month = 1;
602 pTime->u8MonthDay = 1;
603 }
604 }
605
606 pTime->u8Second = uSecond;
607 pTime->u8Minute = uMinute;
608 pTime->u8Hour = uHour;
609
610 /*
611 * Correct the leap year flag.
612 * Assert if it's wrong, but ignore if unset.
613 */
614 if (fLeapYear)
615 {
616 Assert(!(pTime->fFlags & RTTIME_FLAGS_COMMON_YEAR));
617 pTime->fFlags &= ~RTTIME_FLAGS_COMMON_YEAR;
618 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
619 }
620 else
621 {
622 Assert(!(pTime->fFlags & RTTIME_FLAGS_LEAP_YEAR));
623 pTime->fFlags &= ~RTTIME_FLAGS_LEAP_YEAR;
624 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
625 }
626
627
628 /*
629 * Calc week day.
630 *
631 * 1970-01-01 was a Thursday (3), so find the number of days relative to
632 * that point. We use the table when possible and a slow+stupid+brute-force
633 * algorithm for points outside it. Feel free to optimize the latter by
634 * using some clever formula.
635 */
636 if ( pTime->i32Year >= OFF_YEAR_IDX_0_YEAR
637 && pTime->i32Year < OFF_YEAR_IDX_0_YEAR + (int32_t)RT_ELEMENTS(g_aoffYear))
638 {
639 int32_t offDays = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
640 + pTime->u16YearDay -1;
641 pTime->u8WeekDay = ((offDays % 7) + 3 + 7) % 7;
642 }
643 else
644 {
645 int32_t i32Year = pTime->i32Year;
646 if (i32Year >= 1970)
647 {
648 uint64_t offDays = pTime->u16YearDay - 1;
649 while (--i32Year >= 1970)
650 offDays += rtTimeIsLeapYear(i32Year) ? 366 : 365;
651 pTime->u8WeekDay = (uint8_t)((offDays + 3) % 7);
652 }
653 else
654 {
655 int64_t offDays = (fLeapYear ? -366 - 1 : -365 - 1) + pTime->u16YearDay;
656 while (++i32Year < 1970)
657 offDays -= rtTimeIsLeapYear(i32Year) ? 366 : 365;
658 pTime->u8WeekDay = ((int)(offDays % 7) + 3 + 7) % 7;
659 }
660 }
661 return pTime;
662}
663
664
665/**
666 * Normalizes the fields of a time structure.
667 *
668 * It is possible to calculate year-day from month/day and vice
669 * versa. If you adjust any of these, make sure to zero the
670 * other so you make it clear which of the fields to use. If
671 * it's ambiguous, the year-day field is used (and you get
672 * assertions in debug builds).
673 *
674 * All the time fields and the year-day or month/day fields will
675 * be adjusted for overflows. (Since all fields are unsigned, there
676 * is no underflows.) It is possible to exploit this for simple
677 * date math, though the recommended way of doing that to implode
678 * the time into a timespec and do the math on that.
679 *
680 * @returns pTime on success.
681 * @returns NULL if the data is invalid.
682 *
683 * @param pTime The time structure to normalize.
684 *
685 * @remarks This function doesn't work with local time, only with UTC time.
686 */
687RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime)
688{
689 /*
690 * Validate that we've got the minimum of stuff handy.
691 */
692 AssertReturn(VALID_PTR(pTime), NULL);
693 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
694 AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_LOCAL, ("Use RTTimeLocalNormalize!\n"), NULL);
695 AssertMsgReturn(pTime->offUTC == 0, ("%d; Use RTTimeLocalNormalize!\n", pTime->offUTC), NULL);
696
697 pTime = rtTimeNormalizeInternal(pTime);
698 if (pTime)
699 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
700 return pTime;
701}
702RT_EXPORT_SYMBOL(RTTimeNormalize);
703
704
705/**
706 * Normalizes the fields of a time structure, assuming local time.
707 *
708 * It is possible to calculate year-day from month/day and vice
709 * versa. If you adjust any of these, make sure to zero the
710 * other so you make it clear which of the fields to use. If
711 * it's ambiguous, the year-day field is used (and you get
712 * assertions in debug builds).
713 *
714 * All the time fields and the year-day or month/day fields will
715 * be adjusted for overflows. (Since all fields are unsigned, there
716 * is no underflows.) It is possible to exploit this for simple
717 * date math, though the recommended way of doing that to implode
718 * the time into a timespec and do the math on that.
719 *
720 * @returns pTime on success.
721 * @returns NULL if the data is invalid.
722 *
723 * @param pTime The time structure to normalize.
724 *
725 * @remarks This function doesn't work with UTC time, only with local time.
726 */
727RTDECL(PRTTIME) RTTimeLocalNormalize(PRTTIME pTime)
728{
729 /*
730 * Validate that we've got the minimum of stuff handy.
731 */
732 AssertReturn(VALID_PTR(pTime), NULL);
733 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
734 AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC, ("Use RTTimeNormalize!\n"), NULL);
735
736 pTime = rtTimeNormalizeInternal(pTime);
737 if (pTime)
738 pTime->fFlags |= RTTIME_FLAGS_TYPE_LOCAL;
739 return pTime;
740}
741RT_EXPORT_SYMBOL(RTTimeLocalNormalize);
742
743
744/**
745 * Converts a time spec to a ISO date string.
746 *
747 * @returns psz on success.
748 * @returns NULL on buffer underflow.
749 * @param pTime The time. Caller should've normalized this.
750 * @param psz Where to store the string.
751 * @param cb The size of the buffer.
752 */
753RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb)
754{
755 size_t cch;
756
757 /* (Default to UTC if not specified) */
758 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
759 && pTime->offUTC)
760 {
761 int32_t offUTC = pTime->offUTC;
762 Assert(offUTC <= 840 && offUTC >= -840);
763 char chSign;
764 if (offUTC >= 0)
765 chSign = '+';
766 else
767 {
768 chSign = '-';
769 offUTC = -offUTC;
770 }
771 uint32_t offUTCHour = (uint32_t)offUTC / 60;
772 uint32_t offUTCMinute = (uint32_t)offUTC % 60;
773 cch = RTStrPrintf(psz, cb,
774 "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32%c%02d%:02d",
775 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
776 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond,
777 chSign, offUTCHour, offUTCMinute);
778 if ( cch <= 15
779 || psz[cch - 6] != chSign)
780 return NULL;
781 }
782 else
783 {
784 cch = RTStrPrintf(psz, cb, "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32Z",
785 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
786 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond);
787 if ( cch <= 15
788 || psz[cch - 1] != 'Z')
789 return NULL;
790 }
791 return psz;
792}
793RT_EXPORT_SYMBOL(RTTimeToString);
794
795
796/**
797 * Converts a time spec to a ISO date string.
798 *
799 * @returns psz on success.
800 * @returns NULL on buffer underflow.
801 * @param pTime The time spec.
802 * @param psz Where to store the string.
803 * @param cb The size of the buffer.
804 */
805RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb)
806{
807 RTTIME Time;
808 return RTTimeToString(RTTimeExplode(&Time, pTime), psz, cb);
809}
810RT_EXPORT_SYMBOL(RTTimeSpecToString);
811
812
813
814/**
815 * Attempts to convert an ISO date string to a time structure.
816 *
817 * We're a little forgiving with zero padding, unspecified parts, and leading
818 * and trailing spaces.
819 *
820 * @retval pTime on success,
821 * @retval NULL on failure.
822 * @param pTime Where to store the time on success.
823 * @param pszString The ISO date string to convert.
824 */
825RTDECL(PRTTIME) RTTimeFromString(PRTTIME pTime, const char *pszString)
826{
827 /* Ignore leading spaces. */
828 while (RT_C_IS_SPACE(*pszString))
829 pszString++;
830
831 /*
832 * Init non date & time parts.
833 */
834 pTime->fFlags = RTTIME_FLAGS_TYPE_LOCAL;
835 pTime->offUTC = 0;
836
837 /*
838 * The day part.
839 */
840
841 /* Year */
842 int rc = RTStrToInt32Ex(pszString, (char **)&pszString, 10, &pTime->i32Year);
843 if (rc != VWRN_TRAILING_CHARS)
844 return NULL;
845
846 bool const fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
847 if (fLeapYear)
848 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
849
850 if (*pszString++ != '-')
851 return NULL;
852
853 /* Month of the year. */
854 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Month);
855 if (rc != VWRN_TRAILING_CHARS)
856 return NULL;
857 if (pTime->u8Month == 0 || pTime->u8Month > 12)
858 return NULL;
859 if (*pszString++ != '-')
860 return NULL;
861
862 /* Day of month.*/
863 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8MonthDay);
864 if (rc != VWRN_TRAILING_CHARS && rc != VINF_SUCCESS)
865 return NULL;
866 unsigned const cDaysInMonth = fLeapYear
867 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
868 : g_acDaysInMonths[pTime->u8Month - 1];
869 if (pTime->u8MonthDay == 0 || pTime->u8MonthDay > cDaysInMonth)
870 return NULL;
871
872 /* Calculate year day. */
873 pTime->u16YearDay = pTime->u8MonthDay - 1
874 + (fLeapYear
875 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
876 : g_aiDayOfYear[pTime->u8Month - 1]);
877
878 /*
879 * The time part.
880 */
881 if (*pszString++ != 'T')
882 return NULL;
883
884 /* Hour. */
885 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Hour);
886 if (rc != VWRN_TRAILING_CHARS)
887 return NULL;
888 if (pTime->u8Hour > 23)
889 return NULL;
890 if (*pszString++ != ':')
891 return NULL;
892
893 /* Minute. */
894 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Minute);
895 if (rc != VWRN_TRAILING_CHARS)
896 return NULL;
897 if (pTime->u8Minute > 59)
898 return NULL;
899 if (*pszString++ != ':')
900 return NULL;
901
902 /* Second. */
903 rc = RTStrToUInt8Ex(pszString, (char **)&pszString, 10, &pTime->u8Second);
904 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
905 return NULL;
906 if (pTime->u8Second > 59)
907 return NULL;
908
909 /* Nanoseconds is optional and probably non-standard. */
910 if (*pszString == '.')
911 {
912 rc = RTStrToUInt32Ex(pszString + 1, (char **)&pszString, 10, &pTime->u32Nanosecond);
913 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
914 return NULL;
915 if (pTime->u32Nanosecond >= 1000000000)
916 return NULL;
917 }
918 else
919 pTime->u32Nanosecond = 0;
920
921 /*
922 * Time zone.
923 */
924 if (*pszString == 'Z')
925 {
926 pszString++;
927 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
928 pTime->fFlags |= ~RTTIME_FLAGS_TYPE_UTC;
929 pTime->offUTC = 0;
930 }
931 else if ( *pszString == '+'
932 || *pszString == '-')
933 {
934 int8_t cUtcHours = 0;
935 rc = RTStrToInt8Ex(pszString, (char **)&pszString, 10, &cUtcHours);
936 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS && rc != VWRN_TRAILING_SPACES)
937 return NULL;
938 uint8_t cUtcMin = 0;
939 if (*pszString == ':')
940 {
941 rc = RTStrToUInt8Ex(pszString + 1, (char **)&pszString, 10, &cUtcMin);
942 if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_SPACES)
943 return NULL;
944 }
945 else if (*pszString && !RT_C_IS_BLANK(*pszString))
946 return NULL;
947 if (cUtcHours >= 0)
948 pTime->offUTC = cUtcHours * 60 + cUtcMin;
949 else
950 pTime->offUTC = cUtcHours * 60 - cUtcMin;
951 if (RT_ABS(pTime->offUTC) > 840)
952 return NULL;
953 }
954 /* else: No time zone given, local with offUTC = 0. */
955
956 /*
957 * The rest of the string should be blanks.
958 */
959 char ch;
960 while ((ch = *pszString++) != '\0')
961 if (!RT_C_IS_BLANK(ch))
962 return NULL;
963
964 return pTime;
965}
966RT_EXPORT_SYMBOL(RTTimeFromString);
967
968
969/**
970 * Attempts to convert an ISO date string to a time structure.
971 *
972 * We're a little forgiving with zero padding, unspecified parts, and leading
973 * and trailing spaces.
974 *
975 * @retval pTime on success,
976 * @retval NULL on failure.
977 * @param pTime The time spec.
978 * @param pszString The ISO date string to convert.
979 */
980RTDECL(PRTTIMESPEC) RTTimeSpecFromString(PRTTIMESPEC pTime, const char *pszString)
981{
982 RTTIME Time;
983 if (RTTimeFromString(&Time, pszString))
984 return RTTimeImplode(pTime, &Time);
985 return NULL;
986}
987RT_EXPORT_SYMBOL(RTTimeSpecFromString);
988
989
990/**
991 * Adds one day to @a pTime.
992 *
993 * ASSUMES it is zulu time so DST can be ignored.
994 */
995static PRTTIME rtTimeAdd1Day(PRTTIME pTime)
996{
997 Assert(!pTime->offUTC);
998 rtTimeNormalizeInternal(pTime);
999 pTime->u8MonthDay += 1;
1000 pTime->u16YearDay = 0;
1001 return rtTimeNormalizeInternal(pTime);
1002}
1003
1004
1005/**
1006 * Subtracts one day from @a pTime.
1007 *
1008 * ASSUMES it is zulu time so DST can be ignored.
1009 */
1010static PRTTIME rtTimeSub1Day(PRTTIME pTime)
1011{
1012 Assert(!pTime->offUTC);
1013 rtTimeNormalizeInternal(pTime);
1014 if (pTime->u16YearDay > 1)
1015 {
1016 pTime->u16YearDay -= 1;
1017 pTime->u8Month = 0;
1018 pTime->u8MonthDay = 0;
1019 }
1020 else
1021 {
1022 pTime->i32Year -= 1;
1023 pTime->u16YearDay = rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365;
1024 pTime->u8MonthDay = 31;
1025 pTime->u8Month = 12;
1026 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
1027 }
1028 return rtTimeNormalizeInternal(pTime);
1029}
1030
1031
1032/**
1033 * Adds a signed number of minutes to @a pTime.
1034 *
1035 * ASSUMES it is zulu time so DST can be ignored.
1036 *
1037 * @param pTime The time structure to work on.
1038 * @param cAddend Number of minutes to add.
1039 * ASSUMES the value isn't all that high!
1040 */
1041static PRTTIME rtTimeAddMinutes(PRTTIME pTime, int32_t cAddend)
1042{
1043 Assert(RT_ABS(cAddend) < 31 * 24 * 60);
1044
1045 /*
1046 * Work on minutes of the day.
1047 */
1048 int32_t const cMinutesInDay = 24 * 60;
1049 int32_t iDayMinute = (unsigned)pTime->u8Hour * 60 + pTime->u8Minute;
1050 iDayMinute += cAddend;
1051
1052 while (iDayMinute >= cMinutesInDay)
1053 {
1054 rtTimeAdd1Day(pTime);
1055 iDayMinute -= cMinutesInDay;
1056 }
1057
1058 while (iDayMinute < 0)
1059 {
1060 rtTimeSub1Day(pTime);
1061 iDayMinute += cMinutesInDay;
1062 }
1063
1064 pTime->u8Hour = iDayMinute / 60;
1065 pTime->u8Minute = iDayMinute % 60;
1066
1067 return pTime;
1068}
1069
1070
1071/**
1072 * Converts @a pTime to zulu time (UTC) if needed.
1073 *
1074 * @returns pTime.
1075 * @param pTime What to convert (in/out).
1076 */
1077static PRTTIME rtTimeConvertToZulu(PRTTIME pTime)
1078{
1079 RTTIME_ASSERT_NORMALIZED(pTime);
1080 if ((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_UTC)
1081 {
1082 int32_t offUTC = pTime->offUTC;
1083 pTime->offUTC = 0;
1084 pTime->fFlags &= ~RTTIME_FLAGS_TYPE_MASK;
1085 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
1086 if (offUTC != 0)
1087 rtTimeAddMinutes(pTime, -offUTC);
1088 }
1089 return pTime;
1090}
1091
1092
1093/**
1094 * Converts a time structure to UTC, relying on UTC offset information if it contains local time.
1095 *
1096 * @returns pTime on success.
1097 * @returns NULL if the data is invalid.
1098 * @param pTime The time structure to convert.
1099 */
1100RTDECL(PRTTIME) RTTimeConvertToZulu(PRTTIME pTime)
1101{
1102 /*
1103 * Validate that we've got the minimum of stuff handy.
1104 */
1105 AssertReturn(VALID_PTR(pTime), NULL);
1106 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
1107
1108 return rtTimeConvertToZulu(rtTimeNormalizeInternal(pTime));
1109}
1110RT_EXPORT_SYMBOL(RTTimeConvertToZulu);
1111
1112
1113/**
1114 * Compares two normalized time structures.
1115 *
1116 * @retval 0 if equal.
1117 * @retval -1 if @a pLeft is earlier than @a pRight.
1118 * @retval 1 if @a pRight is earlier than @a pLeft.
1119 *
1120 * @param pLeft The left side time. NULL is accepted.
1121 * @param pRight The right side time. NULL is accepted.
1122 *
1123 * @note A NULL time is considered smaller than anything else. If both are
1124 * NULL, they are considered equal.
1125 */
1126RTDECL(int) RTTimeCompare(PCRTTIME pLeft, PCRTTIME pRight)
1127{
1128#ifdef RT_STRICT
1129 if (pLeft)
1130 RTTIME_ASSERT_NORMALIZED(pLeft);
1131 if (pRight)
1132 RTTIME_ASSERT_NORMALIZED(pRight);
1133#endif
1134
1135 int iRet;
1136 if (pLeft)
1137 {
1138 if (pRight)
1139 {
1140 /*
1141 * Only work with normalized zulu time.
1142 */
1143 RTTIME TmpLeft;
1144 if ( pLeft->offUTC != 0
1145 || pLeft->u16YearDay == 0
1146 || pLeft->u16YearDay > 366
1147 || pLeft->u8Hour >= 60
1148 || pLeft->u8Minute >= 60
1149 || pLeft->u8Second >= 60)
1150 {
1151 TmpLeft = *pLeft;
1152 pLeft = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpLeft));
1153 }
1154
1155 RTTIME TmpRight;
1156 if ( pRight->offUTC != 0
1157 || pRight->u16YearDay == 0
1158 || pRight->u16YearDay > 366
1159 || pRight->u8Hour >= 60
1160 || pRight->u8Minute >= 60
1161 || pRight->u8Second >= 60)
1162 {
1163 TmpRight = *pRight;
1164 pRight = rtTimeConvertToZulu(rtTimeNormalizeInternal(&TmpRight));
1165 }
1166
1167 /*
1168 * Do the comparison.
1169 */
1170 if ( pLeft->i32Year != pRight->i32Year)
1171 iRet = pLeft->i32Year < pRight->i32Year ? -1 : 1;
1172 else if ( pLeft->u16YearDay != pRight->u16YearDay)
1173 iRet = pLeft->u16YearDay < pRight->u16YearDay ? -1 : 1;
1174 else if ( pLeft->u8Hour != pRight->u8Hour)
1175 iRet = pLeft->u8Hour < pRight->u8Hour ? -1 : 1;
1176 else if ( pLeft->u8Minute != pRight->u8Minute)
1177 iRet = pLeft->u8Minute < pRight->u8Minute ? -1 : 1;
1178 else if ( pLeft->u8Second != pRight->u8Second)
1179 iRet = pLeft->u8Second < pRight->u8Second ? -1 : 1;
1180 else if ( pLeft->u32Nanosecond != pRight->u32Nanosecond)
1181 iRet = pLeft->u32Nanosecond < pRight->u32Nanosecond ? -1 : 1;
1182 else
1183 iRet = 0;
1184 }
1185 else
1186 iRet = 1;
1187 }
1188 else
1189 iRet = pRight ? -1 : 0;
1190 return iRet;
1191}
1192RT_EXPORT_SYMBOL(RTTimeCompare);
1193
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