Repository navigation
Expand file tree
/
Copy pathtz.cpp
More file actions
156 lines (139 loc) · 4.34 KB
/
Copy pathtz.cpp
File metadata and controls
156 lines (139 loc) · 4.34 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*############################################################################
# Copyright (c) 2020 Source Simian : https://github.com/sourcesimian/uICAL #
############################################################################*/
#include "uICAL/cppstl.h"
#include "uICAL/types.h"
#include "uICAL/error.h"
#include "uICAL/tz.h"
#include "uICAL/datestamp.h"
#include "uICAL/epochtime.h"
#include "uICAL/tzmap.h"
namespace uICAL {
static TZ_ptr UNDEF = new_ptr<TZ>();
static TZ_ptr UNAWARE = new_ptr<TZ>(false);
TZ_ptr TZ::undef() {
return UNDEF;
}
TZ_ptr TZ::unaware() {
return UNAWARE;
}
TZ::TZ() {
this->offsetSecs = -1;
this->aware = false;
}
TZ::TZ(bool aware) {
this->offsetSecs = 0;
this->aware = aware;
}
TZ::TZ(const string& tz) {
this->offsetSecs = TZ::parseOffsetSecs(tz);
this->aware = true;
}
TZ::TZ(const string& tz, const TZMap_ptr& tzmap)
: idmap(tzmap)
{
string id = "";
if (tzmap != NULL) {
id = this->idmap->findId(tz);
}
if (!id.empty()) {
this->id = id;
this->aware = true;
}
else {
this->offsetSecs = TZ::parseOffsetSecs(tz);
this->aware = true;
}
}
bool TZ::is_aware() const {
return this->aware;
}
int TZ::parseOffsetSecs(const string& tz) {
if (tz == "Z") {
return 0;
}
try {
if (tz.length() == 5 || tz.length() == 7) {
char sign;
unsigned tzH, tzM, tzS;
// e.g.: +0200
sign = tz.at(0);
tzH = tz.substr(1, 2).as_int();
tzM = tz.substr(3, 2).as_int();
if (tz.length() == 7) {
tzS = tz.substr(5, 2).as_int();
} else {
tzS = 0;
}
int offset = ((tzH * 60) + tzM) * 60 + tzS;
if (sign == '-') {
offset *= -1;
}
return offset;
}
}
catch (std::invalid_argument const &e)
{}
catch (std::out_of_range const &e)
{}
throw ValueError("Bad timezone: \"" + tz + "\"");
}
void TZ::offsetAsString(ostream& out, int offsetSecs) {
if (offsetSecs != -1) {
if (offsetSecs == 0) {
out << "Z";
}
else
{
if (offsetSecs < 0) {
out << "-";
offsetSecs *= -1;
}
else
{
out << "+";
}
out << string::fmt(fmt_02d, offsetSecs / 60 / 60);
out << string::fmt(fmt_02d, (offsetSecs / 60)% 60);
if (offsetSecs % 60) {
out << string::fmt(fmt_02d, offsetSecs % 60);
}
}
}
}
seconds_t TZ::toUTC(seconds_t timestamp) const {
if (!this->id.empty()) {
return this->idmap->toUTC(this->id, timestamp);
}
if (this->offsetSecs == -1)
throw ImplementationError("Timezone not defined");
return timestamp - this->offsetSecs;
}
seconds_tz_t TZ::fromUTC(seconds_t timestamp) const {
if (!this->id.empty()) {
return this->idmap->fromUTC(this->id, timestamp);
}
if (this->offsetSecs == -1)
throw ImplementationError("Timezone not defined");
return seconds_tz_t(timestamp + this->offsetSecs, this->as_str());
}
start_offset_tz_t TZ::next_transition_UTC(seconds_t timestamp) const {
if (!this->id.empty()) {
return this->idmap->next_transition_UTC(this->id, timestamp);
}
if (this->offsetSecs == -1)
throw ImplementationError("Timezone not defined");
return start_offset_tz_t(MAX_UICAL_SECONDS, this->offsetSecs, this->as_str());
}
void TZ::str(ostream& out) const {
if (!this->id.empty()) {
out << this->idmap->getName(this->id);
return;
}
if (this->offsetSecs == -1)
throw ImplementationError("Timezone not defined");
if (!this->aware)
return;
TZ::offsetAsString(out, this->offsetSecs);
}
}