1 | /* $Id $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Local Time, Posix.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
27 | #define RTTIME_INCL_TIMEVAL
|
---|
28 | #include <iprt/types.h>
|
---|
29 | #include <sys/time.h>
|
---|
30 | #include <time.h>
|
---|
31 |
|
---|
32 | #include <iprt/time.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This tries to find the UTC offset for a given timespec.
|
---|
37 | *
|
---|
38 | * It does probably not take into account changes in daylight
|
---|
39 | * saving over the years or similar stuff.
|
---|
40 | *
|
---|
41 | * @returns UTC offset in nanoseconds.
|
---|
42 | * @param pTime The time.
|
---|
43 | * @param fCurrentTime Whether the input is current time or not.
|
---|
44 | * This is for avoid infinit recursion on errors in the fallback path.
|
---|
45 | */
|
---|
46 | static int64_t rtTimeLocalUTCOffset(PCRTTIMESPEC pTime, bool fCurrentTime)
|
---|
47 | {
|
---|
48 | RTTIMESPEC Fallback;
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Convert to time_t.
|
---|
52 | */
|
---|
53 | int64_t i64UnixTime = RTTimeSpecGetSeconds(pTime);
|
---|
54 | time_t UnixTime = i64UnixTime;
|
---|
55 | if (UnixTime != i64UnixTime)
|
---|
56 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * Explode it as both local and uct time.
|
---|
60 | */
|
---|
61 | struct tm TmLocal;
|
---|
62 | if ( !localtime_r(&UnixTime, &TmLocal)
|
---|
63 | || !TmLocal.tm_year)
|
---|
64 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
65 | struct tm TmUct;
|
---|
66 | if (!gmtime_r(&UnixTime, &TmUct))
|
---|
67 | return fCurrentTime ? 0 : rtTimeLocalUTCOffset(RTTimeNow(&Fallback), true);
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Calc the difference (if any).
|
---|
71 | * We ASSUME that the difference is less that 24 hours.
|
---|
72 | */
|
---|
73 | if ( TmLocal.tm_hour == TmUct.tm_hour
|
---|
74 | && TmLocal.tm_min == TmUct.tm_min
|
---|
75 | && TmLocal.tm_sec == TmUct.tm_sec
|
---|
76 | && TmLocal.tm_mday == TmUct.tm_mday)
|
---|
77 | return 0;
|
---|
78 |
|
---|
79 | int LocalSecs = TmLocal.tm_hour * 3600
|
---|
80 | + TmLocal.tm_min * 60
|
---|
81 | + TmLocal.tm_sec;
|
---|
82 | int UctSecs = TmUct.tm_hour * 3600
|
---|
83 | + TmUct.tm_min * 60
|
---|
84 | + TmUct.tm_sec;
|
---|
85 | if (TmLocal.tm_mday != TmUct.tm_mday)
|
---|
86 | {
|
---|
87 | if ( ( TmLocal.tm_mday > TmUct.tm_mday
|
---|
88 | && TmUct.tm_mday != 1)
|
---|
89 | || TmLocal.tm_mday == 1)
|
---|
90 | LocalSecs += 24*60*60;
|
---|
91 | else
|
---|
92 | UctSecs += 24*60*60;
|
---|
93 | }
|
---|
94 |
|
---|
95 | return (LocalSecs - UctSecs) * INT64_C(1000000000);
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Gets the delta between UTC and local time.
|
---|
101 | *
|
---|
102 | * @code
|
---|
103 | * RTTIMESPEC LocalTime;
|
---|
104 | * RTTimeSpecAddNano(RTTimeNow(&LocalTime), RTTimeLocalDeltaNano());
|
---|
105 | * @endcode
|
---|
106 | *
|
---|
107 | * @returns Returns the nanosecond delta between UTC and local time.
|
---|
108 | */
|
---|
109 | RTDECL(int64_t) RTTimeLocalDeltaNano(void)
|
---|
110 | {
|
---|
111 | RTTIMESPEC Time;
|
---|
112 | return rtTimeLocalUTCOffset(RTTimeNow(&Time), true /* current time, skip fallback */);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Explodes a time spec to the localized timezone.
|
---|
118 | *
|
---|
119 | * @returns pTime.
|
---|
120 | * @param pTime Where to store the exploded time.
|
---|
121 | * @param pTimeSpec The time spec to exploded. (UTC)
|
---|
122 | */
|
---|
123 | RTDECL(PRTTIME) RTTimeLocalExplode(PRTTIME pTime, PCRTTIMESPEC pTimeSpec)
|
---|
124 | {
|
---|
125 | RTTIMESPEC LocalTime = *pTimeSpec;
|
---|
126 | RTTimeSpecAddNano(&LocalTime, rtTimeLocalUTCOffset(&LocalTime, true /* current time, skip fallback */));
|
---|
127 | pTime = RTTimeExplode(pTime, &LocalTime);
|
---|
128 | if (pTime)
|
---|
129 | pTime->fFlags = (pTime->fFlags & ~RTTIME_FLAGS_TYPE_MASK) | RTTIME_FLAGS_TYPE_LOCAL;
|
---|
130 | return pTime;
|
---|
131 | }
|
---|
132 |
|
---|