1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*
* Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <string.h>
#include <time.h>
#include <wtf/DateMath.h>
#include <wtf/Noncopyable.h>
#include <wtf/StdLibExtras.h>
namespace WTF {
class GregorianDateTime final {
WTF_MAKE_FAST_ALLOCATED;
public:
GregorianDateTime() = default;
WTF_EXPORT_PRIVATE explicit GregorianDateTime(double ms, LocalTimeOffset);
explicit GregorianDateTime(int year, int month, int yearDay, int monthDay, int weekDay, int hour, int minute, int second, int utcOffsetInMinute, bool isDST)
: m_year(year)
, m_month(month)
, m_yearDay(yearDay)
, m_monthDay(monthDay)
, m_weekDay(weekDay)
, m_hour(hour)
, m_minute(minute)
, m_second(second)
, m_utcOffsetInMinute(utcOffsetInMinute)
, m_isDST(isDST)
{
}
inline int year() const { return m_year; }
inline int month() const { return m_month; }
inline int yearDay() const { return m_yearDay; }
inline int monthDay() const { return m_monthDay; }
inline int weekDay() const { return m_weekDay; }
inline int hour() const { return m_hour; }
inline int minute() const { return m_minute; }
inline int second() const { return m_second; }
inline int utcOffsetInMinute() const { return m_utcOffsetInMinute; }
inline int isDST() const { return m_isDST; }
inline void setYear(int year) { m_year = year; }
inline void setMonth(int month) { m_month = month; }
inline void setYearDay(int yearDay) { m_yearDay = yearDay; }
inline void setMonthDay(int monthDay) { m_monthDay = monthDay; }
inline void setWeekDay(int weekDay) { m_weekDay = weekDay; }
inline void setHour(int hour) { m_hour = hour; }
inline void setMinute(int minute) { m_minute = minute; }
inline void setSecond(int second) { m_second = second; }
inline void setUTCOffsetInMinute(int utcOffsetInMinute) { m_utcOffsetInMinute = utcOffsetInMinute; }
inline void setIsDST(int isDST) { m_isDST = isDST; }
static constexpr ptrdiff_t offsetOfYear() { return OBJECT_OFFSETOF(GregorianDateTime, m_year); }
static constexpr ptrdiff_t offsetOfMonth() { return OBJECT_OFFSETOF(GregorianDateTime, m_month); }
static constexpr ptrdiff_t offsetOfYearDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_yearDay); }
static constexpr ptrdiff_t offsetOfMonthDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_monthDay); }
static constexpr ptrdiff_t offsetOfWeekDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_weekDay); }
static constexpr ptrdiff_t offsetOfHour() { return OBJECT_OFFSETOF(GregorianDateTime, m_hour); }
static constexpr ptrdiff_t offsetOfMinute() { return OBJECT_OFFSETOF(GregorianDateTime, m_minute); }
static constexpr ptrdiff_t offsetOfSecond() { return OBJECT_OFFSETOF(GregorianDateTime, m_second); }
static constexpr ptrdiff_t offsetOfUTCOffsetInMinute() { return OBJECT_OFFSETOF(GregorianDateTime, m_utcOffsetInMinute); }
static constexpr ptrdiff_t offsetOfIsDST() { return OBJECT_OFFSETOF(GregorianDateTime, m_isDST); }
WTF_EXPORT_PRIVATE void setToCurrentLocalTime();
operator tm() const
{
tm ret;
zeroBytes(ret);
ret.tm_year = m_year - 1900;
ret.tm_mon = m_month;
ret.tm_yday = m_yearDay;
ret.tm_mday = m_monthDay;
ret.tm_wday = m_weekDay;
ret.tm_hour = m_hour;
ret.tm_min = m_minute;
ret.tm_sec = m_second;
ret.tm_isdst = m_isDST;
#if HAVE(TM_GMTOFF)
ret.tm_gmtoff = static_cast<long>(m_utcOffsetInMinute) * static_cast<long>(secondsPerMinute);
#endif
return ret;
}
private:
int m_year { 0 };
int m_month { 0 };
int m_yearDay { 0 };
int m_monthDay { 0 };
int m_weekDay { 0 };
int m_hour { 0 };
int m_minute { 0 };
int m_second { 0 };
int m_utcOffsetInMinute { 0 };
int m_isDST { 0 };
};
} // namespace WTF
using WTF::GregorianDateTime;
|