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