1/*
2 * Copyright (C) 2012, 2014 Patrick Gansterer <[email protected]>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26#include <wtf/GregorianDateTime.h>
27
28#include <wtf/DateMath.h>
29
30#if OS(WINDOWS)
31#include <windows.h>
32#else
33#include <time.h>
34#endif
35
36namespace WTF {
37
38void GregorianDateTime::setToCurrentLocalTime()
39{
40#if OS(WINDOWS)
41 SYSTEMTIME systemTime;
42 GetLocalTime(&systemTime);
43 TIME_ZONE_INFORMATION timeZoneInformation;
44 DWORD timeZoneId = GetTimeZoneInformation(&timeZoneInformation);
45
46 LONG bias = 0;
47 if (timeZoneId != TIME_ZONE_ID_INVALID) {
48 bias = timeZoneInformation.Bias;
49 if (timeZoneId == TIME_ZONE_ID_DAYLIGHT)
50 bias += timeZoneInformation.DaylightBias;
51 else if ((timeZoneId == TIME_ZONE_ID_STANDARD) || (timeZoneId == TIME_ZONE_ID_UNKNOWN))
52 bias += timeZoneInformation.StandardBias;
53 else
54 ASSERT(0);
55 }
56
57 m_year = systemTime.wYear;
58 m_month = systemTime.wMonth - 1;
59 m_monthDay = systemTime.wDay;
60 m_yearDay = dayInYear(m_year, m_month, m_monthDay);
61 m_weekDay = systemTime.wDayOfWeek;
62 m_hour = systemTime.wHour;
63 m_minute = systemTime.wMinute;
64 m_second = systemTime.wSecond;
65 m_utcOffset = -bias * secondsPerMinute;
66 m_isDST = timeZoneId == TIME_ZONE_ID_DAYLIGHT ? 1 : 0;
67#else
68 tm localTM;
69 time_t localTime = time(0);
70#if HAVE(LOCALTIME_R)
71 localtime_r(&localTime, &localTM);
72#else
73 localtime_s(&localTime, &localTM);
74#endif
75
76 m_year = localTM.tm_year + 1900;
77 m_month = localTM.tm_mon;
78 m_monthDay = localTM.tm_mday;
79 m_yearDay = localTM.tm_yday;
80 m_weekDay = localTM.tm_wday;
81 m_hour = localTM.tm_hour;
82 m_minute = localTM.tm_min;
83 m_second = localTM.tm_sec;
84 m_isDST = localTM.tm_isdst;
85#if HAVE(TM_GMTOFF)
86 m_utcOffset = localTM.tm_gmtoff;
87#else
88 m_utcOffset = calculateLocalTimeOffset(localTime * msPerSecond).offset / msPerSecond;
89#endif
90#endif
91}
92
93} // namespace WTF
94