ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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 
126  int64_t getBaseTime();
127  int64_t getUTCBaseTime();
128  int64_t getUnixTime();
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);
153  void formatBaseTime(char *buffer);
154  void formatUnixTime(char *buffer);
155  void formatDays(char *buffer);
156  void formatEuropeanDate(char *buffer, const char *sep);
157  void formatMonthName(char *buffer);
158  void formatNormalDate(char *buffer, const char *sep);
159  void formatOrderedDate(char *buffer, const char *sep);
160  void formatStandardDate(char *buffer, const char *sep);
161  void formatUsaDate(char *buffer, const char *sep);
162  void formatWeekDay(char *buffer);
163  void formatCivilTime(char *buffer);
164  void formatHours(char *buffer);
165  void formatLongTime(char *buffer);
166  void formatMinutes(char *buffer);
167  void formatNormalTime(char *buffer);
168  void formatSeconds(char *buffer);
169  void formatTimeZone(char *buffer);
170  inline void setTimeZoneOffset(int64_t o) { timeZoneOffset = o; }
171  inline int64_t getTimeZoneOffset() { return timeZoneOffset; }
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
RexxDateTime::leapMonthStarts
static int leapMonthStarts[]
Definition: RexxDateTime.hpp:192
RexxDateTime::formatOrderedDate
void formatOrderedDate(char *buffer, const char *sep)
Definition: RexxDateTime.cpp:1231
RexxDateTime::minutes
int minutes
Definition: RexxDateTime.hpp:178
RexxDateTime::formatEuropeanDate
void formatEuropeanDate(char *buffer, const char *sep)
Definition: RexxDateTime.cpp:1188
RexxDateTime::formatLongTime
void formatLongTime(char *buffer)
Definition: RexxDateTime.cpp:1321
RexxDateTime::seconds
int seconds
Definition: RexxDateTime.hpp:179
RexxDateTime::valid
bool valid
Definition: RexxDateTime.hpp:173
RexxDateTime::dayNames
static const char * dayNames[]
Definition: RexxDateTime.hpp:189
RexxDateTime::isLeapYear
bool isLeapYear()
Definition: RexxDateTime.hpp:120
RexxDateTime::getNumber
bool getNumber(const char *input, wholenumber_t length, int *target)
Definition: RexxDateTime.cpp:1082
OLYMPIAD
#define OLYMPIAD
Definition: RexxDateTime.hpp:76
RexxDateTime::getTimeSeconds
wholenumber_t getTimeSeconds()
Definition: RexxDateTime.cpp:199
RexxDateTime::parseNormalDate
bool parseNormalDate(const char *date, const char *sep)
Definition: RexxDateTime.cpp:536
RexxDateTime::formatSeconds
void formatSeconds(char *buffer)
Definition: RexxDateTime.cpp:1354
RexxDateTime::day
int day
Definition: RexxDateTime.hpp:176
RexxDateTime::monthdays
static int monthdays[]
Definition: RexxDateTime.hpp:193
RexxDateTime::formatBaseDate
void formatBaseDate(char *buffer)
Definition: RexxDateTime.cpp:1139
RexxDateTime::getTimeZoneOffset
int64_t getTimeZoneOffset()
Definition: RexxDateTime.hpp:171
RexxDateTime::getWeekDay
wholenumber_t getWeekDay()
Definition: RexxDateTime.cpp:499
RexxDateTime::RexxDateTime
RexxDateTime()
Definition: RexxDateTime.cpp:101
RexxDateTime::formatStandardDate
void formatStandardDate(char *buffer, const char *sep)
Definition: RexxDateTime.cpp:1247
RexxDateTime::formatCivilTime
void formatCivilTime(char *buffer)
Definition: RexxDateTime.cpp:1290
RexxDateTime::monthNames
static const char * monthNames[]
Definition: RexxDateTime.hpp:190
RexxDateTime::maxBaseTime
static RexxDateTime maxBaseTime
Definition: RexxDateTime.hpp:195
RexxDateTime::getMonthName
const char * getMonthName()
Definition: RexxDateTime.cpp:521
RexxDateTime::setBaseTime
bool setBaseTime(int64_t basetime)
Definition: RexxDateTime.cpp:355
RexxDateTime::month
int month
Definition: RexxDateTime.hpp:175
RexxDateTime::formatHours
void formatHours(char *buffer)
Definition: RexxDateTime.cpp:1310
RexxDateTime
Definition: RexxDateTime.hpp:110
RexxDateTime::parseLongTime
bool parseLongTime(const char *date)
Definition: RexxDateTime.cpp:645
RexxDateTime::parseOrderedDate
bool parseOrderedDate(const char *date, const char *sep, wholenumber_t currentYear)
Definition: RexxDateTime.cpp:606
RexxDateTime::unixBaseTime
static RexxDateTime unixBaseTime
Definition: RexxDateTime.hpp:194
RexxDateTime::getBaseDate
wholenumber_t getBaseDate()
Definition: RexxDateTime.cpp:182
RexxDateTime::setDate
void setDate(wholenumber_t newYear, wholenumber_t newDay)
Definition: RexxDateTime.cpp:438
RexxDateTime::setUnixTime
bool setUnixTime(int64_t basetime)
Definition: RexxDateTime.cpp:391
RexxDateTime::setHours
bool setHours(wholenumber_t h)
Definition: RexxDateTime.cpp:660
RexxDateTime::parseCivilTime
bool parseCivilTime(const char *date)
Definition: RexxDateTime.cpp:632
RexxDateTime::formatMinutes
void formatMinutes(char *buffer)
Definition: RexxDateTime.cpp:1332
RexxDateTime::getUnixTime
int64_t getUnixTime()
Definition: RexxDateTime.cpp:248
RexxDateTime::formatNormalTime
void formatNormalTime(char *buffer)
Definition: RexxDateTime.cpp:1343
RexxDateTime::setTimeZoneOffset
void setTimeZoneOffset(int64_t o)
Definition: RexxDateTime.hpp:170
RexxDateTime::parseStandardDate
bool parseStandardDate(const char *date, const char *sep)
Definition: RexxDateTime.cpp:552
RexxDateTime::formatNormalDate
void formatNormalDate(char *buffer, const char *sep)
Definition: RexxDateTime.cpp:1215
RexxDateTime::formatMonthName
void formatMonthName(char *buffer)
Definition: RexxDateTime.cpp:1201
RexxDateTime::setMinutes
bool setMinutes(wholenumber_t m)
Definition: RexxDateTime.cpp:710
RexxDateTime::formatBaseTime
void formatBaseTime(char *buffer)
Definition: RexxDateTime.cpp:1151
RexxDateTime::year
int year
Definition: RexxDateTime.hpp:174
RexxDateTime::formatUsaDate
void formatUsaDate(char *buffer, const char *sep)
Definition: RexxDateTime.cpp:1263
RexxDateTime::getYearDay
wholenumber_t getYearDay()
Definition: RexxDateTime.cpp:480
RexxDateTime::formatWeekDay
void formatWeekDay(char *buffer)
Definition: RexxDateTime.cpp:1278
RexxDateTime::clear
void clear()
Definition: RexxDateTime.cpp:420
wholenumber_t
ssize_t wholenumber_t
Definition: rexx.h:229
RexxDateTime::microseconds
int microseconds
Definition: RexxDateTime.hpp:180
RexxDateTime::parseNormalTime
bool parseNormalTime(const char *date)
Definition: RexxDateTime.cpp:619
RexxDateTime::timeZoneOffset
int64_t timeZoneOffset
Definition: RexxDateTime.hpp:181
RexxDateTime::getBaseTime
int64_t getBaseTime()
Definition: RexxDateTime.cpp:213
RexxDateTime::setDay
void setDay(wholenumber_t basedays)
Definition: RexxDateTime.cpp:454
RexxDateTime::adjustTimeZone
bool adjustTimeZone(int64_t o)
Definition: RexxDateTime.cpp:734
RexxDateTime::parseEuropeanDate
bool parseEuropeanDate(const char *date, const char *sep, wholenumber_t currentYear)
Definition: RexxDateTime.cpp:570
LEAP_CYCLE
#define LEAP_CYCLE
Definition: RexxDateTime.hpp:74
RexxDateTime::formatUnixTime
void formatUnixTime(char *buffer)
Definition: RexxDateTime.cpp:1162
RexxDateTime::setSeconds
bool setSeconds(wholenumber_t s)
Definition: RexxDateTime.cpp:683
RexxDateTime::formatDays
void formatDays(char *buffer)
Definition: RexxDateTime.cpp:1173
RexxDateTime::parseUsaDate
bool parseUsaDate(const char *date, const char *sep, wholenumber_t currentYear)
Definition: RexxDateTime.cpp:588
RexxDateTime::setTimeInSeconds
void setTimeInSeconds(wholenumber_t basetime)
Definition: RexxDateTime.cpp:405
RexxDateTime::monthStarts
static int monthStarts[]
Definition: RexxDateTime.hpp:191
RexxDateTime::getDayName
const char * getDayName()
Definition: RexxDateTime.cpp:510
RexxCore.h
RexxDateTime::setBaseDate
bool setBaseDate(wholenumber_t basedays)
Definition: RexxDateTime.cpp:261
RexxDateTime::formatTimeZone
void formatTimeZone(char *buffer)
Definition: RexxDateTime.cpp:1365
RexxDateTime::hours
int hours
Definition: RexxDateTime.hpp:177
RexxDateTime::getUTCBaseTime
int64_t getUTCBaseTime()
Definition: RexxDateTime.cpp:233
CENTURY
#define CENTURY
Definition: RexxDateTime.hpp:75
RexxDateTime::parseDateTimeFormat
bool parseDateTimeFormat(const char *date, const char *format, const char *sep, wholenumber_t currentYear)
Definition: RexxDateTime.cpp:778