MeteoIODoc  MeteoIODoc-2.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Date.h
Go to the documentation of this file.
1 /***********************************************************************************/
2 /* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
3 /***********************************************************************************/
4 /* This file is part of MeteoIO.
5  MeteoIO is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  MeteoIO is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifndef DATE_H
19 #define DATE_H
20 
21 #include <meteoio/IOExceptions.h>
22 
23 #include <string>
24 #include <sstream>
25 
26 //#define NEGATIVE_JULIAN
27 namespace mio {
28 
79 class Date {
80  public:
82  typedef enum {
83  ISO,
85  FULL,
86  NUM,
87  DIN,
90  } FORMATS;
91 
93  typedef enum RND_TYPE {
94  UP,
95  DOWN,
97  } RND;
98 
99  static const int daysLeapYear[];
100  static const int daysNonLeapYear[];
101  static const double DST_shift;
102  static const double MJD_offset;
103  static const double RFC868_offset;
104  static const double Unix_offset;
105  static const double Excel_offset;
106  static const double Matlab_offset;
107 
108  Date();
109  Date(const double& julian_in, const double& in_timezone, const bool& in_dst=false);
110  Date(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& in_timezone, const bool& in_dst=false);
111  Date(const time_t&, const bool& in_dst=false);
112 
113  void setFromSys();
114  void setTimeZone(const double& in_timezone, const bool& in_dst=false);
115  void setDate(const Date& in_date);
116  void setDate(const double& julian_in, const double& in_timezone, const bool& in_dst=false);
117  void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& in_timezone, const bool& in_dst=false);
118  void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const int& second, const double& in_timezone, const bool& in_dst=false);
119  void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& second, const double& in_timezone, const bool& in_dst=false);
120  void setDate(const int& year, const unsigned int& month, const unsigned int& day, const unsigned int& hour, const unsigned int& minute, const double& in_timezone, const bool& in_dst=false);
121  void setDate(const int& year, const unsigned int& month, const unsigned int& day, const unsigned int& hour, const unsigned int& minute, const unsigned int& second, const double& in_timezone, const bool& in_dst=false);
122  void setDate(const int& year, const unsigned int& month, const unsigned int& day, const unsigned int& hour, const unsigned int& minute, const double& second, const double& in_timezone, const bool& in_dst=false);
123  void setDate(const time_t& in_time, const bool& in_dst=false);
124  void setModifiedJulianDate(const double& julian_in, const double& in_timezone, const bool& in_dst=false);
125  void setRFC868Date(const double& julian_in, const double& in_timezone, const bool& in_dst=false);
126  void setUnixDate(const time_t& in_time, const bool& in_dst=false);
127  void setExcelDate(const double excel_in, const double& in_timezone, const bool& in_dst=false);
128  void setMatlabDate(const double matlab_in, const double& in_timezone, const bool& in_dst=false);
129  void setUndef(const bool& flag);
130 
131  bool isUndef() const;
132  double getTimeZone() const;
133  bool getDST() const;
134  double getJulian(const bool& gmt=false) const;
135  double getModifiedJulianDate(const bool& gmt=false) const;
136  double getTruncatedJulianDate(const bool& gmt=false) const;
137  double getRFC868Date(const bool& gmt=false) const;
138  time_t getUnixDate() const;
139  double getExcelDate(const bool& gmt=false) const;
140  double getMatlabDate(const bool& gmt=false) const;
141 
142  void getDate(double& julian_out, const bool& gmt=false) const;
143  void getDate(int& year, int& month, int& day, const bool& gmt=false) const;
144  void getDate(int& year, int& month, int& day, int& hour, const bool& gmt=false) const;
145  void getDate(int& year, int& month, int& day, int& hour, int& minute, const bool& gmt=false) const;
146  void getDate(int& year, int& month, int& day, int& hour, int& minute, int& second, const bool& gmt=false) const;
147  void getTime(int& hour_out, int& minute_out, const bool& gmt=false) const;
148  void getTime(int& hour_out, int& minute_out, int& second_out, const bool& gmt=false) const;
149  int getYear(const bool& gmt=false) const;
150 
151  unsigned short getDayOfWeek(const bool& gmt=false) const;
152  unsigned short getISOWeekNr(const bool& gmt=false) const;
153  unsigned short getISOWeekNr(int &ISO_year, const bool& gmt=false) const;
154  int getJulianDayNumber(const bool& gmt=false) const;
155  bool isLeapYear() const;
156 
157  static unsigned int mod(const double& julian, const unsigned int& seconds);
158  static unsigned int mod(const Date& indate, const unsigned int& seconds);
159  static double rnd(const double& julian, const unsigned int& precision, const RND& type=CLOSEST);
160  void rnd(const unsigned int& precision, const RND& type=CLOSEST);
161  static const Date rnd(const Date& indate, const unsigned int& precision, const RND& type=CLOSEST);
162  static double parseTimeZone(const std::string& timezone_iso);
163 
164  static std::string printFractionalDay(const double& fractional);
165  const std::string toString(const FORMATS& type, const bool& gmt=false) const;
166  const std::string toString() const;
167  friend std::iostream& operator<<(std::iostream& os, const Date& date);
168  friend std::iostream& operator>>(std::iostream& is, Date& date);
169 
170  //Operator Prototypes
171  bool operator==(const Date&) const;
172  bool operator!=(const Date&) const;
173  bool operator<(const Date&) const;
174  bool operator<=(const Date&) const;
175  bool operator>(const Date&) const;
176  bool operator>=(const Date&) const;
177 
182  Date& operator+=(const Date&);
183  Date& operator-=(const Date&);
184  Date& operator+=(const double&);
185  Date& operator-=(const double&);
186  Date& operator*=(const double&);
187  Date& operator/=(const double&);
188 
189  const Date operator+(const Date&) const;
190  const Date operator-(const Date&) const;
191  const Date operator+(const double&) const;
192  const Date operator-(const double&) const;
193  const Date operator*(const double&) const;
194  const Date operator/(const double&) const;
195 
196  protected:
197  double localToGMT(const double& in_julian) const;
198  double GMTToLocal(const double& in_gmt_julian) const;
199  double calculateJulianDate(const int& in_year, const int& in_month, const int& in_day, const int& in_hour, const int& in_minute, const double& i_second) const;
200  void calculateValues(const double& i_julian, int& out_year, int& out_month, int& out_day, int& out_hour, int& out_minute, int& o_second) const;
201  long getJulianDayNumber(const int&, const int&, const int&) const;
202  bool isLeapYear(const int&) const;
203  void plausibilityCheck(const int& in_year, const int& in_month, const int& in_day, const int& in_hour, const int& in_minute, const double& in_second) const;
204 
205  static const double epsilon;
206  double timezone;
207  double gmt_julian;
209  bool dst;
210  bool undef;
211 };
212 
213 typedef Date Duration; //so that later, we can implement a true Interval/Duration class
214 
215 } //end namespace
216 
217 #endif
unsigned short getDayOfWeek(const bool &gmt=false) const
Return the day of the week for the current date. The day of the week is between 1 (Monday) and 7 (Sun...
Definition: Date.cc:668
double getTruncatedJulianDate(const bool &gmt=false) const
Return truncated julian date (TJD). The truncated julian date is defined as the julian day shifted to...
Definition: Date.cc:412
void getDate(double &julian_out, const bool &gmt=false) const
Retrieve julian date. This method is a candidate for deletion: it should now be obsolete.
Definition: Date.cc:490
Date & operator-=(const Date &)
Definition: Date.cc:851
Date Duration
Definition: Date.h:213
double getModifiedJulianDate(const bool &gmt=false) const
Return modified julian date (MJD). The modified julian date is defined as the fractional number of da...
Definition: Date.cc:374
DIN5008 format: DD.MM.YYYY HH:MM.
Definition: Date.h:87
RND
Keywords for selecting rounding strategy.
Definition: Date.h:93
bool operator<=(const Date &) const
Definition: Date.cc:923
void setFromSys()
Set internal gmt time from system time as well as system time zone.
Definition: Date.cc:123
static const double Excel_offset
offset between julian date and Excel dates (note that excel invented some days...) ...
Definition: Date.h:105
void getTime(int &hour_out, int &minute_out, const bool &gmt=false) const
Return time of the day.
Definition: Date.cc:528
int gmt_minute
Definition: Date.h:208
ISO 8601 extended format combined date: YYYY-MM-DDTHH:mm:SS (fields might be dropped, in the least to the most significant order)
Definition: Date.h:83
rounding toward smallest absolute value
Definition: Date.h:95
double getTimeZone() const
Returns timezone.
Definition: Date.cc:331
ISO 8601 basic format date: YYYYMMDDHHmmSS (fields might be dropped, in the least to the most signifi...
Definition: Date.h:86
rounding toward highest absolute value
Definition: Date.h:94
double localToGMT(const double &in_julian) const
Definition: Date.cc:1348
static const double MJD_offset
offset between julian date and modified julian date
Definition: Date.h:102
Date()
Default constructor: timezone is set to GMT without DST, julian date is set to 0 (meaning -4713-01-01...
Definition: Date.cc:63
bool operator!=(const Date &) const
Definition: Date.cc:906
static const double epsilon
minimum difference between two dates. 1 second in units of days. 3600.025 is intentional ...
Definition: Date.h:205
static const double Matlab_offset
offset between julian date and Matlab dates
Definition: Date.h:106
Date & operator/=(const double &)
Definition: Date.cc:889
void setMatlabDate(const double matlab_in, const double &in_timezone, const bool &in_dst=false)
Set date from an Matlab date.
Definition: Date.cc:316
int gmt_hour
Definition: Date.h:208
void setModifiedJulianDate(const double &julian_in, const double &in_timezone, const bool &in_dst=false)
Set date from a modified julian date (MJD).
Definition: Date.cc:271
static std::string printFractionalDay(const double &fractional)
Nicely format an hour given as fractional day into a human readable hour.
Definition: Date.cc:1079
bool undef
Definition: Date.h:210
static double rnd(const double &julian, const unsigned int &precision, const RND &type=CLOSEST)
Round a julian date to a given precision. If you want to round a local date, do NOT provide it as gmt...
Definition: Date.cc:798
ISO 8601 format (same as ISO) but with time zone specification.
Definition: Date.h:84
ISO 8601 followed by the julian date (in parenthesis)
Definition: Date.h:85
double GMTToLocal(const double &in_gmt_julian) const
Definition: Date.cc:1356
void setTimeZone(const double &in_timezone, const bool &in_dst=false)
Set timezone and Daylight Saving Time flag.
Definition: Date.cc:141
void plausibilityCheck(const int &in_year, const int &in_month, const int &in_day, const int &in_hour, const int &in_minute, const double &in_second) const
Definition: Date.cc:1316
int getYear(const bool &gmt=false) const
Return year.
Definition: Date.cc:507
double getMatlabDate(const bool &gmt=false) const
Return Matlab date. This is the number of days since 0000-01-01T00:00:00. See http://www.mathworks.com/help/techdoc/ref/datenum.html.
Definition: Date.cc:471
rounding toward closest
Definition: Date.h:96
friend std::iostream & operator<<(std::iostream &os, const Date &date)
Definition: Date.cc:1220
bool operator>=(const Date &) const
Definition: Date.cc:949
Date & operator*=(const double &)
Definition: Date.cc:880
double getExcelDate(const bool &gmt=false) const
Return Excel date. The (sick) Excel date is defined as the number of days since 1900-01-00T00:00 (no...
Definition: Date.cc:450
static const double Unix_offset
offset between julian date and Unix Epoch time
Definition: Date.h:104
bool isLeapYear() const
Return true if the current year is a leap year.
Definition: Date.cc:757
bool operator==(const Date &) const
Definition: Date.cc:898
int gmt_day
Definition: Date.h:208
time_t getUnixDate() const
Return Unix time (or POSIX time). The Unix time is defined as the number of seconds since 1970-01-01T...
Definition: Date.cc:432
static const double RFC868_offset
offset between julian date and RFC868 time (ref is 1900-01-01T00:00 GMT)
Definition: Date.h:103
bool getDST() const
Returns Daylight Saving Time flag.
Definition: Date.cc:342
const Date operator-(const Date &) const
Definition: Date.cc:973
Date & operator+=(const Date &)
Definition: Date.cc:840
static unsigned int mod(const double &julian, const unsigned int &seconds)
Modulus of a julian date by a given number of seconds. This returns the modulus (in seconds) of a giv...
Definition: Date.cc:771
double calculateJulianDate(const int &in_year, const int &in_month, const int &in_day, const int &in_hour, const int &in_minute, const double &i_second) const
Definition: Date.cc:1253
int gmt_year
Definition: Date.h:208
unsigned short getISOWeekNr(const bool &gmt=false) const
Definition: Date.cc:726
double gmt_julian
Definition: Date.h:207
static const int daysLeapYear[]
Definition: Date.h:99
FORMATS
Keywords for selecting the date formats.
Definition: Date.h:82
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:79
bool operator<(const Date &) const
Definition: Date.cc:910
bool operator>(const Date &) const
Definition: Date.cc:936
void setUndef(const bool &flag)
Definition: Date.cc:116
void setRFC868Date(const double &julian_in, const double &in_timezone, const bool &in_dst=false)
Set date from an RFC868 date (time since 1900-01-01T00:00 GMT).
Definition: Date.cc:282
double getJulian(const bool &gmt=false) const
Return julian date (JD). The julian date is defined as the fractional number of days since -4713-01-0...
Definition: Date.cc:355
void setDate(const Date &in_date)
Copy setter.
Definition: Date.cc:155
ISO 8601 week date: YYYY-Www-D (for example: 2014-W41-1)
Definition: Date.h:88
void calculateValues(const double &i_julian, int &out_year, int &out_month, int &out_day, int &out_hour, int &out_minute, int &o_second) const
Definition: Date.cc:1261
int getJulianDayNumber(const bool &gmt=false) const
Return the julian day for the current date. Return the day of the year index for the current Date obj...
Definition: Date.cc:738
double timezone
Definition: Date.h:206
int gmt_second
Definition: Date.h:208
static const int daysNonLeapYear[]
Definition: Date.h:100
ISO 8601 date format without the time (ie YYYY-MM-DD)
Definition: Date.h:89
bool dst
Definition: Date.h:209
void setUnixDate(const time_t &in_time, const bool &in_dst=false)
Set date from a Unix date.
Definition: Date.cc:292
double getRFC868Date(const bool &gmt=false) const
Return RFC868 date. The RFC868 date is defined as the fractional number of days since 1900-01-01T00:0...
Definition: Date.cc:392
const Date operator/(const double &) const
Definition: Date.cc:1005
int gmt_month
Definition: Date.h:208
bool isUndef() const
Definition: Date.cc:323
void setExcelDate(const double excel_in, const double &in_timezone, const bool &in_dst=false)
Set date from an Excel date.
Definition: Date.cc:303
const Date operator+(const Date &) const
Definition: Date.cc:962
const std::string toString() const
Definition: Date.cc:1197
static double parseTimeZone(const std::string &timezone_iso)
Parse an ISO 8601 formatted time zone specification. Time zones MUST be specified right after a date/...
Definition: Date.cc:1025
static const double DST_shift
Definition: Date.h:101
friend std::iostream & operator>>(std::iostream &is, Date &date)
Definition: Date.cc:1236
const Date operator*(const double &) const
Definition: Date.cc:998