UCommon
datetime.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ 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 GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef _UCOMMON_DATETIME_H_
31 #define _UCOMMON_DATETIME_H_
32 
33 #ifndef _UCOMMON_CONFIG_H_
34 #include <ucommon/platform.h>
35 #endif
36 
37 #ifndef _UCOMMON_NUMBERS_H_
38 #include <ucommon/numbers.h>
39 #endif
40 
41 #ifndef _UCOMMON_TYPEREF_H_
42 #include <ucommon/typeref.h>
43 #endif
44 
45 #ifndef _MSWINDOWS_
46 #include <unistd.h>
47 #include <sys/time.h>
48 #endif
49 
50 #include <time.h>
51 
52 #define DATE_STRING_SIZE 10
53 #define DATE_BUFFER_SIZE 11
54 #define TIME_STRING_SIZE 8
55 #define TIME_BUFFER_SIZE 9
56 #define DATETIME_STRING_SIZE 19
57 #define DATETIME_BUFFER_SIZE 20
58 
62 typedef struct tm tm_t;
63 
64 namespace ucommon {
65 
66 #ifdef __BORLANDC__
67  using std::tm;
68  using std::time_t;
69 #endif
70 
79 class __EXPORT Date
80 {
81 protected:
82  long julian;
83 
84  void set(long year, long month, long day);
85 
90  virtual void update(void);
91 
92 public:
96  static const size_t sz_string;
97 
102  Date(time_t value);
103 
108  Date(const struct tm *object);
109 
115  Date(const char *pointer, size_t size = 0);
116 
123  Date(int year, unsigned month, unsigned day);
124 
129  Date(const Date& object);
130 
134  Date();
135 
139  virtual ~Date();
140 
145  int year(void) const;
146 
151  unsigned month(void) const;
152 
157  unsigned day(void) const;
158 
163  unsigned dow(void) const;
164 
170  const char *put(char *buffer) const;
171 
176  time_t timeref(void) const;
177 
182  long get(void) const;
183 
187  void set(void);
188 
194  void set(const char *pointer, size_t size = 0);
195 
200  bool is_valid(void) const;
201 
206  inline operator long() const {
207  return get();
208  }
209 
214  inline long operator*() const {
215  return get();
216  }
217 
223  stringref_t operator()() const;
224 
230 
236 
242  Date& operator+=(long offset);
243 
249  Date& operator-=(long offset);
250 
256  const Date operator+(long days) const;
257 
263  const Date operator-(long days) const;
264 
270  inline long operator-(const Date &date) {
271  return (julian - date.julian);
272  }
273 
279  Date& operator=(const Date& date);
280 
286  bool operator==(const Date& date) const;
287 
293  bool operator!=(const Date& date) const;
294 
300  bool operator<(const Date& date) const;
301 
307  bool operator<=(const Date& date) const;
308 
314  bool operator>(const Date& date) const;
315 
321  bool operator>=(const Date& date) const;
322 
327  inline bool operator!() const {
328  return !is_valid();
329  }
330 
335  inline operator bool() const {
336  return is_valid();
337  }
338 };
339 
351 class __EXPORT Time
352 {
353 protected:
354  long seconds;
355 
356 protected:
357  virtual void update(void);
358 
359 public:
360  void set(int hour, int minute = 0, int second = 0);
361 
365  static const long c_day;
366 
370  static const long c_hour;
371 
375  static const long c_week;
376 
380  static const size_t sz_string;
381 
386  Time(const time_t value);
387 
392  Time(const tm_t *object);
393 
399  Time(const char *pointer, size_t size = 0);
400 
407  Time(int hour, int minute, int second);
408 
413  Time(const Time& object);
414 
418  Time();
419 
423  virtual ~Time();
424 
429  long get(void) const;
430 
435  int hour(void) const;
436 
441  int minute(void) const;
442 
447  int second(void) const;
448 
454  const char *put(char *buffer) const;
455 
459  void set(void);
460 
466  void set(const char *pointer, size_t size = 0);
467 
472  bool is_valid(void) const;
473 
478  inline operator bool() const {
479  return is_valid();
480  }
481 
486  inline bool operator!() const {
487  return !is_valid();
488  }
489 
495  long operator-(const Time &reference);
496 
502  const Time operator+(long seconds) const;
503 
509  const Time operator-(long seconds) const;
510 
515  inline operator long() const {
516  return get();
517  }
518 
523  inline long operator*() const {
524  return get();
525  }
526 
531  stringref_t operator()() const;
532 
538 
544 
550  Time& operator=(const Time& time);
551 
557  Time& operator+=(long seconds);
558 
564  Time& operator-=(long seconds);
565 
571  bool operator==(const Time &time) const;
572 
578  bool operator!=(const Time &time) const;
579 
585  bool operator<(const Time &time) const;
586 
592  bool operator<=(const Time &time) const;
593 
599  bool operator>(const Time &time) const;
600 
606  bool operator>=(const Time &time) const;
607 };
608 
618 class __EXPORT DateTime : public Date, public Time
619 {
620 protected:
621  virtual void update(void) __OVERRIDE;
622 
623 public:
627  static const size_t sz_string;
628 
633  DateTime(const time_t time);
634 
639  DateTime(const tm_t *tm);
640 
646  DateTime(const char *pointer, size_t size = 0);
647 
657  DateTime(int year, unsigned month, unsigned day,
658  int hour = 0, int minute = 0, int second = 0);
659 
664  DateTime(const DateTime& object);
665 
670 
674  virtual ~DateTime();
675 
681  const char *put(char *buffer) const;
682 
687  time_t get(void) const;
688 
693  bool is_valid(void) const;
694 
700  long operator-(const DateTime &datetime);
701 
707  DateTime& operator=(const DateTime& datetime);
708 
715  DateTime& operator+=(long seconds);
716 
723  DateTime& operator-=(long seconds);
724 
731  const DateTime operator+(long seconds) const;
732 
739  const DateTime operator-(long seconds) const;
740 
746 
752 
758  bool operator==(const DateTime& datetime) const;
759 
765  bool operator!=(const DateTime& datetime) const;
766 
772  bool operator<(const DateTime& datetime) const;
773 
780  bool operator<=(const DateTime& datetime) const;
781 
787  bool operator>(const DateTime& datetime) const;
788 
795  bool operator>=(const DateTime& datetime) const;
796 
801  bool operator!() const;
802 
807  operator bool() const;
808 
813  inline operator long() const {
814  return Date::get();
815  }
816 
820  void set(void);
821 
826  operator double() const;
827 
833  stringref_t format(const char *strftime) const;
834 
843  static tm_t *local(const time_t *time = NULL);
844 
853  static tm_t *gmt(const time_t *time = NULL);
854 
859  static void release(tm_t *object);
860 };
861 
869 class __EXPORT DateTimeString : public DateTime
870 {
871 public:
876  typedef enum {
877  DATE, TIME, BOTH
878  } mode_t;
879 
880 private:
881  char buffer[DATETIME_BUFFER_SIZE];
882  mode_t mode;
883 
884 protected:
885  virtual void update(void) __OVERRIDE;
886 
887 public:
892  DateTimeString(const time_t time);
893 
898  DateTimeString(const tm_t *tm);
899 
905  DateTimeString(const char *pointer, size_t size = 0);
906 
916  DateTimeString(int year, unsigned month, unsigned day,
917  int hour = 0, int minute = 0, int second = 0);
918 
924 
928  DateTimeString(mode_t string = DateTimeString::BOTH);
929 
933  virtual ~DateTimeString();
934 
940  inline const char *c_str(void) const {
941  return buffer;
942  }
943 
949  inline operator const char *(void) const {
950  return buffer;
951  }
952 
956  void set(void);
957 
962  void set(mode_t string);
963 };
964 
971 class __EXPORT DateNumber : public Number, public Date
972 {
973 protected:
974  virtual void update(void) __OVERRIDE;
975 
976 public:
982 
986  virtual ~DateNumber();
987 
991  void set(void);
992 };
993 
994 class __EXPORT isotime : public __PROTOCOL PrintProtocol, public __PROTOCOL InputProtocol
995 {
996 private:
997  Date *d;
998  Time *t;
999 
1000  enum {
1001  DATE, TIME, DATETIME
1002  } mode;
1003 
1004  char buf[32];
1005  unsigned pos;
1006 
1007 protected:
1008  const char *_print(void) const;
1009 
1010  int _input(int code) __OVERRIDE;
1011 
1012 public:
1013  isotime(Date& date, Time& time);
1014  isotime(Date& date);
1015  isotime(Time& time);
1016 };
1017 
1022 
1027 
1031 typedef Date date_t;
1032 
1036 typedef Time tod_t;
1037 
1038 } // namespace ucommon
1039 
1040 #endif
ucommon::Time::operator*
long operator*() const
Get object time in seconds.
Definition: datetime.h:523
ucommon::Date::operator+=
Date & operator+=(long offset)
Increment date by offset.
ucommon::Date::operator!
bool operator!() const
Check if julian date is not valid.
Definition: datetime.h:327
ucommon::Date::Date
Date(int year, unsigned month, unsigned day)
Create a julian date from an arbitrary year, month, and day.
ucommon::DateTime::DateTime
DateTime(const DateTime &object)
Create a datetime object from another object.
ucommon::Time::operator-
const Time operator-(long seconds) const
Subtract seconds to the current time, wrap if 24 hours.
ucommon::pointer
Generic smart pointer class.
Definition: generics.h:55
ucommon::Date::operator==
bool operator==(const Date &date) const
Compare julian dates if same date.
ucommon::Time::operator!=
bool operator!=(const Time &time) const
Compare time with another time to see if not same time.
ucommon::DateTime::get
time_t get(void) const
Get C library time_t type if object in C library epoch range.
ucommon::Date::operator<
bool operator<(const Date &date) const
Compare julian date if earlier than another date.
ucommon::DateTime::operator>=
bool operator>=(const DateTime &datetime) const
Compare date and time with another date and time to see if later or the same.
ucommon::Date::operator--
Date & operator--()
Decrement date by one day.
ucommon::Date::operator!=
bool operator!=(const Date &date) const
Compare julian dates if not same date.
ucommon::DateNumber::update
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
ucommon::DateTime::operator<
bool operator<(const DateTime &datetime) const
Compare date and time with another date and time to see if earlier.
ucommon::DateTimeString::set
void set(void)
Set (update) the date and time with current date and time.
ucommon::DateTime::operator-
const DateTime operator-(long seconds) const
Subtract seconds from datetime in an expression.
ucommon::Date::operator-
const Date operator-(long days) const
Subtract days from a julian date in an expression.
ucommon::Date::operator-=
Date & operator-=(long offset)
Decrement date by offset.
ucommon::Time::Time
Time(const char *pointer, size_t size=0)
Create a time from a hh:mm:ss formatted time string.
ucommon::Time::put
const char * put(char *buffer) const
Get a hh:mm:ss formatted string for current time.
ucommon::Time::set
void set(void)
Set (update) the time with current time.
ucommon::Time::Time
Time(int hour, int minute, int second)
Create a time from hours (0-23), minutes (0-59), and seconds (0-59).
ucommon::DateTimeString::mode_t
mode_t
Specify string buffer mode.
Definition: datetime.h:876
ucommon::Time::set
void set(const char *pointer, size_t size=0)
Set time from a hh:mm:ss formatted string.
ucommon::datetimestring_t
DateTimeString datetimestring_t
Convenience type for using DateTimeString object.
Definition: datetime.h:1026
ucommon::Time::c_hour
static const long c_hour
Constant for number of seconds in a hour.
Definition: datetime.h:370
numbers.h
Support classes for manipulation of numbers as strings.
ucommon::DateTime::~DateTime
virtual ~DateTime()
Destroy date and time object.
ucommon::Time::get
long get(void) const
Get current time in seconds from midnight.
ucommon::Date::dow
unsigned dow(void) const
Get the day of the week (0-7).
ucommon::Time::operator!
bool operator!() const
Check if time object has valid value for ! operator.
Definition: datetime.h:486
ucommon::DateTime::release
static void release(tm_t *object)
Release a struct tm object from glt or gmt.
ucommon::Date::update
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
ucommon::Time::operator++
Time & operator++()
Incrememnt time by 1 second, wrap on 24 hour period.
ucommon::Date::put
const char * put(char *buffer) const
Get a ISO string representation of the date (yyyy-mm-dd).
ucommon::DateTime::operator--
DateTime & operator--()
Subtract a day from the current date and time.
ucommon::DateTime::gmt
static tm_t * gmt(const time_t *time=NULL)
Fetch an instance of time converted to gmt.
ucommon::Date::operator+
const Date operator+(long days) const
Add days to julian date in an expression.
ucommon::DateTime::is_valid
bool is_valid(void) const
Test if object is valid.
ucommon::InputProtocol
Used for processing input.
Definition: protocols.h:154
ucommon::Date::get
long get(void) const
Get the date as a number for the object or 0 if invalid.
ucommon::Date::year
int year(void) const
Get the year of the date.
ucommon::Date::Date
Date()
Construct a new julian date with today's date.
ucommon::tod_t
Time tod_t
Convenience type for using Time object.
Definition: datetime.h:1036
ucommon::DateTime::DateTime
DateTime(int year, unsigned month, unsigned day, int hour=0, int minute=0, int second=0)
Construct a date and time object from explicit date and time values.
ucommon::Date::is_valid
bool is_valid(void) const
Check if date is valid.
ucommon::DateTime::operator!
bool operator!() const
Check if date and time is not valid.
ucommon::Time::operator+=
Time & operator+=(long seconds)
Increment time by specified seconds.
ucommon::Date::operator<=
bool operator<=(const Date &date) const
Compare julian date if earlier than or equal to another date.
ucommon::Time::operator<=
bool operator<=(const Time &time) const
Compare time if earlier than or equal to another time.
ucommon::DateNumber::~DateNumber
virtual ~DateNumber()
Release a datenumber object.
ucommon::Date::Date
Date(const Date &object)
Create a julian date object from another object.
ucommon::DateTime::DateTime
DateTime(const tm_t *tm)
Construct a date and time from C library time structure.
ucommon::DateTime::DateTime
DateTime(const time_t time)
Construct a date and time from C library time_t type.
ucommon::PrintProtocol
Used for forming stream output.
Definition: protocols.h:135
ucommon::Date::operator*
long operator*() const
Access julian value.
Definition: datetime.h:214
ucommon::Time::operator>=
bool operator>=(const Time &time) const
Compare time if later than or equal to another time.
ucommon::Time::second
int second(void) const
Get seconds from current minute.
ucommon::DateTimeString::DateTimeString
DateTimeString(const char *pointer, size_t size=0)
Construct a date and time from ISO string buffer.
ucommon::DateTime::operator-
long operator-(const DateTime &datetime)
Operator to compute number of days between two dates.
ucommon::DateTime::operator++
DateTime & operator++()
Add a day from the current date and time.
ucommon::DateNumber
A number class that manipulates a string buffer that is also a date.
Definition: datetime.h:972
ucommon::DateNumber::set
void set(void)
Set date number to current date.
ucommon::DateTime::operator==
bool operator==(const DateTime &datetime) const
Compare date and time with another date and time to see if the same.
ucommon::Date::operator=
Date & operator=(const Date &date)
Assign date from another date object.
ucommon::DateTimeString::DateTimeString
DateTimeString(mode_t string=DateTimeString::BOTH)
Construct a new date and time object with current date and time.
ucommon::Date::operator>
bool operator>(const Date &date) const
Compare julian date if later than another date.
ucommon::Time::operator+
const Time operator+(long seconds) const
Add seconds to the current time, wrap if 24 hours.
ucommon::Date::operator++
Date & operator++()
Increment date by one day.
ucommon::Date::set
void set(void)
Set (update) the date with current date.
ucommon::Date::month
unsigned month(void) const
Get the month of the date (1-12).
ucommon::Time::operator-
long operator-(const Time &reference)
Get difference (in seconds) between two times.
ucommon::Time::Time
Time(const time_t value)
Create a time from the time portion of a time_t.
platform.h
Various miscellaneous platform specific headers and defines.
ucommon
Common namespace for all ucommon objects.
Definition: access.h:47
ucommon::DateTimeString::set
void set(mode_t string)
Set the string mode.
ucommon::Date::operator>=
bool operator>=(const Date &date) const
Compare julian date if later than or equal to another date.
ucommon::date_t
Date date_t
Convenience type for using Date object.
Definition: datetime.h:1031
ucommon::DateTime::operator=
DateTime & operator=(const DateTime &datetime)
Assign date and time from another datetime object.
ucommon::Date::sz_string
static const size_t sz_string
Size of date string field.
Definition: datetime.h:96
ucommon::Date::timeref
time_t timeref(void) const
Get a time_t for the julian date if in time_t epoch.
ucommon::DateTime::set
void set(void)
Set (update) the date and time with current date and time.
ucommon::DateNumber::DateNumber
DateNumber(char *pointer)
Create a date number tied to a refreshed string buffer.
ucommon::Time::sz_string
static const size_t sz_string
Size of time string field.
Definition: datetime.h:380
ucommon::DateTime::sz_string
static const size_t sz_string
Size of datetime string field.
Definition: datetime.h:627
ucommon::DateTime::DateTime
DateTime()
Construct a new date and time object with current date and time.
ucommon::Time::operator<
bool operator<(const Time &time) const
Compare time if earlier than another time.
ucommon::DateTimeString::DateTimeString
DateTimeString(const tm_t *tm)
Construct a date and time from C library time structure.
ucommon::DateTime::operator>
bool operator>(const DateTime &datetime) const
Compare date and time with another date and time to see if later.
ucommon::Date::operator()
stringref_t operator()() const
Expression operator to return an ISO date string for the current julian date.
ucommon::DateTimeString::update
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
ucommon::Time::hour
int hour(void) const
Get hours from midnight.
ucommon::Time::~Time
virtual ~Time()
Destroy time object.
ucommon::DateTimeString::DateTimeString
DateTimeString(const DateTimeString &object)
Create a datetime object from another object.
ucommon::DateTime::operator<=
bool operator<=(const DateTime &datetime) const
Compare date and time with another date and time to see if earlier or the same.
ucommon::datetime_t
DateTime datetime_t
Convenience type for using DateTime object.
Definition: datetime.h:1021
ucommon::Date::day
unsigned day(void) const
Get the day of the month of the date.
ucommon::DateTimeString::c_str
const char * c_str(void) const
Extract char from string.
Definition: datetime.h:940
ucommon::Time::operator=
Time & operator=(const Time &time)
Assign a time as a copy of another time.
ucommon::Time::Time
Time()
Create a time from current time.
ucommon::DateTime::operator-=
DateTime & operator-=(long seconds)
Subtract seconds from current datetime object.
ucommon::DateTime::local
static tm_t * local(const time_t *time=NULL)
Fetch an instance of time converted to local time.
typeref.h
A thread-safe atomic heap management system.
ucommon::DateTimeString::~DateTimeString
virtual ~DateTimeString()
Destroy date time string.
ucommon::Time::c_day
static const long c_day
Constant for number of seconds in a day.
Definition: datetime.h:365
ucommon::Time::Time
Time(const Time &object)
Create a time object from another object.
ucommon::Time::operator>
bool operator>(const Time &time) const
Compare time if later than another time.
ucommon::DateTimeString
A DateTime string class.
Definition: datetime.h:870
ucommon::Date::set
void set(const char *pointer, size_t size=0)
Set the julian date based on an ISO date string of specified size.
ucommon::DateTime::operator+
const DateTime operator+(long seconds) const
Add seconds to datetime in an expression.
ucommon::Date::Date
Date(const char *pointer, size_t size=0)
Create a julian date from a ISO date string of a specified size.
ucommon::Date::Date
Date(const struct tm *object)
Create a julian date from a local or gmt date and time.
ucommon::Time::Time
Time(const tm_t *object)
Create a time from the time portion of a date and time object.
ucommon::Time::operator--
Time & operator--()
Decrement time by 1 second, wrap on 24 hour period.
ucommon::DateTime::DateTime
DateTime(const char *pointer, size_t size=0)
Construct a date and time from ISO string buffer.
ucommon::Number
A number manipulation class.
Definition: numbers.h:48
ucommon::Time
The Time class uses a integer representation of the current time.
Definition: datetime.h:352
ucommon::Time::c_week
static const long c_week
Constant for number of seconds in a week.
Definition: datetime.h:375
ucommon::Time::is_valid
bool is_valid(void) const
Check if time object had valid value.
ucommon::Time::operator()
stringref_t operator()() const
Convert to standard 24 hour time string.
ucommon::Date::~Date
virtual ~Date()
Destroy julian date object.
ucommon::Date::operator-
long operator-(const Date &date)
Operator to compute number of days between two dates.
Definition: datetime.h:270
ucommon::DateTime::format
stringref_t format(const char *strftime) const
Return date and time formatted using strftime format values.
ucommon::DateTimeString::DateTimeString
DateTimeString(const time_t time)
Construct a date and time from C libraray time_t type.
ucommon::DateTime::operator+=
DateTime & operator+=(long seconds)
Add seconds to the current datetime object.
ucommon::DateTime
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition: datetime.h:619
ucommon::Time::operator==
bool operator==(const Time &time) const
Compare time with another time to see if same time.
ucommon::Date::Date
Date(time_t value)
Create a julian date from a time_t type.
ucommon::DateTime::put
const char * put(char *buffer) const
Get a ISO formatted date and time string for current object.
ucommon::DateTime::operator!=
bool operator!=(const DateTime &datetime) const
Compare date and time with another date and time to see if not same.
ucommon::Time::minute
int minute(void) const
Get minutes from current hour.
tm_t
struct tm tm_t
Convenience type for struct tm.
Definition: datetime.h:62
ucommon::DateTimeString::DateTimeString
DateTimeString(int year, unsigned month, unsigned day, int hour=0, int minute=0, int second=0)
Construct a date and time object from explicit date and time values.
ucommon::Date
The Date class uses a julian date representation of the current year, month, and day.
Definition: datetime.h:80
ucommon::DateTime::update
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
ucommon::Time::operator-=
Time & operator-=(long seconds)
Decrement time by specified seconds.