Repository navigation
Expand file tree
/
Copy pathtziter.cpp
More file actions
148 lines (124 loc) · 4.74 KB
/
Copy pathtziter.cpp
File metadata and controls
148 lines (124 loc) · 4.74 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
/*############################################################################
# Copyright (c) 2020 Source Simian : https://github.com/sourcesimian/uICAL #
############################################################################*/
#include "uICAL/cppstl.h"
#include "uICAL/types.h"
#include "uICAL/util.h"
#include "uICAL/error.h"
#include "uICAL/datetime.h"
#include "uICAL/datestamp.h"
#include "uICAL/rrule.h"
#include "uICAL/rruleiter.h"
#include "uICAL/tz.h"
#include "uICAL/tziter.h"
#include "uICAL/vobject.h"
#include "uICAL/vline.h"
namespace uICAL {
TZIter::TZIter(const VObject_ptr& timezone)
{
this->timezone = timezone;
}
seconds_t TZIter::toUTC(seconds_t timestamp) {
if (this->timezone) { this->init(); }
while (timestamp > this->transitions.back().local && this->next())
{}
auto i = this->transitions.begin();
while (1) {
if (i + 1 == this->transitions.end() || timestamp <= (i + 1)->local) {
return timestamp - i->offset;
}
++i;
}
}
seconds_tz_t TZIter::fromUTC(seconds_t timestamp) {
if (this->timezone) { this->init(); }
while (timestamp > this->transitions.back().utc && this->next()) { }
auto i = this->transitions.begin();
while (1) {
if (i + 1 == this->transitions.end() || timestamp <= (i + 1)->utc) {
return seconds_tz_t(timestamp + i->offset, i->name);
}
++i;
}
}
start_offset_tz_t TZIter::next_transition_UTC(seconds_t timestamp) {
if (this->timezone) { this->init(); }
while (timestamp >= this->transitions.back().utc && this->next()) { }
for (auto i = this->transitions.begin(); i != this->transitions.end(); ++i) {
if (timestamp <= i->utc) {
return start_offset_tz_t(i->utc, i->offset, i->name);
}
}
auto i = this->transitions.back();
return start_offset_tz_t(MAX_UICAL_SECONDS, i.offset, i.name);
}
void TZIter::init() {
auto objs = this->timezone->listObjects("STANDARD");
for (auto obj: objs) {
components.push_back(parse_stuff(obj, true));
}
objs = this->timezone->listObjects("DAYLIGHT");
for (auto obj: objs) {
components.push_back(parse_stuff(obj, false));
}
this->timezone = NULL;
this->next();
}
TZIter::tzcomp_t TZIter::parse_stuff(const VObject_ptr& obj, bool standard_time) {
VLine_ptr dtStart = obj->getPropertyByName("DTSTART");
VLine_ptr offsetFrom = obj->getPropertyByName("TZOFFSETFROM");
VLine_ptr offsetTo = obj->getPropertyByName("TZOFFSETTO");
VLine_ptr tzName = obj->getPropertyByName("TZNAME");
RRule_ptr rrule;
VLine_ptr rRule = obj->getPropertyByName("RRULE");
DateStamp ds(dtStart->value);
TZ_ptr tzFrom, tzTo;
tzFrom = new_ptr<TZ>(offsetFrom->value);
tzTo = new_ptr<TZ>(offsetTo->value);
auto start = DateTime(ds, tzFrom);
if (rRule != nullptr) {
rrule = new_ptr<RRule>(rRule->value, start, nullptr, "");
} else {
rrule = new_ptr<RRule>("", start, nullptr, "");
}
auto rdates = obj->getPropertiesByName("RDATE");
for (auto date : rdates) {
DateStamp d(date->value);
rrule->include(DateTime(d, tzFrom));
}
auto exdates = obj->getPropertiesByName("EXDATE");
for (auto date : exdates) {
DateStamp d(date->value);
rrule->exclude(DateTime(d, tzFrom));
}
auto iter = new_ptr<RRuleIter>(rrule, DateTime(), DateTime());
iter->next();
if (!iter->now().valid()) {
throw ImplementationError("Now is invalid");
}
TZIter::tzcomp_t ret;
if (tzName != nullptr) {
ret = {iter, tzFrom, tzTo, tzName->value};
} else if (standard_time) {
ret = {iter, tzFrom, tzTo, "STANDARD"};
} else {
ret = {iter, tzFrom, tzTo, "DAYLIGHT"};
}
return ret;
}
bool TZIter::next() {
if (this->components.size() == 0) {
return false;
}
auto i = std::min_element(this->components.begin(), this->components.end());
TZIter::transitions_t t = {i->rr->now().seconds(), i->rr->now().seconds(), i->to->offsetSecs, i->name};
this->transitions.push_back(t);
if (!i->rr->next()) {
this->components.erase(i);
}
return true;
}
bool operator < (const TZIter::tzcomp_t a, const TZIter::tzcomp_t b) {
return a.rr->now() < b.rr->now();
}
}