1 //////////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright 2006 - 2021, Paul Beckingham, Federico Hernandez. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included 13 // in all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 // 23 // http://www.opensource.org/licenses/mit-license.php 24 // 25 //////////////////////////////////////////////////////////////////////////////// 26 27 #ifndef INCLUDED_DATETIME 28 #define INCLUDED_DATETIME 29 30 #include <string> 31 #include <ctime> 32 #include <Pig.h> 33 34 class Datetime 35 { 36 public: 37 static int weekstart; 38 static int minimumMatchLength; 39 static bool isoEnabled; 40 static bool standaloneDateEnabled; 41 static bool standaloneTimeEnabled; 42 static bool timeRelative; 43 44 Datetime (); 45 Datetime (const std::string&, const std::string& format = ""); 46 Datetime (time_t); 47 Datetime (const int, const int, const int); 48 Datetime (const int, const int, const int, const int, const int, const int); 49 bool parse (const std::string&, std::string::size_type&, const std::string& format = ""); 50 time_t toEpoch () const; 51 std::string toEpochString () const; 52 std::string toISO () const; 53 std::string toISOLocalExtended () const; 54 double toJulian () const; 55 void toYMD (int&, int&, int&) const; 56 const std::string toString (const std::string& format = "Y-M-D") const; 57 58 Datetime startOfDay () const; 59 Datetime startOfWeek () const; 60 Datetime startOfMonth () const; 61 Datetime startOfYear () const; 62 63 static bool valid (const std::string&, const std::string& format = ""); 64 static bool valid (const int, const int, const int, const int, const int, const int); 65 static bool valid (const int, const int, const int); 66 static bool valid (const int, const int); 67 static bool leapYear (int); 68 static int daysInMonth (int, int); 69 static int daysInYear (int); 70 static std::string monthName (int); 71 static std::string monthNameShort (int); 72 static std::string dayName (int); 73 static std::string dayNameShort (int); 74 static int dayOfWeek (const std::string&); 75 static int dayOfWeek (int, int, int); 76 static int monthOfYear (const std::string&); 77 static int length (const std::string&); 78 79 int month () const; 80 int week () const; 81 int day () const; 82 int year () const; 83 int dayOfWeek () const; 84 int dayOfYear () const; 85 int hour () const; 86 int minute () const; 87 int second () const; 88 89 bool operator== (const Datetime&) const; 90 bool operator!= (const Datetime&) const; 91 bool operator< (const Datetime&) const; 92 bool operator> (const Datetime&) const; 93 bool operator<= (const Datetime&) const; 94 bool operator>= (const Datetime&) const; 95 bool sameHour (const Datetime&) const; 96 bool sameDay (const Datetime&) const; 97 bool sameWeek (const Datetime&) const; 98 bool sameMonth (const Datetime&) const; 99 bool sameQuarter (const Datetime&) const; 100 bool sameYear (const Datetime&) const; 101 Datetime operator+ (const int); 102 Datetime operator- (const int); 103 Datetime& operator+= (const int); 104 Datetime& operator-= (const int); 105 time_t operator- (const Datetime&); 106 void operator-- (); // Prefix 107 void operator-- (int); // Postfix 108 void operator++ (); // Prefix 109 void operator++ (int); // Postfix 110 111 private: 112 void clear (); 113 bool parse_formatted (Pig&, const std::string&); 114 bool parse_named (Pig&); 115 bool parse_epoch (Pig&); 116 bool parse_date_time_ext (Pig&); 117 bool parse_date_ext (Pig&); 118 bool parse_off_ext (Pig&); 119 bool parse_time_ext (Pig&, bool terminated = true); 120 bool parse_time_utc_ext (Pig&); 121 bool parse_time_off_ext (Pig&); 122 bool parse_date_time (Pig&); 123 bool parse_date (Pig&); 124 bool parse_time_utc (Pig&); 125 bool parse_time_off (Pig&); 126 bool parse_time (Pig&, bool terminated = true); 127 bool parse_off (Pig&); 128 129 bool parse_year (Pig&, int&); 130 bool parse_month (Pig&, int&); 131 bool parse_week (Pig&, int&); 132 bool parse_julian (Pig&, int&); 133 bool parse_day (Pig&, int&); 134 bool parse_weekday (Pig&, int&); 135 bool parse_hour (Pig&, int&); 136 bool parse_minute (Pig&, int&); 137 bool parse_second (Pig&, int&); 138 bool parse_off_hour (Pig&, int&); 139 bool parse_off_minute (Pig&, int&); 140 141 bool initializeNow (Pig&); 142 bool initializeYesterday (Pig&); 143 bool initializeToday (Pig&); 144 bool initializeTomorrow (Pig&); 145 bool initializeOrdinal (Pig&); 146 bool initializeDayName (Pig&); 147 bool initializeMonthName (Pig&); 148 bool initializeLater (Pig&); 149 bool initializeSopd (Pig&); 150 bool initializeSod (Pig&); 151 bool initializeSond (Pig&); 152 bool initializeEopd (Pig&); 153 bool initializeEod (Pig&); 154 bool initializeEond (Pig&); 155 bool initializeSopw (Pig&); 156 bool initializeSow (Pig&); 157 bool initializeSonw (Pig&); 158 bool initializeEopw (Pig&); 159 bool initializeEow (Pig&); 160 bool initializeEonw (Pig&); 161 bool initializeSopww (Pig&); 162 bool initializeSonww (Pig&); 163 bool initializeSoww (Pig&); 164 bool initializeEopww (Pig&); 165 bool initializeEonww (Pig&); 166 bool initializeEoww (Pig&); 167 bool initializeSopm (Pig&); 168 bool initializeSom (Pig&); 169 bool initializeSonm (Pig&); 170 bool initializeEopm (Pig&); 171 bool initializeEom (Pig&); 172 bool initializeEonm (Pig&); 173 bool initializeSopq (Pig&); 174 bool initializeSoq (Pig&); 175 bool initializeSonq (Pig&); 176 bool initializeEopq (Pig&); 177 bool initializeEoq (Pig&); 178 bool initializeEonq (Pig&); 179 bool initializeSopy (Pig&); 180 bool initializeSoy (Pig&); 181 bool initializeSony (Pig&); 182 bool initializeEopy (Pig&); 183 bool initializeEoy (Pig&); 184 bool initializeEony (Pig&); 185 bool initializeEaster (Pig&); 186 bool initializeMidsommar (Pig&); 187 bool initializeMidsommarafton (Pig&); 188 bool initializeInformalTime (Pig&); 189 void easter (struct tm*) const; 190 void midsommar (struct tm*) const; 191 void midsommarafton (struct tm*) const; 192 193 bool initializeNthDayInMonth (const std::vector <std::string>&); 194 195 bool isOrdinal (const std::string&, int&); 196 197 bool validate (); 198 void resolve (); 199 200 public: 201 int _year {0}; 202 int _month {0}; 203 int _week {0}; 204 int _weekday {0}; 205 int _julian {0}; 206 int _day {0}; 207 int _seconds {0}; 208 int _offset {0}; 209 bool _utc {false}; 210 time_t _date {0}; 211 }; 212 213 #endif 214 215 //////////////////////////////////////////////////////////////////////////////// 216