32 #ifndef QORE_TIMEZONEMANAGER_H
34 #define QORE_TIMEZONEMANAGER_H
41 #ifndef LOCALTIME_LOCATION
42 #define LOCALTIME_LOCATION "/etc/localtime"
45 #ifndef ZONEINFO_LOCATION
46 #define ZONEINFO_LOCATION "/usr/share/zoneinfo"
49 DLLLOCAL
extern const char *STATIC_UTC;
52 hashdecl QoreTransitionInfo {
60 typedef std::vector<QoreTransitionInfo> trans_vec_t;
62 hashdecl QoreLeapInfo {
67 hashdecl QoreDSTTransition {
69 hashdecl QoreTransitionInfo *trans;
72 class AbstractQoreZoneInfo {
82 DLLLOCAL
virtual int getUTCOffsetImpl(
int64 epoch_offset,
bool &is_dst,
const char *&zone_name)
const = 0;
85 DLLLOCAL AbstractQoreZoneInfo(
int n_utcoff,
const std::string& n_name) : utcoff(n_utcoff), has_dst(false) {
89 DLLLOCAL AbstractQoreZoneInfo() : utcoff(-1), has_dst(false) {
92 DLLLOCAL AbstractQoreZoneInfo(
const std::string &n_name,
int n_utcoff = -1) : name(n_name), utcoff(n_utcoff), has_dst(false) {
94 if (!name.compare(0, localtime_path_prefix.size(), localtime_path_prefix)) {
95 name = name.c_str() + localtime_path_prefix.size();
99 virtual DLLLOCAL ~AbstractQoreZoneInfo() {
103 DLLLOCAL
int getUTCOffset()
const {
104 return utcoff == -1 ? 0 : utcoff;
108 DLLLOCAL
int getUTCOffset(
int64 epoch_offset)
const {
111 return getUTCOffsetImpl(epoch_offset, is_dst, temp);
115 DLLLOCAL
int getUTCOffset(
int64 epoch_offset,
bool &is_dst)
const {
117 return getUTCOffsetImpl(epoch_offset, is_dst, temp);
121 DLLLOCAL
int getUTCOffset(
int64 epoch_offset,
bool &is_dst,
const char *&zone_name)
const {
122 return getUTCOffsetImpl(epoch_offset, is_dst, zone_name);
126 DLLLOCAL
bool hasDST()
const {
130 DLLLOCAL
const char* getRegionName()
const {
137 DLLLOCAL
static int getUTCOffset(
const AbstractQoreZoneInfo* zone) {
138 return zone ? zone->getUTCOffset() : 0;
142 DLLLOCAL
static int getUTCOffset(
const AbstractQoreZoneInfo* zone,
int64 epoch_offset) {
143 return zone ? zone->getUTCOffset(epoch_offset) : 0;
147 DLLLOCAL
static int getUTCOffset(
const AbstractQoreZoneInfo* zone,
int64 epoch_offset,
bool &is_dst) {
149 return zone->getUTCOffset(epoch_offset, is_dst);
155 DLLLOCAL
static int getUTCOffset(
const AbstractQoreZoneInfo* zone,
int64 epoch_offset,
bool &is_dst,
const char *&zone_name) {
157 return zone->getUTCOffset(epoch_offset, is_dst, zone_name);
164 DLLLOCAL
static bool hasDST(
const AbstractQoreZoneInfo* zone) {
165 return zone ? zone->has_dst :
false;
168 DLLLOCAL
static const char *getRegionName(
const AbstractQoreZoneInfo* zone) {
169 return zone ? zone->getRegionName() : STATIC_UTC;
172 static std::string localtime_path_prefix;
173 static std::string localtime_location;
178 class QoreOffsetZoneInfo :
public AbstractQoreZoneInfo {
180 DLLLOCAL
virtual int getUTCOffsetImpl(
int64 epoch_offset,
bool &is_dst,
const char *&zone_name)
const {
181 zone_name = name.c_str();
186 DLLLOCAL QoreOffsetZoneInfo(std::string &n_name,
int seconds_east) : AbstractQoreZoneInfo(seconds_east, n_name) {
188 DLLLOCAL QoreOffsetZoneInfo(
const char *n_name,
int seconds_east) : AbstractQoreZoneInfo(seconds_east, n_name) {
192 class QoreZoneInfo :
public AbstractQoreZoneInfo {
197 const char *std_abbr;
200 typedef std::vector<QoreDSTTransition> dst_transition_vec_t;
201 dst_transition_vec_t QoreDSTTransitions;
207 typedef std::vector<QoreLeapInfo> leap_vec_t;
211 DLLLOCAL
virtual int getUTCOffsetImpl(
int64 epoch_offset,
bool &is_dst,
const char *&zone_name)
const;
216 DLLLOCAL
virtual ~QoreZoneInfo() {
219 DLLLOCAL
operator bool()
const {
223 DLLLOCAL
const trans_vec_t &getTransitionList()
const {
229 class QoreWindowsZoneInfo :
public AbstractQoreZoneInfo {
243 SYSTEMTIME daylight_date, standard_date;
246 DLLLOCAL
virtual int getUTCOffsetImpl(
int64 epoch_offset,
bool &is_dst,
const char *&zone_name)
const;
249 DLLLOCAL
void getTransitions(
int year,
int64 &dst,
int64 &std)
const;
252 DLLLOCAL QoreWindowsZoneInfo(
const char *name,
ExceptionSink *xsink);
254 DLLLOCAL
virtual ~QoreWindowsZoneInfo() {
257 DLLLOCAL
operator bool()
const {
263 class QoreTimeZoneManager {
272 typedef std::map<std::string, AbstractQoreZoneInfo *> tzmap_t;
275 typedef std::map<int, QoreOffsetZoneInfo *> tzomap_t;
286 tzomap_t tzo_std_map;
292 AbstractQoreZoneInfo* localtz;
293 std::string localtzname;
295 DLLLOCAL
int processIntern(
const char *fn,
ExceptionSink *xsink);
296 DLLLOCAL
int process(
const char *fn);
298 DLLLOCAL
const AbstractQoreZoneInfo *processFile(
const char *fn,
bool use_path,
ExceptionSink *xsink);
299 DLLLOCAL
int processDir(
const char *d,
ExceptionSink *xsink);
302 DLLLOCAL
int setLocalTZ(std::string fname);
304 DLLLOCAL
int setLocalTZ(std::string fname, AbstractQoreZoneInfo *tzi);
306 DLLLOCAL
void setFromLocalTimeFile();
311 DLLLOCAL QoreTimeZoneManager();
313 DLLLOCAL ~QoreTimeZoneManager() {
314 for (tzmap_t::iterator i = tzmap.begin(), e = tzmap.end(); i != e; ++i) {
318 for (tzomap_t::iterator i = tzo_std_map.begin(), e = tzo_std_map.end(); i != e; ++i) {
323 for (tzomap_t::iterator i = tzomap.begin(), e = tzomap.end(); i != e; ++i) {
328 DLLLOCAL AbstractQoreZoneInfo* getZone(
const char *name) {
330 tzmap_t::iterator i = tzmap.find(name);
331 return i == tzmap.end() ? 0 : i->second;
339 DLLLOCAL
void init();
341 DLLLOCAL
const AbstractQoreZoneInfo* getLocalZoneInfo()
const {
345 DLLLOCAL
const char* getLocalRegion()
const {
346 return localtzname.empty() ? nullptr : localtzname.c_str();
349 DLLLOCAL
const AbstractQoreZoneInfo* findLoadRegion(
const char* name,
ExceptionSink* xsink);
350 DLLLOCAL
const AbstractQoreZoneInfo* findLoadRegionFromPath(
const char* name,
ExceptionSink* xsink);
353 DLLLOCAL
extern QoreTimeZoneManager QTZM;
DLLEXPORT const AbstractQoreZoneInfo * findCreateOffsetZone(int seconds_east)
returns a time zone for the given time zone UTC offset
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack,...
Definition: QoreRWLock.h:105
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:47
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260