TZ-Lib  $VERSION$
tzdata.h
Go to the documentation of this file.
1 //
2 // C++ Interface: TZ Data
3 //
4 // Description: Time Zone Calculator
5 //
6 //
7 // Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2010
8 //
9 // Copyright: See README/COPYING files that come with this distribution
10 //
11 //
12 
13 #ifndef TIME_ZONE_DATA_H
14 #define TIME_ZONE_DATA_H
15 
16 #include <QDate>
17 #include <QDateTime>
18 #include <QString>
19 #include <QTime>
20 
21 
22 #if defined(TZ_LIBRARY_BUILD)
23 # define TZLIB_EXPORT Q_DECL_EXPORT
24 #else
25 # define TZLIB_EXPORT Q_DECL_IMPORT
26 #endif
27 
28 
29 namespace TimeZoneLib {class TZFile;}
30 
39 {
40  public:
42  enum WeekDay{
43  Sunday=0,
44  Monday=1,
45  Tuesday=2,
46  Wednesday=3,
47  Thursday=4,
48  Friday=5,
49  Saturday=6
50  };
51 
53  TimeStamp();
54 
58  TimeStamp(qint64 ts,bool isLocal=true);
62  TimeStamp(qint64 ts,QString zone);
67  TimeStamp(qint64 ts,quint16 msec,bool isLocal=true);
72  TimeStamp(qint64 ts,quint16 msec,QString zone);
73 
75  TimeStamp(const QDate&,bool isLocal=true);
77  TimeStamp(const QDate&,QString);
78 
80  TimeStamp(const QTime&,bool isLocal=true);
82  TimeStamp(const QTime&,QString);
83 
86  TimeStamp(const QDateTime&ts);
90  TimeStamp(const QDateTime&ts,bool isLocal);
94  TimeStamp(const QDateTime&ts,QString zone);
95 
97  TimeStamp(const TimeStamp&);
99  TimeStamp& operator=(const TimeStamp&);
100 
104  bool setZone(QString);
105 
107  void setLocal(){setZone(defaultZone());}
108 
110  void setUTC(){setZone("UTC");}
111 
115  bool moveToZone(QString);
116 
118  void moveToUTC(){moveToZone("UTC");}
119 
121  void moveToLocal(){moveToZone(defaultZone());}
122 
124  QString zone()const{return m_zone;}
125 
127  TimeStamp toUTC()const{return TimeStamp(m_unix,m_msec,false);}
128 
130  TimeStamp toLocal()const{return TimeStamp(m_unix,m_msec,true);}
131 
133  TimeStamp toZone(QString zone)const{return TimeStamp(m_unix,m_msec,zone);}
134 
136  QDate toDate()const{return QDate(m_year,m_month,m_day);}
138  operator QDate()const{return QDate(m_year,m_month,m_day);}
139 
141  QTime toTime()const{return QTime(m_hour,m_min,m_sec,m_msec);}
143  operator QTime()const{return QTime(m_hour,m_min,m_sec,m_msec);}
144 
146  QDateTime toDateTime()const;
148  operator QDateTime()const{return toDateTime();}
149 
151  QDateTime toSystemDateTimeUTC()const{return toUTC().toDateTime();}
153  QDateTime toSystemDateTime()const{return toSystemDateTimeUTC().toLocalTime();}
154 
156  void addMSecs(int msec);
157 
159  void addSecs(int sec);
160 
162  void addDays(int d);
163 
165  qint64 toUnix()const{return m_unix;}
166 
168  qint16 year()const{return m_year;}
170  quint8 month()const{return m_month;}
172  quint8 day()const{return m_day;}
174  quint8 hour()const{return m_hour;}
176  quint8 minute()const{return m_min;}
178  quint8 second()const{return m_sec;}
180  quint16 msecs()const{return m_msec;}
181 
183  WeekDay weekDay()const;
184 
186  QString zoneAbbreviation()const;
187 
189  QString toISO()const;
190 
191 
193  qint32 offsetFromUTC()const{return m_off;}
194 
196  bool operator==(const TimeStamp&ts)const
197  {return m_unix==ts.m_unix && m_msec==ts.m_msec;}
199  bool operator!=(const TimeStamp&ts)const
200  {return m_unix!=ts.m_unix || m_msec!=ts.m_msec;}
201 
203  bool operator<(const TimeStamp&ts)const
204  {return m_unix<ts.m_unix || (m_unix==ts.m_unix && m_msec<ts.m_msec);}
206  bool operator<=(const TimeStamp&ts)const
207  {return m_unix<ts.m_unix || (m_unix==ts.m_unix && m_msec<=ts.m_msec);}
208 
210  bool operator>(const TimeStamp&ts)const
211  {return m_unix>ts.m_unix || (m_unix==ts.m_unix && m_msec>ts.m_msec);}
213  bool operator>=(const TimeStamp&ts)const
214  {return m_unix>ts.m_unix || (m_unix==ts.m_unix && m_msec>=ts.m_msec);}
215 
216  // /////////////////////////////////////////////////////////////
217  // statics
219  static TimeStamp fromDate(int year,quint8 month,quint8 day,bool isLocal=true)
220  {return TimeStamp(QDate(year,month,day),isLocal);}
222  static TimeStamp fromDate(QDate dt,bool isLocal=true)
223  {return TimeStamp(dt,isLocal);}
225  static TimeStamp fromDate(int year,quint8 month,quint8 day,QString zone)
226  {return TimeStamp(QDate(year,month,day),zone);}
228  static TimeStamp fromDate(QDate dt,QString zone)
229  {return TimeStamp(dt,zone);}
230 
232  static TimeStamp fromTime(quint8 hour,quint8 minute,bool isLocal=true)
233  {return TimeStamp(QTime(hour,minute),isLocal);}
235  static TimeStamp fromTime(quint8 hour,quint8 minute,quint8 second,bool isLocal=true)
236  {return TimeStamp(QTime(hour,minute,second),isLocal);}
238  static TimeStamp fromTime(quint8 hour,quint8 minute,QString zone)
239  {return TimeStamp(QTime(hour,minute),zone);}
241  static TimeStamp fromTime(quint8 hour,quint8 minute,quint8 second,QString zone)
242  {return TimeStamp(QTime(hour,minute,second),zone);}
244  static TimeStamp fromTime(QTime tm,bool isLocal=true)
245  {return TimeStamp(tm,isLocal);}
247  static TimeStamp fromTime(QTime tm,QString zone)
248  {return TimeStamp(tm,zone);}
249 
251  static TimeStamp fromDateTime(QDateTime dt)
252  {return TimeStamp(dt);}
254  static TimeStamp fromDateTime(QDateTime dt,bool isLocal)
255  {return TimeStamp(dt,isLocal);}
257  static TimeStamp fromDateTime(QDateTime dt,QString zone)
258  {return TimeStamp(dt,zone);}
259 
261  static TimeStamp fromDateTime(QDate dt,QTime tm)
262  {return TimeStamp(QDateTime(dt,tm));}
264  static TimeStamp fromDateTime(QDate dt,QTime tm,bool isLocal)
265  {return TimeStamp(QDateTime(dt,tm),isLocal);}
267  static TimeStamp fromDateTime(QDate dt,QTime tm,QString zone)
268  {return TimeStamp(QDateTime(dt,tm),zone);}
269 
271  static TimeStamp fromDateTime(int year,quint8 month,quint8 day,quint8 hour,quint8 minute,bool isLocal=true)
272  {return fromDateTime(QDate(year,month,day),QTime(hour,minute),isLocal);}
274  static TimeStamp fromDateTime(int year,quint8 month,quint8 day,quint8 hour,quint8 minute,quint8 second,bool isLocal=true)
275  {return fromDateTime(QDate(year,month,day),QTime(hour,minute,second),isLocal);}
276 
279  static TimeStamp now(QString zone){return TimeStamp(QDateTime::currentDateTime().toTime_t(),zone);}
282  static TimeStamp now(bool isLocal=true){return TimeStamp(QDateTime::currentDateTime().toTime_t(),isLocal);}
283 
285  static void setSearchPath(const QStringList&);
286 
288  static void resetRepository();
289 
293  static bool setDefaultZone(QString zone);
294 
296  static QString defaultZone();
297 
299  static void storeDefaultZone();
300 
302  static void forgetDefaultZone();
303 
305  static bool loadZone(QString zone);
306 
308  static QString systemLocalZone();
309 
311  TimeZoneLib::TZFile zoneFile()const;
312 
314  static TimeZoneLib::TZFile defaultZoneFile();
315 
317  static TimeZoneLib::TZFile systemLocalZoneFile();
318 
319  private:
320  //the main measure of time: the unix timestamp
321  qint64 m_unix;
322  //milliseconds beyond that time
323  quint16 m_msec;
324  //the time zone
325  QString m_zone;
326  //cache values to make calculations easier
327  qint16 m_year;
328  qint32 m_off;
329  quint8 m_month,m_day,m_hour,m_min,m_sec;
330 
332  void recalcToCached();
333 
335  void recalcToUnix();
336 };
337 
338 
339 
340 #endif
Definition: tzdata.h:29
static TimeStamp fromTime(quint8 hour, quint8 minute, QString zone)
Definition: tzdata.h:238
static TimeStamp fromTime(QTime tm, bool isLocal=true)
Definition: tzdata.h:244
static TimeStamp fromDate(int year, quint8 month, quint8 day, bool isLocal=true)
Definition: tzdata.h:219
static TimeStamp fromDateTime(QDate dt, QTime tm)
Definition: tzdata.h:261
bool operator<=(const TimeStamp &ts) const
Definition: tzdata.h:206
static TimeStamp fromDateTime(int year, quint8 month, quint8 day, quint8 hour, quint8 minute, quint8 second, bool isLocal=true)
Definition: tzdata.h:274
QTime toTime() const
Definition: tzdata.h:141
static TimeStamp fromDateTime(QDate dt, QTime tm, bool isLocal)
Definition: tzdata.h:264
static TimeStamp fromDateTime(QDateTime dt, QString zone)
Definition: tzdata.h:257
static TimeStamp fromDateTime(QDate dt, QTime tm, QString zone)
Definition: tzdata.h:267
static TimeStamp now(QString zone)
Definition: tzdata.h:279
static TimeStamp fromTime(quint8 hour, quint8 minute, quint8 second, QString zone)
Definition: tzdata.h:241
qint64 toUnix() const
Definition: tzdata.h:165
TimeStamp toZone(QString zone) const
Definition: tzdata.h:133
quint8 minute() const
Definition: tzdata.h:176
Definition: tzdata.h:38
void setUTC()
Definition: tzdata.h:110
bool operator<(const TimeStamp &ts) const
Definition: tzdata.h:203
static TimeStamp fromDateTime(QDateTime dt)
Definition: tzdata.h:251
bool operator!=(const TimeStamp &ts) const
Definition: tzdata.h:199
quint8 hour() const
Definition: tzdata.h:174
bool operator>(const TimeStamp &ts) const
Definition: tzdata.h:210
qint32 offsetFromUTC() const
Definition: tzdata.h:193
quint8 second() const
Definition: tzdata.h:178
static TimeStamp now(bool isLocal=true)
Definition: tzdata.h:282
TimeStamp toUTC() const
Definition: tzdata.h:127
QDateTime toSystemDateTime() const
Definition: tzdata.h:153
static TimeStamp fromDate(QDate dt, QString zone)
Definition: tzdata.h:228
#define TZLIB_EXPORT
Definition: tzdata.h:25
QString zone() const
Definition: tzdata.h:124
static TimeStamp fromDateTime(QDateTime dt, bool isLocal)
Definition: tzdata.h:254
quint16 msecs() const
Definition: tzdata.h:180
static TimeStamp fromDateTime(int year, quint8 month, quint8 day, quint8 hour, quint8 minute, bool isLocal=true)
Definition: tzdata.h:271
static TimeStamp fromTime(QTime tm, QString zone)
Definition: tzdata.h:247
void setLocal()
Definition: tzdata.h:107
QDate toDate() const
Definition: tzdata.h:136
Definition: include/tzfile.h:149
static TimeStamp fromTime(quint8 hour, quint8 minute, quint8 second, bool isLocal=true)
Definition: tzdata.h:235
TimeStamp toLocal() const
Definition: tzdata.h:130
WeekDay
Definition: tzdata.h:42
static TimeStamp fromDate(int year, quint8 month, quint8 day, QString zone)
Definition: tzdata.h:225
static TimeStamp fromTime(quint8 hour, quint8 minute, bool isLocal=true)
Definition: tzdata.h:232
bool operator>=(const TimeStamp &ts) const
Definition: tzdata.h:213
quint8 month() const
Definition: tzdata.h:170
Definition: zic.c:90
static TimeStamp fromDate(QDate dt, bool isLocal=true)
Definition: tzdata.h:222
QDateTime toSystemDateTimeUTC() const
Definition: tzdata.h:151
static struct tm tm
Definition: localtime.c:189
quint8 day() const
Definition: tzdata.h:172
void moveToLocal()
Definition: tzdata.h:121
qint16 year() const
Definition: tzdata.h:168
void moveToUTC()
Definition: tzdata.h:118
bool operator==(const TimeStamp &ts) const
Definition: tzdata.h:196