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