-
Notifications
You must be signed in to change notification settings - Fork 74
/
timezone.h
121 lines (100 loc) · 3.66 KB
/
timezone.h
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
/*!
\file timezone.h
\brief Timezone definition
\author Ivan Shynkarenka
\date 18.07.2016
\copyright MIT License
*/
#ifndef CPPCOMMON_TIME_TIMEZONE_H
#define CPPCOMMON_TIME_TIMEZONE_H
#include "time/time.h"
#include <string>
namespace CppCommon {
//! Timezone
/*!
Timezone wraps the current timezone and dynamic daylight saving time settings.
Not thread-safe.
*/
class Timezone
{
public:
//! Initialize timezone with a current local timezone
Timezone();
//! Initialize timezone with a given local time offset and daylight saving time offset
/*!
\param name - Timezone name
\param offset - Local time offset
\param dstoffset - Daylight saving time offset (default is Timespan::zero())
*/
explicit Timezone(const std::string& name, const Timespan& offset, const Timespan& dstoffset = Timespan::zero())
: _name(name), _offset(offset), _dstoffset(dstoffset)
{}
Timezone(const Timezone&) = default;
Timezone(Timezone&&) = default;
~Timezone() = default;
Timezone& operator=(const Timezone&) = default;
Timezone& operator=(Timezone&&) = default;
// Timezone comparison
friend bool operator==(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() == timezone2.total(); }
friend bool operator!=(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() != timezone2.total(); }
friend bool operator>(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() > timezone2.total(); }
friend bool operator<(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() < timezone2.total(); }
friend bool operator>=(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() >= timezone2.total(); }
friend bool operator<=(const Timezone& timezone1, const Timezone& timezone2) noexcept
{ return timezone1.total() <= timezone2.total(); }
//! Get timezone name
const std::string& name() const noexcept { return _name; }
//! Get timezone offset
const Timespan& offset() const noexcept { return _offset; }
//! Get timezone daylight saving time offset
const Timespan& daylight() const noexcept { return _dstoffset; }
//! Get timezone total offset
Timespan total() const noexcept { return _offset + _dstoffset; }
//! Convert UTC time to local time using the current timezone
/*!
\param utctime - UTC time
\return Local time
*/
LocalTime Convert(const UtcTime& utctime) const
{ return LocalTime(utctime + total()); }
//! Convert local time to UTC time using the current timezone
/*!
\param localtime - Local time
\return Local time
*/
UtcTime Convert(const LocalTime& localtime) const
{ return UtcTime(localtime - total()); }
//! Get the UTC timezone (Greenwich Mean Time)
/*!
Thread-safe.
\return UTC timezone
*/
static Timezone utc()
{ return Timezone("GMT", Timespan::zero()); }
//! Get the local timezone
/*!
Thread-safe.
\return Local timezone
*/
static Timezone local()
{ return Timezone(); }
//! Swap two instances
void swap(Timezone& timezone) noexcept;
friend void swap(Timezone& timezone1, Timezone& timezone2) noexcept;
protected:
//! Timezone name
std::string _name;
//! Timezone offset
Timespan _offset;
//! Timezone daylight saving time offset
Timespan _dstoffset;
};
/*! \example time_timezone.cpp Timezone example */
} // namespace CppCommon
#include "timezone.inl"
#endif // CPPCOMMON_TIME_TIMEZONE_H