RexxDateTime.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel RexxDateTime.hpp */
40 /* */
41 /* Primitive Rexx Timestamp class */
42 /* */
43 /******************************************************************************/
44 
45 #ifndef Included_RexxDateTime
46 #define Included_RexxDateTime
47 
48 #include "RexxCore.h"
49 
50 #define LEAPMONTH 29 /* days in a leap year February */
51 #define MAXCIVILHOURS 12 /* maximum hours in a civil time */
52 #define MAXHOURS 23 /* maximum hours in 24 hour clock */
53 #define MAXSECONDS 59 /* maximum seconds in time */
54 #define MAXMINUTES 59 /* maximum minutes in time */
55 #define HOURS_IN_DAY 24 /* hours in a day */
56 #define MINUTES_IN_HOUR 60 /* minutes in an hour */
57 #define SECONDS_IN_MINUTE 60 /* second in a minute */
58 #define MONTHS 12 /* months in a year */
59  /* seconds in an hour */
60 #define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * MINUTES_IN_HOUR)
61  /* seconds in a complete day */
62 #define SECONDS_IN_DAY (SECONDS_IN_HOUR * HOURS_IN_DAY)
63 #define MINUTES_IN_DAY (MINUTES_IN_HOUR * HOURS_IN_DAY)
64 #define MICROSECONDS 1000000 /* microseconds in a second */
65 #define MICROSECONDS_IN_DAY ((int64_t)SECONDS_IN_DAY * (int64_t)MICROSECONDS)
66 
67  /* days in a 400 year olympiad */
68 #define BASE_DAYS(year) (((year) * 365) + ((year) / 4) - ((year) / 100) + ((year) / 400))
69  /* days in a 400 year olympiad */
70 #define OLYMPIAD_DAYS BASE_DAYS(400) /* days in a 400 year Olympiad */
71 #define CENTURY_DAYS BASE_DAYS(100) /* days in a 100 year century */
72 #define LEAP_DAYS BASE_DAYS(4) /* days in a 4 year leap cycle */
73 #define YEAR_DAYS 365 /* days in a normal year */
74 #define LEAP_CYCLE 4 /* years in a leap cycle */
75 #define CENTURY 100 /* years in a century */
76 #define OLYMPIAD 400 /* years in an Olympiad cycle */
77 
78 
79 #define MONTH_SIZE 2 /* size of a month date field */
80 #define DAY_SIZE 2 /* size of a day date field */
81 #define SHORT_YEAR 2 /* size of a 2 digit year field */
82 #define LONG_YEAR 4 /* size of a 4 digit year field */
83 #define CHAR_MONTH 3 /* size of the character month field */
84 #define HOURS_SIZE 2 /* size of an hours field */
85 #define MINUTES_SIZE 2 /* size of a minutes field */
86 #define SECONDS_SIZE 2 /* size of a seconds field */
87 #define MICRO_SIZE 6 /* size of micro seconds field */
88 
89 #define PAST_THRESHOLD 50 /* past threshold for 2 digit years */
90 #define FUTURE_THRESHOLD 49 /* future threshold for 2 digit years*/
91 #define POSTMERIDIAN "pm" /* "pm" spec of civil time */
92 #define ANTEMERIDIAN "am" /* "am" spec of civil time */
93  /* leap year calculation */
94 #define LeapYear(year) ((!(year % LEAP_CYCLE)) && ((year % CENTURY) || (!(year % OLYMPIAD))))
95 
96 #define JANUARY 1 /* positions of the months */
97 #define FEBRUARY 2
98 #define MARCH 3
99 #define APRIL 4
100 #define MAY 5
101 #define JUNE 6
102 #define JULY 7
103 #define AUGUST 8
104 #define SEPTEMBER 9
105 #define OCTOBER 10
106 #define NOVEMBER 11
107 #define DECEMBER 12
108 
109 
110 class RexxDateTime // ooRexx internal time stamp class
111 {
112 public:
113 
114  RexxDateTime();
115  RexxDateTime(int64_t basetime);
116  RexxDateTime(wholenumber_t basedate, bool dummy);
119 
120  inline bool isLeapYear() {
121  return ((!(year % LEAP_CYCLE)) && ((year % CENTURY) || (!(year % OLYMPIAD))));
122  }
123 
131  const char *getMonthName();
132  const char *getDayName();
133  bool setBaseDate(wholenumber_t basedays);
134  bool setBaseTime(int64_t basetime);
135  bool setUnixTime(int64_t basetime);
136  void setTimeInSeconds(wholenumber_t basetime);
137  void clear();
138  void setDate(wholenumber_t newYear, wholenumber_t newDay);
139  void setDay(wholenumber_t basedays);
140  bool parseNormalDate(const char *date, const char *sep);
141  bool parseStandardDate(const char *date, const char *sep);
142  bool parseEuropeanDate(const char *date, const char *sep, wholenumber_t currentYear);
143  bool parseUsaDate(const char *date, const char *sep, wholenumber_t currentYear);
144  bool parseOrderedDate(const char *date, const char *sep, wholenumber_t currentYear);
145  bool parseNormalTime(const char *date);
146  bool parseCivilTime(const char *date);
147  bool parseLongTime(const char *date);
148  bool setHours(wholenumber_t h);
149  bool setSeconds(wholenumber_t s);
150  bool setMinutes(wholenumber_t m);
151  bool adjustTimeZone(int64_t o);
152  void formatBaseDate(char *buffer, size_t size);
153  void formatBaseTime(char *buffer);
154  void formatUnixTime(char *buffer);
155  void formatDays(char *buffer, size_t size);
156  void formatEuropeanDate(char *buffer, size_t size, const char *sep);
157  void formatMonthName(char *buffer);
158  void formatNormalDate(char *buffer, size_t size, const char *sep);
159  void formatOrderedDate(char *buffer, size_t size, const char *sep);
160  void formatStandardDate(char *buffer, size_t size, const char *sep);
161  void formatUsaDate(char *buffer, size_t size, const char *sep);
162  void formatWeekDay(char *buffer);
163  void formatCivilTime(char *buffer, size_t size);
164  void formatHours(char *buffer, size_t size);
165  void formatLongTime(char *buffer, size_t size);
166  void formatMinutes(char *buffer, size_t size);
167  void formatNormalTime(char *buffer, size_t size);
168  void formatSeconds(char *buffer, size_t size);
169  void formatTimeZone(char *buffer);
170  inline void setTimeZoneOffset(int64_t o) { timeZoneOffset = o; }
172 
173  bool valid;
174  int year; // current year
175  int month; // month of the year
176  int day; // day of the month
177  int hours; // hour of the day (24-hour)
178  int minutes; // minute of the hour
179  int seconds; // second of the minute
180  int microseconds; // microseconds
181  int64_t timeZoneOffset; // offset from UTC for this time stamp
182 
183 protected:
184 
185  bool parseDateTimeFormat(const char *date, const char *format, const char *sep, wholenumber_t currentYear);
186  bool getNumber(const char *input, wholenumber_t length, int *target);
187  bool getNumber(const char *input, wholenumber_t length, int *target, int max);
188 
189  static const char *dayNames[]; // table of day names for date formatting
190  static const char *monthNames[]; // table of month names for date formatting
191  static int monthStarts[]; // table of first day of month values for non-leap years
192  static int leapMonthStarts[]; // table of first day of month values for leap years
193  static int monthdays[]; // month number of days mapping table
194  static RexxDateTime unixBaseTime; // a base time used for Date('T')/Time('T') calculations.
195  static RexxDateTime maxBaseTime; // the largest possible date we can handle.
196 
197 };
198 
199 #endif
#define CENTURY
#define LEAP_CYCLE
#define OLYMPIAD
void formatNormalDate(char *buffer, size_t size, const char *sep)
static const char * monthNames[]
void formatMinutes(char *buffer, size_t size)
wholenumber_t getTimeSeconds()
void formatHours(char *buffer, size_t size)
wholenumber_t getWeekDay()
static const char * dayNames[]
void formatNormalTime(char *buffer, size_t size)
static int monthStarts[]
void formatBaseTime(char *buffer)
int64_t getUnixTime()
void formatBaseDate(char *buffer, size_t size)
void formatWeekDay(char *buffer)
void setDate(wholenumber_t newYear, wholenumber_t newDay)
void formatMonthName(char *buffer)
wholenumber_t getYearDay()
bool setMinutes(wholenumber_t m)
void formatCivilTime(char *buffer, size_t size)
bool setBaseDate(wholenumber_t basedays)
void setTimeInSeconds(wholenumber_t basetime)
int64_t timeZoneOffset
bool parseUsaDate(const char *date, const char *sep, wholenumber_t currentYear)
int64_t getBaseTime()
bool parseOrderedDate(const char *date, const char *sep, wholenumber_t currentYear)
bool getNumber(const char *input, wholenumber_t length, int *target)
void formatLongTime(char *buffer, size_t size)
bool parseNormalTime(const char *date)
void formatTimeZone(char *buffer)
void formatUsaDate(char *buffer, size_t size, const char *sep)
void formatSeconds(char *buffer, size_t size)
bool parseLongTime(const char *date)
static RexxDateTime maxBaseTime
bool parseEuropeanDate(const char *date, const char *sep, wholenumber_t currentYear)
bool parseCivilTime(const char *date)
const char * getDayName()
int64_t getUTCBaseTime()
void formatDays(char *buffer, size_t size)
const char * getMonthName()
void formatStandardDate(char *buffer, size_t size, const char *sep)
static int leapMonthStarts[]
bool parseDateTimeFormat(const char *date, const char *format, const char *sep, wholenumber_t currentYear)
bool setBaseTime(int64_t basetime)
bool setHours(wholenumber_t h)
bool parseStandardDate(const char *date, const char *sep)
bool parseNormalDate(const char *date, const char *sep)
bool adjustTimeZone(int64_t o)
bool setUnixTime(int64_t basetime)
void formatEuropeanDate(char *buffer, size_t size, const char *sep)
void setDay(wholenumber_t basedays)
static RexxDateTime unixBaseTime
wholenumber_t getBaseDate()
void formatUnixTime(char *buffer)
void formatOrderedDate(char *buffer, size_t size, const char *sep)
void setTimeZoneOffset(int64_t o)
static int monthdays[]
int64_t getTimeZoneOffset()
bool setSeconds(wholenumber_t s)
ssize_t wholenumber_t
Definition: rexx.h:230
signed __int64 int64_t