1/*
2 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
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 "GregorianDateTime.h"
27
28#include "DateMath.h"
29
30#if OS(WIN)
31#include <windows.h>
32#endif
33
34namespace WTF {
35
36void GregorianDateTime::setToCurrentLocalTime()
37{
38#if OS(WIN)
39    SYSTEMTIME systemTime;
40    GetLocalTime(&systemTime);
41    TIME_ZONE_INFORMATION timeZoneInformation;
42    DWORD timeZoneId = GetTimeZoneInformation(&timeZoneInformation);
43
44    LONG bias = timeZoneInformation.Bias;
45    if (timeZoneId == TIME_ZONE_ID_DAYLIGHT)
46        bias += timeZoneInformation.DaylightBias;
47    else if (timeZoneId == TIME_ZONE_ID_STANDARD)
48        bias += timeZoneInformation.StandardBias;
49    else
50        ASSERT(timeZoneId == TIME_ZONE_ID_UNKNOWN);
51
52    m_year = systemTime.wYear;
53    m_month = systemTime.wMonth - 1;
54    m_monthDay = systemTime.wDay;
55    m_yearDay = dayInYear(m_year, m_month, m_monthDay);
56    m_weekDay = systemTime.wDayOfWeek;
57    m_hour = systemTime.wHour;
58    m_minute = systemTime.wMinute;
59    m_second = systemTime.wSecond;
60    m_utcOffset = -bias * secondsPerMinute;
61    m_isDST = timeZoneId == TIME_ZONE_ID_DAYLIGHT ? 1 : 0;
62#else
63    tm localTM;
64    time_t localTime = time(0);
65    localtime_r(&localTime, &localTM);
66
67    m_year = localTM.tm_year + 1900;
68    m_month = localTM.tm_mon;
69    m_monthDay = localTM.tm_mday;
70    m_yearDay = localTM.tm_yday;
71    m_weekDay = localTM.tm_wday;
72    m_hour = localTM.tm_hour;
73    m_minute = localTM.tm_min;
74    m_second = localTM.tm_sec;
75    m_isDST = localTM.tm_isdst;
76#if HAVE(TM_GMTOFF)
77    m_utcOffset = localTM.tm_gmtoff;
78#else
79    int utcOffset = calculateUTCOffset();
80    utcOffset += calculateDSTOffset(localTime * msPerSecond, utcOffset);
81    m_utcOffset = utcOffset / msPerSecond;
82#endif
83#endif
84}
85
86} // namespace WTF
87