|
Qore Programming Language
0.8.3
|
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */ 00002 /* 00003 DateTime.h 00004 00005 Qore programming language 00006 00007 Copyright 2003 - 2011 David Nichols 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #ifndef QORE_DATETIME_H 00025 00026 #define QORE_DATETIME_H 00027 00028 #include <time.h> 00029 00030 class AbstractQoreZoneInfo; 00031 00033 struct qore_tm { 00034 int year, month, day, hour, minute, second, us, utc_secs_east; 00035 bool dst; 00036 const char *zone_name; 00037 const AbstractQoreZoneInfo *zone; 00038 00039 DLLLOCAL bool isTimeNull() const { 00040 return !hour && !minute && !second && !us; 00041 } 00042 00044 DLLEXPORT int secsEast() const; 00045 00047 DLLEXPORT const char *regionName() const; 00048 }; 00049 00051 00071 class DateTime { 00072 friend class DateTimeNode; 00073 00074 protected: 00076 struct qore_date_private *priv; 00077 00078 DLLLOCAL void setDateLiteral(int64 date); 00079 DLLLOCAL void setRelativeDateLiteral(int64 date); 00080 00082 DLLLOCAL DateTime& operator=(const DateTime&); 00083 00085 DLLLOCAL DateTime(qore_date_private *n_priv); 00086 00087 public: 00089 00092 DLLEXPORT DateTime(bool r = false); 00093 00095 00105 DLLEXPORT DateTime(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, short n_ms = 0, bool n_relative = false); 00106 00108 00111 DLLEXPORT DateTime(int64 seconds); 00112 00114 00118 DLLEXPORT DateTime(int64 seconds, int ms); 00119 00121 00124 DLLEXPORT DateTime(const char *date); 00125 00127 00130 DLLEXPORT DateTime(const struct tm *tms); 00131 00133 DLLEXPORT DateTime(const DateTime &dt); 00134 00136 DLLEXPORT ~DateTime(); 00137 00139 DLLEXPORT void getTM(struct tm *tms) const; 00140 00142 00145 DLLEXPORT void setDate(int64 seconds); 00146 00148 00152 DLLEXPORT void setDate(int64 seconds, int ms); 00153 00155 00159 DLLEXPORT void setDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us); 00160 00162 00166 DLLEXPORT void setLocalDate(const AbstractQoreZoneInfo *zone, int64 seconds, int us); 00167 00169 DLLEXPORT void setDate(const AbstractQoreZoneInfo *n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0); 00170 00172 00176 DLLEXPORT void setDate(const char *str); 00177 00179 DLLEXPORT void setRelativeDate(const char *str); 00180 00182 DLLEXPORT void setDate(const struct tm *tms, short ms = 0); 00183 00185 DLLEXPORT void setDate(const DateTime &date); 00186 00188 00194 DLLEXPORT void setTime(int h, int m, int s, short ms = 0); 00195 00196 DLLEXPORT bool checkValidity() const; 00197 DLLEXPORT bool isEqual(const DateTime *dt) const; 00198 DLLEXPORT DateTime *add(const DateTime *dt) const; 00199 DLLEXPORT DateTime *subtractBy(const DateTime *dt) const; 00200 00202 00205 DLLEXPORT int64 getEpochSeconds() const; 00206 00208 00211 DLLEXPORT int64 getEpochSecondsUTC() const; 00212 00214 00217 DLLEXPORT int64 getEpochMicrosecondsUTC() const; 00218 00220 00223 DLLEXPORT int64 getEpochMillisecondsUTC() const; 00224 00226 00229 DLLEXPORT int getDayNumber() const; 00230 00232 00235 DLLEXPORT int getDayOfWeek() const; 00236 00238 00243 DLLEXPORT void getISOWeek(int &year, int &week, int &day) const; 00244 00246 00285 DLLEXPORT void format(QoreString &str, const char *fmt) const; 00286 00288 00291 DLLEXPORT bool isRelative() const; 00292 00294 00297 DLLEXPORT bool isAbsolute() const; 00298 00300 00303 DLLEXPORT short getYear() const; 00304 00306 00309 DLLEXPORT int getMonth() const; 00310 00312 00315 DLLEXPORT int getDay() const; 00316 00318 00321 DLLEXPORT int getHour() const; 00322 00324 00327 DLLEXPORT int getMinute() const; 00328 00330 00333 DLLEXPORT int getSecond() const; 00334 00336 00339 DLLEXPORT int getMillisecond() const; 00340 00342 00344 DLLEXPORT int getMicrosecond() const; 00345 00347 00350 DLLEXPORT int64 getRelativeSeconds() const; 00351 00353 00356 DLLEXPORT int64 getRelativeMilliseconds() const; 00357 00359 00362 DLLEXPORT int64 getRelativeMicroseconds() const; 00363 00365 DLLEXPORT bool hasValue() const; 00366 00368 DLLEXPORT DateTime *unaryMinus() const; 00369 00371 DLLEXPORT void unaryMinusInPlace(); 00372 00374 DLLEXPORT void getInfo(const AbstractQoreZoneInfo *n_zone, qore_tm &info) const; 00375 00377 DLLEXPORT void getInfo(qore_tm &info) const; 00378 00380 DLLEXPORT void setZone(const AbstractQoreZoneInfo *n_zone); 00381 00382 // static methods 00384 DLLEXPORT static bool isLeapYear(int year); 00385 00387 DLLEXPORT static int getLastDayOfMonth(int month, int year); 00388 00390 00398 DLLEXPORT static DateTime *getDateFromISOWeek(int year, int week, int day, ExceptionSink *xsink); 00399 00401 DLLEXPORT static int compareDates(const DateTime *left, const DateTime *right); 00402 00404 DLLEXPORT static DateTime *makeAbsolute(const AbstractQoreZoneInfo *n_zone, int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0); 00405 00407 00412 DLLEXPORT static DateTime *makeAbsolute(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0); 00413 00415 00420 DLLEXPORT static DateTime *makeAbsoluteLocal(const AbstractQoreZoneInfo *zone, int64 seconds, int us = 0); 00421 00423 DLLEXPORT static DateTime *makeRelative(int n_year, int n_month, int n_day, int n_hour = 0, int n_minute = 0, int n_second = 0, int n_us = 0); 00424 }; 00425 00426 #endif