VirtualBox

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

Last change on this file was 99758, checked in by vboxsync, 13 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

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

© 2023 Oracle
ContactPrivacy policyTerms of Use