1/*
2 * Copyright (C) 2012 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#pragma once
26
27#include <string.h>
28#include <time.h>
29#include <wtf/DateMath.h>
30#include <wtf/Noncopyable.h>
31#include <wtf/StdLibExtras.h>
32
33namespace WTF {
34
35class GregorianDateTime final {
36 WTF_MAKE_FAST_ALLOCATED;
37public:
38 GregorianDateTime() = default;
39 WTF_EXPORT_PRIVATE explicit GregorianDateTime(double ms, LocalTimeOffset);
40
41 inline int year() const { return m_year; }
42 inline int month() const { return m_month; }
43 inline int yearDay() const { return m_yearDay; }
44 inline int monthDay() const { return m_monthDay; }
45 inline int weekDay() const { return m_weekDay; }
46 inline int hour() const { return m_hour; }
47 inline int minute() const { return m_minute; }
48 inline int second() const { return m_second; }
49 inline int utcOffsetInMinute() const { return m_utcOffsetInMinute; }
50 inline int isDST() const { return m_isDST; }
51
52 inline void setYear(int year) { m_year = year; }
53 inline void setMonth(int month) { m_month = month; }
54 inline void setYearDay(int yearDay) { m_yearDay = yearDay; }
55 inline void setMonthDay(int monthDay) { m_monthDay = monthDay; }
56 inline void setWeekDay(int weekDay) { m_weekDay = weekDay; }
57 inline void setHour(int hour) { m_hour = hour; }
58 inline void setMinute(int minute) { m_minute = minute; }
59 inline void setSecond(int second) { m_second = second; }
60 inline void setUTCOffsetInMinute(int utcOffsetInMinute) { m_utcOffsetInMinute = utcOffsetInMinute; }
61 inline void setIsDST(int isDST) { m_isDST = isDST; }
62
63 static ptrdiff_t offsetOfYear() { return OBJECT_OFFSETOF(GregorianDateTime, m_year); }
64 static ptrdiff_t offsetOfMonth() { return OBJECT_OFFSETOF(GregorianDateTime, m_month); }
65 static ptrdiff_t offsetOfYearDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_yearDay); }
66 static ptrdiff_t offsetOfMonthDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_monthDay); }
67 static ptrdiff_t offsetOfWeekDay() { return OBJECT_OFFSETOF(GregorianDateTime, m_weekDay); }
68 static ptrdiff_t offsetOfHour() { return OBJECT_OFFSETOF(GregorianDateTime, m_hour); }
69 static ptrdiff_t offsetOfMinute() { return OBJECT_OFFSETOF(GregorianDateTime, m_minute); }
70 static ptrdiff_t offsetOfSecond() { return OBJECT_OFFSETOF(GregorianDateTime, m_second); }
71 static ptrdiff_t offsetOfUTCOffsetInMinute() { return OBJECT_OFFSETOF(GregorianDateTime, m_utcOffsetInMinute); }
72 static ptrdiff_t offsetOfIsDST() { return OBJECT_OFFSETOF(GregorianDateTime, m_isDST); }
73
74 WTF_EXPORT_PRIVATE void setToCurrentLocalTime();
75
76 operator tm() const
77 {
78 tm ret;
79 memset(&ret, 0, sizeof(ret));
80
81 ret.tm_year = m_year - 1900;
82 ret.tm_mon = m_month;
83 ret.tm_yday = m_yearDay;
84 ret.tm_mday = m_monthDay;
85 ret.tm_wday = m_weekDay;
86 ret.tm_hour = m_hour;
87 ret.tm_min = m_minute;
88 ret.tm_sec = m_second;
89 ret.tm_isdst = m_isDST;
90
91#if HAVE(TM_GMTOFF)
92 ret.tm_gmtoff = static_cast<long>(m_utcOffsetInMinute) * static_cast<long>(secondsPerMinute);
93#endif
94
95 return ret;
96 }
97
98private:
99 int m_year { 0 };
100 int m_month { 0 };
101 int m_yearDay { 0 };
102 int m_monthDay { 0 };
103 int m_weekDay { 0 };
104 int m_hour { 0 };
105 int m_minute { 0 };
106 int m_second { 0 };
107 int m_utcOffsetInMinute { 0 };
108 int m_isDST { 0 };
109};
110
111} // namespace WTF
112
113using WTF::GregorianDateTime;
114