1 | /* $Id $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Local Time, Posix.
|
---|
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 | #define RTTIME_INCL_TIMEVAL
|
---|
37 | #include <iprt/types.h>
|
---|
38 | #include <sys/time.h>
|
---|
39 | #include <time.h>
|
---|
40 |
|
---|
41 | #include <iprt/time.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * This tries to find the UTC offset for a given timespec.
|
---|
46 | *
|
---|
47 | * It does probably not take into account changes in daylight
|
---|
48 | * saving over the years or similar stuff.
|
---|
49 | *
|
---|
50 | * @returns UTC offset in nanoseconds.
|
---|
51 | * @param pTime The time.
|
---|
52 | * @param fCurrentTime Whether the input is current time or not.
|
---|
53 | * This is for avoid infinit recursion on errors in the fallback path.
|
---|
54 | */
|
---|
55 | static int64_t rtTimeLocalUTCOffset(PCRTTIMESPEC pTime, bool fCurrentTime)
|
---|
56 | {
|
---|
57 | RTTIMESPEC Fallback;
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Convert to time_t.
|
---|
61 | */
|
---|
62 | int64_t i64UnixTime = RTTimeSpecGetSeconds(pTime);
|
---|
63 | time_t UnixTime = i64UnixTime;
|
---|
64 | if (UnixTime != i64UnixTime)
|
---|
65 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Explode it as both local and uct time.
|
---|
69 | */
|
---|
70 | struct tm TmLocal;
|
---|
71 | if ( !localtime_r(&UnixTime, &TmLocal)
|
---|
72 | || !TmLocal.tm_year)
|
---|
73 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
74 | struct tm TmUct;
|
---|
75 | if (!gmtime_r(&UnixTime, &TmUct))
|
---|
76 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Calc the difference (if any).
|
---|
80 | * We ASSUME that the difference is less that 24 hours.
|
---|
81 | */
|
---|
82 | if ( TmLocal.tm_hour == TmUct.tm_hour
|
---|
83 | && TmLocal.tm_min == TmUct.tm_min
|
---|
84 | && TmLocal.tm_sec == TmUct.tm_sec
|
---|
85 | && TmLocal.tm_mday == TmUct.tm_mday)
|
---|
86 | return 0;
|
---|
87 |
|
---|
88 | int LocalSecs = TmLocal.tm_hour * 3600
|
---|
89 | + TmLocal.tm_min * 60
|
---|
90 | + TmLocal.tm_sec;
|
---|
91 | int UctSecs = TmUct.tm_hour * 3600
|
---|
92 | + TmUct.tm_min * 60
|
---|
93 | + TmUct.tm_sec;
|
---|
94 | if (TmLocal.tm_mday != TmUct.tm_mday)
|
---|
95 | {
|
---|
96 | if ( ( TmLocal.tm_mday > TmUct.tm_mday
|
---|
97 | && TmUct.tm_mday != 1)
|
---|
98 | || TmLocal.tm_mday == 1)
|
---|
99 | LocalSecs += 24*60*60;
|
---|
100 | else
|
---|
101 | UctSecs += 24*60*60;
|
---|
102 | }
|
---|
103 |
|
---|
104 | return (LocalSecs - UctSecs) * INT64_C(1000000000);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Gets the delta between UTC and local time.
|
---|
110 | *
|
---|
111 | * @code
|
---|
112 | * RTTIMESPEC LocalTime;
|
---|
113 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
114 | * @endcode
|
---|
115 | *
|
---|
116 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
117 | */
|
---|
118 | RTDECL(int64_t) RTTimeLocalDeltaNano(void)
|
---|
119 | {
|
---|
120 | RTTIMESPEC Time;
|
---|
121 | return rtTimeLocalUTCOffset(RTTimeNow(&Time), true /* current time, skip fallback */);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Explodes a time spec to the localized timezone.
|
---|
127 | *
|
---|
128 | * @returns pTime.
|
---|
129 | * @param pTime Where to store the exploded time.
|
---|
130 | * @param pTimeSpec The time spec to exploded. (UTC)
|
---|
131 | */
|
---|
132 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
|
---|
133 | {
|
---|
134 | RTTIMESPEC LocalTime = *pTimeSpec;
|
---|
135 | RTTimeSpecAddNano(&LocalTime, rtTimeLocalUTCOffset(&LocalTime, true /* current time, skip fallback */));
|
---|
136 | pTime = RTTimeExplode(pTime, &LocalTime);
|
---|
137 | if (pTime)
|
---|
138 | pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
|
---|
139 | return pTime;
|
---|
140 | }
|
---|
141 |
|
---|