CppCommon  1.0.4.1
C++ Common Library
timezone.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_TIME_TIMEZONE_H
10 #define CPPCOMMON_TIME_TIMEZONE_H
11 
12 #include "time/time.h"
13 
14 #include <string>
15 
16 namespace CppCommon {
17 
19 
24 class Timezone
25 {
26 public:
28  Timezone();
30 
35  explicit Timezone(const std::string& name, const Timespan& offset, const Timespan& dstoffset = Timespan::zero())
36  : _name(name), _offset(offset), _dstoffset(dstoffset)
37  {}
38  Timezone(const Timezone&) = default;
39  Timezone(Timezone&&) = default;
40  ~Timezone() = default;
41 
42  Timezone& operator=(const Timezone&) = default;
43  Timezone& operator=(Timezone&&) = default;
44 
45  // Timezone comparison
46  friend bool operator==(const Timezone& timezone1, const Timezone& timezone2) noexcept
47  { return timezone1.total() == timezone2.total(); }
48  friend bool operator!=(const Timezone& timezone1, const Timezone& timezone2) noexcept
49  { return timezone1.total() != timezone2.total(); }
50  friend bool operator>(const Timezone& timezone1, const Timezone& timezone2) noexcept
51  { return timezone1.total() > timezone2.total(); }
52  friend bool operator<(const Timezone& timezone1, const Timezone& timezone2) noexcept
53  { return timezone1.total() < timezone2.total(); }
54  friend bool operator>=(const Timezone& timezone1, const Timezone& timezone2) noexcept
55  { return timezone1.total() >= timezone2.total(); }
56  friend bool operator<=(const Timezone& timezone1, const Timezone& timezone2) noexcept
57  { return timezone1.total() <= timezone2.total(); }
58 
60  const std::string& name() const noexcept { return _name; }
62  const Timespan& offset() const noexcept { return _offset; }
64  const Timespan& daylight() const noexcept { return _dstoffset; }
65 
67  Timespan total() const noexcept { return _offset + _dstoffset; }
68 
70 
74  LocalTime Convert(const UtcTime& utctime) const
75  { return LocalTime(utctime + total()); }
77 
81  UtcTime Convert(const LocalTime& localtime) const
82  { return UtcTime(localtime - total()); }
83 
85 
90  static Timezone utc()
91  { return Timezone("GMT", Timespan::zero()); }
92 
94 
99  static Timezone local()
100  { return Timezone(); }
101 
103  void swap(Timezone& timezone) noexcept;
104  friend void swap(Timezone& timezone1, Timezone& timezone2) noexcept;
105 
106 protected:
108  std::string _name;
113 };
114 
117 } // namespace CppCommon
118 
119 #include "timezone.inl"
120 
121 #endif // CPPCOMMON_TIME_TIMEZONE_H
Local time.
Definition: time.h:235
static Timespan zero() noexcept
Get zero timespan.
Definition: timespan.h:182
Timezone(Timezone &&)=default
Timespan _dstoffset
Timezone daylight saving time offset.
Definition: timezone.h:112
void swap(Timezone &timezone) noexcept
Swap two instances.
Definition: timezone.inl:11
Timezone(const Timezone &)=default
static Timezone utc()
Get the UTC timezone (Greenwich Mean Time)
Definition: timezone.h:90
std::string _name
Timezone name.
Definition: timezone.h:108
const std::string & name() const noexcept
Get timezone name.
Definition: timezone.h:60
static Timezone local()
Get the local timezone.
Definition: timezone.h:99
Timezone & operator=(Timezone &&)=default
friend bool operator==(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:46
friend bool operator>=(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:54
const Timespan & daylight() const noexcept
Get timezone daylight saving time offset.
Definition: timezone.h:64
Timezone()
Initialize timezone with a current local timezone.
Definition: timezone.cpp:19
UtcTime Convert(const LocalTime &localtime) const
Convert local time to UTC time using the current timezone.
Definition: timezone.h:81
friend bool operator<=(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:56
Timespan _offset
Timezone offset.
Definition: timezone.h:110
Timezone & operator=(const Timezone &)=default
Timezone(const std::string &name, const Timespan &offset, const Timespan &dstoffset=Timespan::zero())
Initialize timezone with a given local time offset and daylight saving time offset.
Definition: timezone.h:35
friend bool operator<(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:52
friend bool operator>(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:50
friend bool operator!=(const Timezone &timezone1, const Timezone &timezone2) noexcept
Definition: timezone.h:48
LocalTime Convert(const UtcTime &utctime) const
Convert UTC time to local time using the current timezone.
Definition: timezone.h:74
const Timespan & offset() const noexcept
Get timezone offset.
Definition: timezone.h:62
Timespan total() const noexcept
Get timezone total offset.
Definition: timezone.h:67
UTC time.
Definition: time.h:209
C++ Common project definitions.
Definition: token_bucket.h:15
Time definition.
Timezone inline implementation.