VirtualBox

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

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

Runtime: eliminate unnecessary forward declaration.

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