]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/Datetime.h
added install instructions
[gen-shell.git] / src / libshared / src / Datetime.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, 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
43   Datetime ();
44   Datetime (const std::string&, const std::string& format = "");
45   Datetime (time_t);
46   Datetime (const int, const int, const int);
47   Datetime (const int, const int, const int, const int, const int, const int);
48   bool parse (const std::string&, std::string::size_type&, const std::string& format = "");
49   time_t toEpoch () const;
50   std::string toEpochString () const;
51   std::string toISO () const;
52   std::string toISOLocalExtended () const;
53   double toJulian () const;
54   void toYMD (int&, int&, int&) const;
55   const std::string toString (const std::string& format = "Y-M-D") const;
56
57   Datetime startOfDay () const;
58   Datetime startOfWeek () const;
59   Datetime startOfMonth () const;
60   Datetime startOfYear () const;
61
62   static bool valid (const std::string&, const std::string& format = "");
63   static bool valid (const int, const int, const int, const int, const int, const int);
64   static bool valid (const int, const int, const int);
65   static bool valid (const int, const int);
66   static bool leapYear (int);
67   static int daysInMonth (int, int);
68   static int daysInYear (int);
69   static std::string monthName (int);
70   static std::string monthNameShort (int);
71   static std::string dayName (int);
72   static std::string dayNameShort (int);
73   static int dayOfWeek (const std::string&);
74   static int dayOfWeek (int, int, int);
75   static int monthOfYear (const std::string&);
76   static int length (const std::string&);
77
78   int month () const;
79   int week () const;
80   int day () const;
81   int year () const;
82   int dayOfWeek () const;
83   int dayOfYear () const;
84   int hour () const;
85   int minute () const;
86   int second () const;
87
88   bool operator==  (const Datetime&) const;
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 sameHour    (const Datetime&) const;
95   bool sameDay     (const Datetime&) const;
96   bool sameWeek    (const Datetime&) const;
97   bool sameMonth   (const Datetime&) const;
98   bool sameQuarter (const Datetime&) const;
99   bool sameYear    (const Datetime&) const;
100   Datetime operator+  (const int);
101   Datetime operator-  (const int);
102   Datetime& operator+= (const int);
103   Datetime& operator-= (const int);
104   time_t operator- (const Datetime&);
105   void operator--  ();    // Prefix
106   void operator--  (int); // Postfix
107   void operator++  ();    // Prefix
108   void operator++  (int); // Postfix
109
110 private:
111   void clear ();
112   bool parse_formatted     (Pig&, const std::string&);
113   bool parse_named         (Pig&);
114   bool parse_epoch         (Pig&);
115   bool parse_date_time_ext (Pig&);
116   bool parse_date_ext      (Pig&);
117   bool parse_off_ext       (Pig&);
118   bool parse_time_ext      (Pig&, bool terminated = true);
119   bool parse_time_utc_ext  (Pig&);
120   bool parse_time_off_ext  (Pig&);
121   bool parse_date_time     (Pig&);
122   bool parse_date          (Pig&);
123   bool parse_time_utc      (Pig&);
124   bool parse_time_off      (Pig&);
125   bool parse_time          (Pig&, bool terminated = true);
126   bool parse_off           (Pig&);
127
128   bool parse_year          (Pig&, int&);
129   bool parse_month         (Pig&, int&);
130   bool parse_week          (Pig&, int&);
131   bool parse_julian        (Pig&, int&);
132   bool parse_day           (Pig&, int&);
133   bool parse_weekday       (Pig&, int&);
134   bool parse_hour          (Pig&, int&);
135   bool parse_minute        (Pig&, int&);
136   bool parse_second        (Pig&, int&);
137   bool parse_off_hour      (Pig&, int&);
138   bool parse_off_minute    (Pig&, int&);
139
140   bool initializeNow            (Pig&);
141   bool initializeYesterday      (Pig&);
142   bool initializeToday          (Pig&);
143   bool initializeTomorrow       (Pig&);
144   bool initializeOrdinal        (Pig&);
145   bool initializeDayName        (Pig&);
146   bool initializeMonthName      (Pig&);
147   bool initializeLater          (Pig&);
148   bool initializeSopd           (Pig&);
149   bool initializeSod            (Pig&);
150   bool initializeSond           (Pig&);
151   bool initializeEopd           (Pig&);
152   bool initializeEod            (Pig&);
153   bool initializeEond           (Pig&);
154   bool initializeSopw           (Pig&);
155   bool initializeSow            (Pig&);
156   bool initializeSonw           (Pig&);
157   bool initializeEopw           (Pig&);
158   bool initializeEow            (Pig&);
159   bool initializeEonw           (Pig&);
160   bool initializeSopww          (Pig&);
161   bool initializeSonww          (Pig&);
162   bool initializeSoww           (Pig&);
163   bool initializeEopww          (Pig&);
164   bool initializeEonww          (Pig&);
165   bool initializeEoww           (Pig&);
166   bool initializeSopm           (Pig&);
167   bool initializeSom            (Pig&);
168   bool initializeSonm           (Pig&);
169   bool initializeEopm           (Pig&);
170   bool initializeEom            (Pig&);
171   bool initializeEonm           (Pig&);
172   bool initializeSopq           (Pig&);
173   bool initializeSoq            (Pig&);
174   bool initializeSonq           (Pig&);
175   bool initializeEopq           (Pig&);
176   bool initializeEoq            (Pig&);
177   bool initializeEonq           (Pig&);
178   bool initializeSopy           (Pig&);
179   bool initializeSoy            (Pig&);
180   bool initializeSony           (Pig&);
181   bool initializeEopy           (Pig&);
182   bool initializeEoy            (Pig&);
183   bool initializeEony           (Pig&);
184   bool initializeEaster         (Pig&);
185   bool initializeMidsommar      (Pig&);
186   bool initializeMidsommarafton (Pig&);
187   bool initializeInformalTime   (Pig&);
188   void easter (struct tm*) const;
189   void midsommar (struct tm*) const;
190   void midsommarafton (struct tm*) const;
191
192   bool initializeNthDayInMonth  (const std::vector <std::string>&);
193
194   bool isOrdinal (const std::string&, int&);
195
196   bool validate ();
197   void resolve ();
198   std::string dump () const;
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 ////////////////////////////////////////////////////////////////////////////////