MeteoIODoc 20260811.aeb9aef5
Environmental timeseries pre-processing
Loading...
Searching...
No Matches
Date.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef DATE_H
20#define DATE_H
21
23
24#include <string>
25#include <map>
26
27//uncomment below to support negative julian dates
28//#define NEGATIVE_JULIAN
29
30namespace mio {
31
86class Date {
87 public:
99
101 typedef enum RND_TYPE {
104 CLOSEST
106
107 static const double DST_shift;
108 static const double MJD_offset;
109 static const double RFC868_offset;
110 static const double Unix_offset;
111 static const double Excel_offset;
112 static const double Matlab_offset;
113 static const double epsilon_sec;
114
115 Date();
116 Date(const double& in_timezone);
117 Date(const double& julian_in, const double& in_timezone);
118 Date(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& in_timezone);
119 Date(const int& year, const int& month, const int& day, const int& hour, const int& minute, const int& second, const double& in_timezone);
120 Date(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& second, const double& in_timezone);
121 Date(const time_t&);
122 Date(const int& year, const double& jdn, const double& in_timezone);
123
124 void setFromSys();
125 void setTimeZone(const double& in_timezone);
126 void setDate(const Date& in_date);
127 void setDate(const double& julian_in, const double& in_timezone);
128 void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& in_timezone);
129 void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const int& second, const double& in_timezone);
130 void setDate(const int& year, const int& month, const int& day, const int& hour, const int& minute, const double& second, const double& in_timezone);
131 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);
132 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);
133 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);
134 void setDate(const int& year, const double& jdn, const double& in_timezone);
135 void setDate(const time_t& in_time);
136 void setModifiedJulianDate(const double& julian_in, const double& in_timezone);
137 void setRFC868Date(const double& julian_in, const double& in_timezone);
138 void setUnixDate(const time_t& in_time);
139 void setExcelDate(const double excel_in, const double& in_timezone);
140 void setMatlabDate(const double matlab_in, const double& in_timezone);
141 void setUndef(const bool& flag=true);
142
143 bool isUndef() const {return undef;}
144 double getTimeZone() const;
145 double getJulian(const bool& gmt=false) const;
146 double getModifiedJulianDate(const bool& gmt=false) const;
147 double getTruncatedJulianDate(const bool& gmt=false) const;
148 double getRFC868Date(const bool& gmt=false) const;
149 time_t getUnixDate() const;
150 double getExcelDate(const bool& gmt=false) const;
151 double getMatlabDate(const bool& gmt=false) const;
152
153 void getDate(double& julian_out, const bool& gmt=false) const;
154 void getDate(int& year, int& month, int& day, const bool& gmt=false) const;
155 void getDate(int& year, int& month, int& day, int& hour, const bool& gmt=false) const;
156 void getDate(int& year, int& month, int& day, int& hour, int& minute, const bool& gmt=false) const;
157 void getDate(int& year, int& month, int& day, int& hour, int& minute, double& second, const bool& gmt=false) const;
158 void getDate(int& year, int& month, int& day, int& hour, int& minute, int& second, const bool& gmt=false) const;
159 void getTime(int& hour_out, int& minute_out, const bool& gmt=false) const;
160 void getTime(int& hour_out, int& minute_out, double& second_out, const bool& gmt=false) const;
161 int getYear(const bool& gmt=false) const;
162
163 unsigned short getDayOfWeek(const bool& gmt=false) const;
164 unsigned short getISOWeekNr(const bool& gmt=false) const;
165 unsigned short getISOWeekNr(int &ISO_year, const bool& gmt=false) const;
166 static int getNumberOfDaysInMonth(const int &ISO_year, const int &month);
167 int getJulianDayNumber(const bool& gmt=false) const;
168 bool isLeapYear() const;
169
170 static unsigned int mod(const double& julian, const unsigned int& seconds);
171 static unsigned int mod(const Date& indate, const unsigned int& seconds);
172 static double rnd(const double& julian, const double& precision, const RND& type=CLOSEST);
173 void rnd(const double& precision, const RND& type=CLOSEST);
174 static const Date rnd(const Date& indate, const double& precision, const RND& type=CLOSEST);
175 static double parseTimeZone(const std::string& timezone_iso);
176
177 static std::string printFractionalDay(const double& fractional);
178 static std::string printISODuration(const Date& dt1, const Date& dt2);
179 const std::string toString(const FORMATS& type, const bool& gmt=false) const;
180 const std::string toString() const;
181 friend std::ostream& operator<<(std::ostream& os, const Date& date);
182 friend std::istream& operator>>(std::istream& is, Date& date);
183
184 //Operator Prototypes
185 bool operator==(const Date&) const;
186 bool operator!=(const Date&) const;
187 bool operator<(const Date&) const;
188 bool operator<=(const Date&) const;
189 bool operator>(const Date&) const;
190 bool operator>=(const Date&) const;
191
196 Date& operator+=(const Date&);
197 Date& operator-=(const Date&);
198 Date& operator+=(const double&);
199 Date& operator-=(const double&);
200 Date& operator*=(const double&);
201 Date& operator/=(const double&);
202
203 const Date operator+(const Date&) const;
204 const Date operator-(const Date&) const;
205 const Date operator+(const double&) const;
206 const Date operator-(const double&) const;
207 const Date operator*(const double&) const;
208 const Date operator/(const double&) const;
209
210 static const double epsilon;
211
212 protected:
213 double localToGMT(const double& in_julian) const;
214 double GMTToLocal(const double& in_gmt_julian) const;
215 static 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);
216 static void calculateDate(const double& i_julian, int& out_year, int& out_month, int& out_day);
217 static void calculateTime(const double& i_julian, int& o_hour, int& o_minute, double& o_second);
218 static void calculateValues(const double& i_julian, int& out_year, int& out_month, int& out_day, int& out_hour, int& out_minute, double& o_second);
219 static long getJulianDayNumber(const int&, const int&, const int&);
220 static bool isLeapYear(const int&);
221 static 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);
222 static bool initStaticData();
223
224 static std::map< std::string, double> TZAbbrev;
225 static const int daysLeapYear[];
226 static const int daysNonLeapYear[];
227 static const bool __init;
228 double timezone;
230 bool undef;
231};
232
243 public:
244 DateRange() : start(), end(), isValid(false) {}
245 DateRange(const Date& d1, const Date& d2) : start(d1), end(d2), isValid(false) {if (!start.isUndef() && !end.isUndef() && start<=end) isValid=true;}
246 //at most daily resolution
247 DateRange(const std::string& range_spec, const double& tz) : start(), end(), isValid(false) {setRange(range_spec, tz);}
248
249 void setRange(const std::string& range_spec, const double& tz);
250 void resetRange(const Date& i_start, const Date& i_end);
251
252 bool isUndef() const {return (!isValid);}
253
259 bool in(const Date& a) const {
260 if (!isValid) throw InvalidArgumentException("Date range '"+toString()+"' has not been properly initialized!", AT);
261 return (a >= start && a <= end);
262 }
263
270 bool hasOverlap(const Date& i_start, const Date& i_end) const;
271
277 bool hasOverlap(const DateRange& i_range) const;
278
284 bool operator<(const Date& a) const {
285 if (!isValid) throw InvalidArgumentException("Date range '"+toString()+"' has not been properly initialized!", AT);
286 return end < a;
287 }
288
294 bool operator>(const Date& a) const {
295 if (!isValid) throw InvalidArgumentException("Date range '"+toString()+"' has not been properly initialized!", AT);
296 return start > a;
297 }
298
299 bool operator<(const DateRange& a) const { //needed for "sort"
300 if (!isValid) throw InvalidArgumentException("Date range '"+toString()+"' has not been properly initialized!", AT);
301 if (!a.isValid) throw InvalidArgumentException("Date range '"+a.toString()+"' has not been properly initialized!", AT);
302 if (start==a.start) return end < a.end;
303 return start < a.start;
304 }
305
306 bool operator==(const DateRange& a) const { //needed to check for uniqueness
307 if (!isValid) throw InvalidArgumentException("Date range '"+toString()+"' has not been properly initialized!", AT);
308 if (!a.isValid) throw InvalidArgumentException("Date range '"+a.toString()+"' has not been properly initialized!", AT);
309 return (start==a.start) && (end==a.end);
310 }
311
312 bool operator!=(const DateRange& a) const { return !(*this==a); }
313
314 const std::string toString() const;
315
317
318 private:
319 static Date parseDateHint(const std::string& Date_str, const double tz, const bool& early_interpretation);
320
321 bool isValid;
322};
323
324typedef Date Duration; //so that later, we can implement a true Interval/Duration class
325
326} //end namespace
327
328#endif
#define AT
Definition IOExceptions.h:28
A class to represent and handle date ranges. They can be sorted, checked for uniqueness and a date ca...
Definition Date.h:242
DateRange()
Definition Date.h:244
bool operator<(const DateRange &a) const
Definition Date.h:299
DateRange(const std::string &range_spec, const double &tz)
Definition Date.h:247
Date end
Definition Date.h:316
bool operator<(const Date &a) const
Is the range before the provided date?
Definition Date.h:284
Date start
Definition Date.h:316
void resetRange(const Date &i_start, const Date &i_end)
Definition Date.cc:1520
bool hasOverlap(const Date &i_start, const Date &i_end) const
Is the provided date range within the current range?
Definition Date.cc:1541
bool isUndef() const
Definition Date.h:252
bool operator==(const DateRange &a) const
Definition Date.h:306
bool operator>(const Date &a) const
Is the range after the provided date?
Definition Date.h:294
bool operator!=(const DateRange &a) const
Definition Date.h:312
DateRange(const Date &d1, const Date &d2)
Definition Date.h:245
const std::string toString() const
Definition Date.cc:1529
bool in(const Date &a) const
Is the provided date within the current range?
Definition Date.h:259
void setRange(const std::string &range_spec, const double &tz)
Set the date range from a provided date range string (at most daily resolution)
Definition Date.cc:1497
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition Date.h:86
double GMTToLocal(const double &in_gmt_julian) const
Definition Date.cc:1440
void setModifiedJulianDate(const double &julian_in, const double &in_timezone)
Set date from a modified julian date (MJD).
Definition Date.cc:352
double gmt_julian
Definition Date.h:229
bool isLeapYear() const
Return true if the current year is a leap year.
Definition Date.cc:800
bool operator>=(const Date &) const
Definition Date.cc:987
static 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)
Definition Date.cc:1315
static void calculateTime(const double &i_julian, int &o_hour, int &o_minute, double &o_second)
Calculate the time components (y,m,d) from a given julian date.
Definition Date.cc:1361
static void calculateValues(const double &i_julian, int &out_year, int &out_month, int &out_day, int &out_hour, int &out_minute, double &o_second)
Definition Date.cc:1373
static const int daysLeapYear[]
Definition Date.h:225
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:509
static const double Excel_offset
offset between julian date and Excel dates (note that excel invented some days...)
Definition Date.h:111
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:547
static const double MJD_offset
offset between julian date and modified julian date
Definition Date.h:108
bool operator<=(const Date &) const
Definition Date.cc:961
void getTime(int &hour_out, int &minute_out, const bool &gmt=false) const
Return time of the day.
Definition Date.cc:576
const Date operator+(const Date &) const
Definition Date.cc:1000
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:435
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:685
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:1063
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:814
double localToGMT(const double &in_julian) const
Definition Date.cc:1435
void setTimeZone(const double &in_timezone)
Set timezone and Daylight Saving Time flag.
Definition Date.cc:219
void setMatlabDate(const double matlab_in, const double &in_timezone)
Set date from an Matlab date.
Definition Date.cc:393
const Date operator/(const double &) const
Definition Date.cc:1043
static double rnd(const double &julian, const double &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:842
static const double Unix_offset
offset between julian date and Unix Epoch time
Definition Date.h:110
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:491
void setFromSys()
Set internal gmt time from system time as well as system time zone.
Definition Date.cc:202
static std::map< std::string, double > TZAbbrev
Definition Date.h:224
Date & operator+=(const Date &)
Definition Date.cc:884
static std::string printISODuration(const Date &dt1, const Date &dt2)
Return the difference between two dates formatted as duration according to ISO8601....
Definition Date.cc:1149
Date & operator*=(const double &)
Definition Date.cc:920
void setUnixDate(const time_t &in_time)
Set date from a Unix date.
Definition Date.cc:371
double getTimeZone() const
Returns timezone.
Definition Date.cc:404
FORMATS
Keywords for selecting the date formats (the subsecond resolution is dropped when not needed)
Definition Date.h:89
@ ISO_TZ
ISO 8601 format (same as ISO) but with time zone specification.
Definition Date.h:91
@ ISO_Z
ISO 8601 format, forcing GMT and Zulu (Z) timezone specification.
Definition Date.h:92
@ ISO_DATE
ISO 8601 date format without the time (ie YYYY-MM-DD)
Definition Date.h:97
@ ISO
ISO 8601 extended format combined date: YYYY-MM-DDTHH:mm:SS.sss (fields might be dropped,...
Definition Date.h:90
@ ISO_WEEK
ISO 8601 week date: YYYY-Www-D (for example: 2014-W41-1)
Definition Date.h:96
@ NUM
ISO 8601 basic format date: YYYYMMDDHHmmSS (fields might be dropped, in the least to the most signifi...
Definition Date.h:94
@ FULL
ISO 8601 followed by the julian date (in parenthesis)
Definition Date.h:93
@ DIN
DIN5008 format: DD.MM.YYYY HH:MM:SS.sss.
Definition Date.h:95
static const double epsilon_sec
Definition Date.h:113
unsigned short getISOWeekNr(const bool &gmt=false) const
Return the ISO 8601 week number The week number range from 1 to 53 for a leap year....
Definition Date.cc:768
RND
Keywords for selecting rounding strategy.
Definition Date.h:101
@ CLOSEST
rounding toward closest
Definition Date.h:104
@ DOWN
rounding toward smallest absolute value
Definition Date.h:103
@ UP
rounding toward highest absolute value
Definition Date.h:102
friend std::ostream & operator<<(std::ostream &os, const Date &date)
Definition Date.cc:1300
bool undef
Definition Date.h:230
static const double Matlab_offset
offset between julian date and Matlab dates
Definition Date.h:112
static std::string printFractionalDay(const double &fractional)
Nicely format an hour given as fractional day into a human readable hour.
Definition Date.cc:1126
Date & operator/=(const double &)
Definition Date.cc:928
bool operator<(const Date &) const
Definition Date.cc:948
const Date operator*(const double &) const
Definition Date.cc:1036
static const double RFC868_offset
offset between julian date and RFC868 time (ref is 1900-01-01T00:00 GMT)
Definition Date.h:109
static void calculateDate(const double &i_julian, int &out_year, int &out_month, int &out_day)
Calculate the date components (y,m,d) from a given julian date.
Definition Date.cc:1332
const std::string toString() const
Definition Date.cc:1277
double timezone
Definition Date.h:228
void setRFC868Date(const double &julian_in, const double &in_timezone)
Set date from an RFC868 date (time since 1900-01-01T00:00 GMT, valid until 2036).
Definition Date.cc:362
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:780
Date()
Default constructor: timezone is set to GMT without DST, julian date is set to 0 (meaning -4713-01-01...
Definition Date.cc:118
bool operator==(const Date &) const
Definition Date.cc:936
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:417
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:452
static 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)
Definition Date.cc:1403
Date & operator-=(const Date &)
Definition Date.cc:894
static const bool __init
Definition Date.h:227
static const int daysNonLeapYear[]
Definition Date.h:226
void setExcelDate(const double excel_in, const double &in_timezone)
Set date from an Excel date.
Definition Date.cc:381
static const double DST_shift
Definition Date.h:107
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....
Definition Date.cc:529
bool operator>(const Date &) const
Definition Date.cc:974
bool operator!=(const Date &) const
Definition Date.cc:944
void setDate(const Date &in_date)
Copy setter.
Definition Date.cc:232
int getYear(const bool &gmt=false) const
Return year.
Definition Date.cc:559
const Date operator-(const Date &) const
Definition Date.cc:1011
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:471
static int getNumberOfDaysInMonth(const int &ISO_year, const int &month)
Return the number of days of the given month for the given year The returned day is between 28 (ferbu...
Definition Date.cc:703
static const double epsilon
minimum difference between two dates in days. 3600.025 is intentional to support high sampling rates
Definition Date.h:210
void setUndef(const bool &flag=true)
Definition Date.cc:195
static bool initStaticData()
initialize the static map TZAbbrev
Definition Date.cc:67
friend std::istream & operator>>(std::istream &is, Date &date)
Definition Date.cc:1307
bool isUndef() const
Definition Date.h:143
thrown when encountered an unexpected function's argument (e.g. bad index, bad or missing parameter n...
Definition IOExceptions.h:130
Definition Config.cc:34
Date Duration
Definition Date.h:324