VirtualBox

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

Last change on this file since 21107 was 19546, checked in by vboxsync, 16 years ago

space

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 30.4 KB
Line 
1/* $Id: time.cpp 19546 2009-05-08 20:39:07Z vboxsync $ */
2/** @file
3 * IPRT - Time.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_TIME
36#include <iprt/time.h>
37#include <iprt/string.h>
38#include <iprt/assert.h>
39#include "internal/time.h"
40
41
42/*******************************************************************************
43* Defined Constants And Macros *
44*******************************************************************************/
45/** The max year we possibly could implode. */
46#define RTTIME_MAX_YEAR (292 + 1970)
47/** The min year we possibly could implode. */
48#define RTTIME_MIN_YEAR (-293 + 1970)
49
50/** The max day supported by our time representation. (2262-04-11T23-47-16.854775807) */
51#define RTTIME_MAX_DAY (365*292+71 + 101-1)
52/** The min day supported by our time representation. (1677-09-21T00-12-43.145224192) */
53#define RTTIME_MIN_DAY (365*-293-70 + 264-1)
54
55/** The max nano second into the max day. (2262-04-11T23-47-16.854775807) */
56#define RTTIME_MAX_DAY_NANO ( INT64_C(1000000000) * (23*3600 + 47*60 + 16) + 854775807 )
57/** The min nano second into the min day. (1677-09-21T00-12-43.145224192) */
58#define RTTIME_MIN_DAY_NANO ( INT64_C(1000000000) * (00*3600 + 12*60 + 43) + 145224192 )
59
60
61/*******************************************************************************
62* Global Variables *
63*******************************************************************************/
64/**
65 * Days per month in a common year.
66 */
67static const uint8_t g_acDaysInMonths[12] =
68{
69 /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */
70 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
71};
72
73/**
74 * Days per month in a leap year.
75 */
76static const uint8_t g_acDaysInMonthsLeap[12] =
77{
78 /*Jan Feb Mar Arp May Jun Jul Aug Sep Oct Nov Dec */
79 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
80};
81
82/**
83 * The day of year for each month in a common year.
84 */
85static const uint16_t g_aiDayOfYear[12 + 1] =
86{
87 1, /* Jan */
88 1+31, /* Feb */
89 1+31+28, /* Mar */
90 1+31+28+31, /* Apr */
91 1+31+28+31+30, /* May */
92 1+31+28+31+30+31, /* Jun */
93 1+31+28+31+30+31+30, /* Jul */
94 1+31+28+31+30+31+30+31, /* Aug */
95 1+31+28+31+30+31+30+31+31, /* Sep */
96 1+31+28+31+30+31+30+31+31+30, /* Oct */
97 1+31+28+31+30+31+30+31+31+30+31, /* Nov */
98 1+31+28+31+30+31+30+31+31+30+31+30, /* Dec */
99 1+31+28+31+30+31+30+31+31+30+31+30+31
100};
101
102/**
103 * The day of year for each month in a leap year.
104 */
105static const uint16_t g_aiDayOfYearLeap[12 + 1] =
106{
107 1, /* Jan */
108 1+31, /* Feb */
109 1+31+29, /* Mar */
110 1+31+29+31, /* Apr */
111 1+31+29+31+30, /* May */
112 1+31+29+31+30+31, /* Jun */
113 1+31+29+31+30+31+30, /* Jul */
114 1+31+29+31+30+31+30+31, /* Aug */
115 1+31+29+31+30+31+30+31+31, /* Sep */
116 1+31+29+31+30+31+30+31+31+30, /* Oct */
117 1+31+29+31+30+31+30+31+31+30+31, /* Nov */
118 1+31+29+31+30+31+30+31+31+30+31+30, /* Dec */
119 1+31+29+31+30+31+30+31+31+30+31+30+31
120};
121
122/** The index of 1970 in g_aoffYear */
123#define OFF_YEAR_IDX_EPOCH 300
124/** The year of the first index. */
125#define OFF_YEAR_IDX_0_YEAR 1670
126
127/**
128 * The number of days the 1st of january a year is offseted from 1970-01-01.
129 */
130static const int32_t g_aoffYear[] =
131{
132/*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,
133/*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,
134/*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,
135/*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,
136/*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,
137/*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,
138/*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,
139/*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,
140/*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,
141/*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,
142/*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,
143/*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,
144/*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,
145/*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,
146/*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,
147/*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,
148/*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,
149/*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,
150/*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,
151/*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,
152/*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,
153/*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,
154/*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,
155/*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,
156/*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,
157/*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,
158/*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 ,
159/*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 ,
160/*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 ,
161/*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 ,
162/*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 ,
163/*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 ,
164/*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 ,
165/*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 ,
166/*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 ,
167/*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 ,
168/*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 ,
169/*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 ,
170/*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 ,
171/*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 ,
172/*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 ,
173/*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 ,
174/*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 ,
175/*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 ,
176/*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 ,
177/*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 ,
178/*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 ,
179/*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 ,
180/*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 ,
181/*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 ,
182/*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 ,
183/*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 ,
184/*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 ,
185/*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 ,
186/*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 ,
187/*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 ,
188/*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 ,
189/*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 ,
190/*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 ,
191/*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
192};
193
194/* generator code:
195#include <stdio.h>
196bool isLeapYear(int iYear)
197{
198 return iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0);
199}
200void printYear(int iYear, int iLeap)
201{
202 if (!(iYear % 10))
203 printf("\n/" "*%d:*" "/", iYear + 1970);
204 printf(" 365*%4d+%-3d,", iYear, iLeap);
205}
206int main()
207{
208 int iYear = 0;
209 int iLeap = 0;
210 while (iYear > -300)
211 iLeap -= isLeapYear(1970 + --iYear);
212 while (iYear < 300)
213 {
214 printYear(iYear, iLeap);
215 iLeap += isLeapYear(1970 + iYear++);
216 }
217 printf("\n");
218 return 0;
219}
220*/
221
222
223/**
224 * Checks if a year is a leap year or not.
225 *
226 * @returns true if it's a leap year.
227 * @returns false if it's a common year.
228 * @param i32Year The year in question.
229 */
230DECLINLINE(bool) rtTimeIsLeapYear(int32_t i32Year)
231{
232 return i32Year % 4 == 0
233 && ( i32Year % 100 != 0
234 || i32Year % 400 == 0);
235}
236
237
238/**
239 * Checks if a year is a leap year or not.
240 *
241 * @returns true if it's a leap year.
242 * @returns false if it's a common year.
243 * @param i32Year The year in question.
244 */
245RTDECL(bool) RTTimeIsLeapYear(int32_t i32Year)
246{
247 return rtTimeIsLeapYear(i32Year);
248}
249
250
251/**
252 * Explodes a time spec (UTC).
253 *
254 * @returns pTime.
255 * @param pTime Where to store the exploded time.
256 * @param pTimeSpec The time spec to exploded.
257 */
258RTDECL(PRTTIME) RTTimeExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
259{
260 AssertMsg(VALID_PTR(pTime), ("%p\n", pTime));
261 AssertMsg(VALID_PTR(pTimeSpec), ("%p\n", pTime));
262
263 /*
264 * The simple stuff first.
265 */
266 pTime->fFlags = RTTIME_FLAGS_TYPE_UTC;
267 int64_t i64Div = pTimeSpec->i64NanosecondsRelativeToUnixEpoch;
268 int32_t i32Rem = (int32_t)(i64Div % 1000000000);
269 i64Div /= 1000000000;
270 if (i32Rem < 0)
271 {
272 i32Rem += 1000000000;
273 i64Div--;
274 }
275 pTime->u32Nanosecond = i32Rem;
276
277 /* second */
278 i32Rem = (int32_t)(i64Div % 60);
279 i64Div /= 60;
280 if (i32Rem < 0)
281 {
282 i32Rem += 60;
283 i64Div--;
284 }
285 pTime->u8Second = i32Rem;
286
287 /* minute */
288 int32_t i32Div = (int32_t)i64Div; /* 60,000,000,000 > 33bit, so 31bit suffices. */
289 i32Rem = i32Div % 60;
290 i32Div /= 60;
291 if (i32Rem < 0)
292 {
293 i32Rem += 60;
294 i32Div--;
295 }
296 pTime->u8Minute = i32Rem;
297
298 /* hour */
299 i32Rem = i32Div % 24;
300 i32Div /= 24; /* days relative to 1970-01-01 */
301 if (i32Rem < 0)
302 {
303 i32Rem += 24;
304 i32Div--;
305 }
306 pTime->u8Hour = i32Rem;
307
308 /* weekday - 1970-01-01 was a Thursday (3) */
309 pTime->u8WeekDay = ((int)(i32Div % 7) + 3 + 7) % 7;
310
311 /*
312 * We've now got a number of days relative to 1970-01-01.
313 * To get the correct year number we have to mess with leap years. Fortunatly,
314 * the represenation we've got only supports a few hundred years, so we can
315 * generate a table and perform a simple two way search from the modulus 365 derived.
316 */
317 unsigned iYear = OFF_YEAR_IDX_EPOCH + i32Div / 365;
318 while (g_aoffYear[iYear + 1] <= i32Div)
319 iYear++;
320 while (g_aoffYear[iYear] > i32Div)
321 iYear--;
322 pTime->i32Year = iYear + OFF_YEAR_IDX_0_YEAR;
323 i32Div -= g_aoffYear[iYear];
324 pTime->u16YearDay = i32Div + 1;
325
326 /*
327 * Figuring out the month is done in a manner similar to the year, only here we
328 * ensure that the index is matching or too small.
329 */
330 const uint16_t *paiDayOfYear;
331 if (rtTimeIsLeapYear(pTime->i32Year))
332 {
333 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
334 paiDayOfYear = &g_aiDayOfYearLeap[0];
335 }
336 else
337 {
338 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
339 paiDayOfYear = &g_aiDayOfYear[0];
340 }
341 int iMonth = i32Div / 32;
342 i32Div++;
343 while (paiDayOfYear[iMonth + 1] <= i32Div)
344 iMonth++;
345 pTime->u8Month = iMonth + 1;
346 i32Div -= paiDayOfYear[iMonth];
347 pTime->u8MonthDay = i32Div + 1;
348
349 /* This is for UTC timespecs, so, no offset. */
350 pTime->offUTC = 0;
351
352 return pTime;
353}
354
355
356/**
357 * Implodes exploded time to a time spec (UTC).
358 *
359 * @returns pTime on success.
360 * @returns NULL if the pTime data is invalid.
361 * @param pTimeSpec Where to store the imploded UTC time.
362 * If pTime specifies a time which outside the range, maximum or
363 * minimum values will be returned.
364 * @param pTime Pointer to the exploded time to implode.
365 * The fields u8Month, u8WeekDay and u8MonthDay are not used,
366 * and all the other fields are expected to be within their
367 * bounds. Use RTTimeNormalize() to calculate u16YearDay and
368 * normalize the ranges of the fields.
369 */
370RTDECL(PRTTIMESPEC) RTTimeImplode(PRTTIMESPEC pTimeSpec, PCRTTIME pTime)
371{
372 /*
373 * Validate input.
374 */
375 AssertReturn(VALID_PTR(pTimeSpec), NULL);
376 AssertReturn(VALID_PTR(pTime), NULL);
377 AssertReturn(pTime->u32Nanosecond < 1000000000, NULL);
378 AssertReturn(pTime->u8Second < 60, NULL);
379 AssertReturn(pTime->u8Minute < 60, NULL);
380 AssertReturn(pTime->u8Hour < 24, NULL);
381 AssertReturn(pTime->u16YearDay >= 1, NULL);
382 AssertReturn(pTime->u16YearDay <= (rtTimeIsLeapYear(pTime->i32Year) ? 366 : 365), NULL);
383 AssertMsgReturn(pTime->i32Year <= RTTIME_MAX_YEAR && pTime->i32Year >= RTTIME_MIN_YEAR, ("%RI32\n", pTime->i32Year), NULL);
384
385 /*
386 * Do the conversion to nanoseconds.
387 */
388 int32_t i32Days = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
389 + pTime->u16YearDay - 1;
390 AssertMsgReturn(i32Days <= RTTIME_MAX_DAY && i32Days >= RTTIME_MIN_DAY, ("%RI32\n", i32Days), NULL);
391
392 uint32_t u32Secs = pTime->u8Second
393 + pTime->u8Minute * 60
394 + pTime->u8Hour * 3600;
395 int64_t i64Nanos = (uint64_t)pTime->u32Nanosecond
396 + u32Secs * UINT64_C(1000000000);
397 AssertMsgReturn(i32Days != RTTIME_MAX_DAY || i64Nanos <= RTTIME_MAX_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
398 AssertMsgReturn(i32Days != RTTIME_MIN_DAY || i64Nanos >= RTTIME_MIN_DAY_NANO, ("%RI64\n", i64Nanos), NULL);
399
400 i64Nanos += i32Days * UINT64_C(86400000000000);
401
402 pTimeSpec->i64NanosecondsRelativeToUnixEpoch = i64Nanos;
403 return pTimeSpec;
404}
405
406
407/**
408 * Internal worker for RTTimeNormalize and RTTimeLocalNormalize.
409 * It doesn't adjust the UCT offset but leaves that for RTTimeLocalNormalize.
410 */
411PRTTIME rtTimeNormalizeInternal(PRTTIME pTime)
412{
413 /*
414 * Fix the YearDay and Month/MonthDay.
415 */
416 bool fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
417 if (!pTime->u16YearDay)
418 {
419 /*
420 * The Month+MonthDay must present, overflow adjust them and calc the year day.
421 */
422 AssertMsgReturn( pTime->u8Month
423 && pTime->u8MonthDay,
424 ("date=%d-%d-%d\n", pTime->i32Year, pTime->u8Month, pTime->u8MonthDay),
425 NULL);
426 while (pTime->u8Month > 12)
427 {
428 pTime->u8Month -= 12;
429 pTime->i32Year++;
430 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
431 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
432 }
433
434 for (;;)
435 {
436 unsigned cDaysInMonth = fLeapYear
437 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
438 : g_acDaysInMonthsLeap[pTime->u8Month - 1];
439 if (pTime->u8MonthDay <= cDaysInMonth)
440 break;
441 pTime->u8MonthDay -= cDaysInMonth;
442 if (pTime->u8Month != 12)
443 pTime->u8Month++;
444 else
445 {
446 pTime->u8Month = 1;
447 pTime->i32Year++;
448 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
449 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
450 }
451 }
452
453 pTime->u16YearDay = pTime->u8MonthDay - 1
454 + (fLeapYear
455 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
456 : g_aiDayOfYear[pTime->u8Month - 1]);
457 }
458 else
459 {
460 /*
461 * Are both YearDay and Month/MonthDay valid?
462 * Check that they don't overflow and match, if not use YearDay (simpler).
463 */
464 bool fRecalc = true;
465 if ( pTime->u8Month
466 && pTime->u8MonthDay)
467 {
468 do
469 {
470 /* If you change one, zero the other to make clear what you mean. */
471 AssertBreak(pTime->u8Month <= 12);
472 AssertBreak(pTime->u8MonthDay <= (fLeapYear
473 ? g_acDaysInMonthsLeap[pTime->u8Month - 1]
474 : g_acDaysInMonths[pTime->u8Month - 1]));
475 uint16_t u16YearDay = pTime->u8MonthDay - 1
476 + (fLeapYear
477 ? g_aiDayOfYearLeap[pTime->u8Month - 1]
478 : g_aiDayOfYear[pTime->u8Month - 1]);
479 AssertBreak(u16YearDay == pTime->u16YearDay);
480 fRecalc = false;
481 } while (0);
482 }
483 if (fRecalc)
484 {
485 /* overflow adjust YearDay */
486 while (pTime->u16YearDay > (fLeapYear ? 366 : 365))
487 {
488 pTime->u16YearDay -= fLeapYear ? 366 : 365;
489 pTime->i32Year++;
490 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
491 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
492 }
493
494 /* calc Month and MonthDay */
495 const uint16_t *paiDayOfYear = fLeapYear
496 ? &g_aiDayOfYearLeap[0]
497 : &g_aiDayOfYear[0];
498 pTime->u8Month = 1;
499 while (pTime->u16YearDay > paiDayOfYear[pTime->u8Month])
500 pTime->u8Month++;
501 Assert(pTime->u8Month >= 1 && pTime->u8Month <= 12);
502 pTime->u8MonthDay = pTime->u16YearDay - paiDayOfYear[pTime->u8Month - 1] + 1;
503 }
504 }
505
506 /*
507 * Fixup time overflows.
508 * Use unsigned int values internally to avoid overflows.
509 */
510 unsigned uSecond = pTime->u8Second;
511 unsigned uMinute = pTime->u8Minute;
512 unsigned uHour = pTime->u8Hour;
513
514 while (pTime->u32Nanosecond >= 1000000000)
515 {
516 pTime->u32Nanosecond -= 1000000000;
517 uSecond++;
518 }
519
520 while (uSecond >= 60)
521 {
522 uSecond -= 60;
523 uMinute++;
524 }
525
526 while (uMinute >= 60)
527 {
528 uMinute -= 60;
529 uHour++;
530 }
531
532 while (uHour >= 24)
533 {
534 uHour -= 24;
535
536 /* This is really a RTTimeIncDay kind of thing... */
537 if (pTime->u16YearDay + 1 != (fLeapYear ? g_aiDayOfYearLeap[pTime->u8Month] : g_aiDayOfYear[pTime->u8Month]))
538 {
539 pTime->u16YearDay++;
540 pTime->u8MonthDay++;
541 }
542 else if (pTime->u8Month != 12)
543 {
544 pTime->u16YearDay++;
545 pTime->u8Month++;
546 pTime->u8MonthDay = 1;
547 }
548 else
549 {
550 pTime->i32Year++;
551 fLeapYear = rtTimeIsLeapYear(pTime->i32Year);
552 pTime->fFlags &= ~(RTTIME_FLAGS_COMMON_YEAR | RTTIME_FLAGS_LEAP_YEAR);
553 pTime->u16YearDay = 1;
554 pTime->u8Month = 1;
555 pTime->u8MonthDay = 1;
556 }
557 }
558
559 pTime->u8Second = uSecond;
560 pTime->u8Minute = uMinute;
561 pTime->u8Hour = uHour;
562
563 /*
564 * Correct the leap year flag.
565 * Assert if it's wrong, but ignore if unset.
566 */
567 if (fLeapYear)
568 {
569 Assert(!(pTime->fFlags & RTTIME_FLAGS_COMMON_YEAR));
570 pTime->fFlags &= ~RTTIME_FLAGS_COMMON_YEAR;
571 pTime->fFlags |= RTTIME_FLAGS_LEAP_YEAR;
572 }
573 else
574 {
575 Assert(!(pTime->fFlags & RTTIME_FLAGS_LEAP_YEAR));
576 pTime->fFlags &= ~RTTIME_FLAGS_LEAP_YEAR;
577 pTime->fFlags |= RTTIME_FLAGS_COMMON_YEAR;
578 }
579
580
581 /*
582 * Calc week day.
583 *
584 * 1970-01-01 was a Thursday (3), so find the number of days relative to
585 * that point. We use the table when possible and a slow+stupid+brute-force
586 * algorithm for points outside it. Feel free to optimize the latter by
587 * using some clever formula.
588 */
589 if ( pTime->i32Year >= OFF_YEAR_IDX_0_YEAR
590 && pTime->i32Year < OFF_YEAR_IDX_0_YEAR + (int32_t)RT_ELEMENTS(g_aoffYear))
591 {
592 int32_t offDays = g_aoffYear[pTime->i32Year - OFF_YEAR_IDX_0_YEAR]
593 + pTime->u16YearDay -1;
594 pTime->u8WeekDay = ((offDays % 7) + 3 + 7) % 7;
595 }
596 else
597 {
598 int32_t i32Year = pTime->i32Year;
599 if (i32Year >= 1970)
600 {
601 uint64_t offDays = pTime->u16YearDay - 1;
602 while (--i32Year >= 1970)
603 offDays += rtTimeIsLeapYear(i32Year) ? 366 : 365;
604 pTime->u8WeekDay = (uint8_t)((offDays + 3) % 7);
605 }
606 else
607 {
608 int64_t offDays = (fLeapYear ? -366 - 1 : -365 - 1) + pTime->u16YearDay;
609 while (++i32Year < 1970)
610 offDays -= rtTimeIsLeapYear(i32Year) ? 366 : 365;
611 pTime->u8WeekDay = ((int)(offDays % 7) + 3 + 7) % 7;
612 }
613 }
614 return pTime;
615}
616
617
618/**
619 * Normalizes the fields of a time structure.
620 *
621 * It is possible to calculate year-day from month/day and vice
622 * versa. If you adjust any of these, make sure to zero the
623 * other so you make it clear which of the fields to use. If
624 * it's ambiguous, the year-day field is used (and you get
625 * assertions in debug builds).
626 *
627 * All the time fields and the year-day or month/day fields will
628 * be adjusted for overflows. (Since all fields are unsigned, there
629 * is no underflows.) It is possible to exploit this for simple
630 * date math, though the recommended way of doing that to implode
631 * the time into a timespec and do the math on that.
632 *
633 * @returns pTime on success.
634 * @returns NULL if the data is invalid.
635 *
636 * @param pTime The time structure to normalize.
637 *
638 * @remarks This function doesn't work with local time, only with UTC time.
639 */
640RTDECL(PRTTIME) RTTimeNormalize(PRTTIME pTime)
641{
642 /*
643 * Validate that we've got the minium of stuff handy.
644 */
645 AssertReturn(VALID_PTR(pTime), NULL);
646 AssertMsgReturn(!(pTime->fFlags & ~RTTIME_FLAGS_MASK), ("%#x\n", pTime->fFlags), NULL);
647 AssertMsgReturn((pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) != RTTIME_FLAGS_TYPE_LOCAL, ("Use RTTimeLocalNormalize!\n"), NULL);
648 AssertMsgReturn(pTime->offUTC == 0, ("%d; Use RTTimeLocalNormalize!\n", pTime->offUTC), NULL);
649
650 pTime = rtTimeNormalizeInternal(pTime);
651 if (pTime)
652 pTime->fFlags |= RTTIME_FLAGS_TYPE_UTC;
653 return pTime;
654}
655
656
657/**
658 * Converts a time spec to a ISO date string.
659 *
660 * @returns psz on success.
661 * @returns NULL on buffer underflow.
662 * @param pTime The time. Caller should've normalized this.
663 * @param psz Where to store the string.
664 * @param cb The size of the buffer.
665 */
666RTDECL(char *) RTTimeToString(PCRTTIME pTime, char *psz, size_t cb)
667{
668 /* (Default to UTC if not specified) */
669 if ( (pTime->fFlags & RTTIME_FLAGS_TYPE_MASK) == RTTIME_FLAGS_TYPE_LOCAL
670 && pTime->offUTC)
671 {
672 Assert(pTime->offUTC <= 840 && pTime->offUTC >= -840);
673 int32_t offUTCHour = pTime->offUTC / 60;
674 int32_t offUTCMinute = pTime->offUTC % 60;
675 char chSign;
676 if (pTime->offUTC >= 0)
677 chSign = '+';
678 else
679 {
680 chSign = '-';
681 offUTCMinute = -offUTCMinute;
682 offUTCHour = -offUTCHour;
683 }
684 size_t cch = RTStrPrintf(psz, cb,
685 "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32%c%02%02",
686 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
687 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond,
688 chSign, offUTCHour, offUTCMinute);
689 if ( cch <= 15
690 || psz[cch - 5] != chSign)
691 return NULL;
692 }
693 else
694 {
695 size_t cch = RTStrPrintf(psz, cb, "%RI32-%02u-%02uT%02u:%02u:%02u.%09RU32Z",
696 pTime->i32Year, pTime->u8Month, pTime->u8MonthDay,
697 pTime->u8Hour, pTime->u8Minute, pTime->u8Second, pTime->u32Nanosecond);
698 if ( cch <= 15
699 || psz[cch - 1] != 'Z')
700 return NULL;
701 }
702 return psz;
703}
704
705
706/**
707 * Converts a time spec to a ISO date string.
708 *
709 * @returns psz on success.
710 * @returns NULL on buffer underflow.
711 * @param pTime The time spec.
712 * @param psz Where to store the string.
713 * @param cb The size of the buffer.
714 */
715RTDECL(char *) RTTimeSpecToString(PCRTTIMESPEC pTime, char *psz, size_t cb)
716{
717 RTTIME Time;
718 return RTTimeToString(RTTimeExplode(&Time, pTime), psz, cb);
719}
720
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