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
62typedef struct tm tm_t;
63
64namespace ucommon {
65
66#ifdef __BORLANDC__
67 using std::tm;
68 using std::time_t;
69#endif
70
79class __EXPORT Date
80{
81protected:
82 long julian;
83
84 void set(long year, long month, long day);
85
90 virtual void update(void);
91
92public:
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
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
351class __EXPORT Time
352{
353protected:
354 long seconds;
355
356protected:
357 virtual void update(void);
358
359public:
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
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
618class __EXPORT DateTime : public Date, public Time
619{
620protected:
621 virtual void update(void) __OVERRIDE;
622
623public:
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
869class __EXPORT DateTimeString : public DateTime
870{
871public:
876 typedef enum {
877 DATE, TIME, BOTH
878 } mode_t;
879
880private:
881 char buffer[DATETIME_BUFFER_SIZE];
882 mode_t mode;
883
884protected:
885 virtual void update(void) __OVERRIDE;
886
887public:
892 DateTimeString(const time_t time);
893
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
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
971class __EXPORT DateNumber : public Number, public Date
972{
973protected:
974 virtual void update(void) __OVERRIDE;
975
976public:
982
986 virtual ~DateNumber();
987
991 void set(void);
992};
993
994class __EXPORT isotime : public __PROTOCOL PrintProtocol, public __PROTOCOL InputProtocol
995{
996private:
997 Date *d;
998 Time *t;
999
1000 enum {
1001 DATE, TIME, DATETIME
1002 } mode;
1003
1004 char buf[32];
1005 unsigned pos;
1006
1007protected:
1008 const char *_print(void) const;
1009
1010 int _input(int code) __OVERRIDE;
1011
1012public:
1013 isotime(Date& date, Time& time);
1014 isotime(Date& date);
1015 isotime(Time& time);
1016};
1017
1022
1027
1031typedef Date date_t;
1032
1036typedef Time tod_t;
1037
1038} // namespace ucommon
1039
1040#endif
A thread-safe atomic heap management system.
Various miscellaneous platform specific headers and defines.
struct tm tm_t
Convenience type for struct tm.
Definition datetime.h:62
Common namespace for all ucommon objects.
Definition access.h:47
DateTime datetime_t
Convenience type for using DateTime object.
Definition datetime.h:1021
DateTimeString datetimestring_t
Convenience type for using DateTimeString object.
Definition datetime.h:1026
Time tod_t
Convenience type for using Time object.
Definition datetime.h:1036
Date date_t
Convenience type for using Date object.
Definition datetime.h:1031
The Date class uses a julian date representation of the current year, month, and day.
Definition datetime.h:80
Date & operator-=(long offset)
Decrement date by offset.
Date(const struct tm *object)
Create a julian date from a local or gmt date and time.
Date & operator=(const Date &date)
Assign date from another date object.
unsigned dow(void) const
Get the day of the week (0-7).
bool operator>=(const Date &date) const
Compare julian date if later than or equal to another date.
Date & operator--()
Decrement date by one day.
Date()
Construct a new julian date with today's date.
int year(void) const
Get the year of the date.
void set(const char *pointer, size_t size=0)
Set the julian date based on an ISO date string of specified size.
virtual ~Date()
Destroy julian date object.
Date(const Date &object)
Create a julian date object from another object.
void set(void)
Set (update) the date with current date.
long operator-(const Date &date)
Operator to compute number of days between two dates.
Definition datetime.h:270
const char * put(char *buffer) const
Get a ISO string representation of the date (yyyy-mm-dd).
bool operator!=(const Date &date) const
Compare julian dates if not same date.
const Date operator-(long days) const
Subtract days from a julian date in an expression.
bool operator>(const Date &date) const
Compare julian date if later than another date.
bool operator<=(const Date &date) const
Compare julian date if earlier than or equal to another date.
static const size_t sz_string
Size of date string field.
Definition datetime.h:96
bool operator==(const Date &date) const
Compare julian dates if same date.
long operator*() const
Access julian value.
Definition datetime.h:214
unsigned day(void) const
Get the day of the month of the date.
Date & operator+=(long offset)
Increment date by offset.
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
stringref_t operator()() const
Expression operator to return an ISO date string for the current julian date.
bool operator<(const Date &date) const
Compare julian date if earlier than another date.
Date(time_t value)
Create a julian date from a time_t type.
long get(void) const
Get the date as a number for the object or 0 if invalid.
time_t timeref(void) const
Get a time_t for the julian date if in time_t epoch.
bool is_valid(void) const
Check if date is valid.
bool operator!() const
Check if julian date is not valid.
Definition datetime.h:327
Date(const char *pointer, size_t size=0)
Create a julian date from a ISO date string of a specified size.
unsigned month(void) const
Get the month of the date (1-12).
Date & operator++()
Increment date by one day.
const Date operator+(long days) const
Add days to julian date in an expression.
Date(int year, unsigned month, unsigned day)
Create a julian date from an arbitrary year, month, and day.
The Time class uses a integer representation of the current time.
Definition datetime.h:352
static const long c_day
Constant for number of seconds in a day.
Definition datetime.h:365
Time & operator-=(long seconds)
Decrement time by specified seconds.
bool operator<=(const Time &time) const
Compare time if earlier than or equal to another time.
long operator*() const
Get object time in seconds.
Definition datetime.h:523
Time()
Create a time from current time.
virtual ~Time()
Destroy time object.
Time(const Time &object)
Create a time object from another object.
Time & operator+=(long seconds)
Increment time by specified seconds.
bool operator==(const Time &time) const
Compare time with another time to see if same time.
static const size_t sz_string
Size of time string field.
Definition datetime.h:380
Time & operator--()
Decrement time by 1 second, wrap on 24 hour period.
stringref_t operator()() const
Convert to standard 24 hour time string.
int hour(void) const
Get hours from midnight.
const char * put(char *buffer) const
Get a hh:mm:ss formatted string for current time.
int minute(void) const
Get minutes from current hour.
bool operator<(const Time &time) const
Compare time if earlier than another time.
const Time operator+(long seconds) const
Add seconds to the current time, wrap if 24 hours.
bool operator!() const
Check if time object has valid value for ! operator.
Definition datetime.h:486
static const long c_hour
Constant for number of seconds in a hour.
Definition datetime.h:370
bool is_valid(void) const
Check if time object had valid value.
bool operator!=(const Time &time) const
Compare time with another time to see if not same time.
Time(int hour, int minute, int second)
Create a time from hours (0-23), minutes (0-59), and seconds (0-59).
long get(void) const
Get current time in seconds from midnight.
void set(void)
Set (update) the time with current time.
Time(const tm_t *object)
Create a time from the time portion of a date and time object.
const Time operator-(long seconds) const
Subtract seconds to the current time, wrap if 24 hours.
static const long c_week
Constant for number of seconds in a week.
Definition datetime.h:375
int second(void) const
Get seconds from current minute.
long operator-(const Time &reference)
Get difference (in seconds) between two times.
bool operator>=(const Time &time) const
Compare time if later than or equal to another time.
void set(const char *pointer, size_t size=0)
Set time from a hh:mm:ss formatted string.
Time(const char *pointer, size_t size=0)
Create a time from a hh:mm:ss formatted time string.
Time & operator++()
Incrememnt time by 1 second, wrap on 24 hour period.
bool operator>(const Time &time) const
Compare time if later than another time.
Time(const time_t value)
Create a time from the time portion of a time_t.
Time & operator=(const Time &time)
Assign a time as a copy of another time.
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition datetime.h:619
bool operator!() const
Check if date and time is not valid.
DateTime(const DateTime &object)
Create a datetime object from another object.
static tm_t * local(const time_t *time=NULL)
Fetch an instance of time converted to local time.
DateTime(const time_t time)
Construct a date and time from C library time_t type.
DateTime & operator-=(long seconds)
Subtract seconds from current datetime object.
bool operator<=(const DateTime &datetime) const
Compare date and time with another date and time to see if earlier or the same.
stringref_t format(const char *strftime) const
Return date and time formatted using strftime format values.
const DateTime operator-(long seconds) const
Subtract seconds from datetime in an expression.
void set(void)
Set (update) the date and time with current date and time.
const char * put(char *buffer) const
Get a ISO formatted date and time string for current object.
DateTime(const tm_t *tm)
Construct a date and time from C library time structure.
DateTime(const char *pointer, size_t size=0)
Construct a date and time from ISO string buffer.
static tm_t * gmt(const time_t *time=NULL)
Fetch an instance of time converted to gmt.
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
static void release(tm_t *object)
Release a struct tm object from glt or gmt.
DateTime & operator--()
Subtract a day from the current date and time.
bool operator<(const DateTime &datetime) const
Compare date and time with another date and time to see if earlier.
DateTime()
Construct a new date and time object with current date and time.
time_t get(void) const
Get C library time_t type if object in C library epoch range.
DateTime & operator=(const DateTime &datetime)
Assign date and time from another datetime object.
bool is_valid(void) const
Test if object is valid.
const DateTime operator+(long seconds) const
Add seconds to datetime in an expression.
bool operator==(const DateTime &datetime) const
Compare date and time with another date and time to see if the same.
long operator-(const DateTime &datetime)
Operator to compute number of days between two dates.
virtual ~DateTime()
Destroy date and time object.
bool operator>(const DateTime &datetime) const
Compare date and time with another date and time to see if later.
static const size_t sz_string
Size of datetime string field.
Definition datetime.h:627
DateTime & operator++()
Add a day from the current date and time.
bool operator!=(const DateTime &datetime) const
Compare date and time with another date and time to see if not same.
DateTime & operator+=(long seconds)
Add seconds to the current datetime object.
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.
bool operator>=(const DateTime &datetime) const
Compare date and time with another date and time to see if later or the same.
A DateTime string class.
Definition datetime.h:870
DateTimeString(const char *pointer, size_t size=0)
Construct a date and time from ISO string buffer.
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
virtual ~DateTimeString()
Destroy date time string.
DateTimeString(const tm_t *tm)
Construct a date and time from C library time structure.
void set(void)
Set (update) the date and time with current date and time.
mode_t
Specify string buffer mode.
Definition datetime.h:876
DateTimeString(mode_t string=DateTimeString::BOTH)
Construct a new date and time object with current date and time.
const char * c_str(void) const
Extract char from string.
Definition datetime.h:940
DateTimeString(const time_t time)
Construct a date and time from C libraray time_t type.
void set(mode_t string)
Set the string mode.
DateTimeString(const DateTimeString &object)
Create a datetime object from another object.
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.
A number class that manipulates a string buffer that is also a date.
Definition datetime.h:972
virtual ~DateNumber()
Release a datenumber object.
DateNumber(char *pointer)
Create a date number tied to a refreshed string buffer.
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
void set(void)
Set date number to current date.
Generic smart pointer class.
Definition generics.h:55
A number manipulation class.
Definition numbers.h:48
Used for forming stream output.
Definition protocols.h:135
Used for processing input.
Definition protocols.h:154
Support classes for manipulation of numbers as strings.