UCommon
datetime.h
Go to the documentation of this file.
1// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015-2020 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
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
229 Date& operator++();
230
235 Date& operator--();
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
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
537 Time& operator++();
538
543 Time& operator--();
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
669 DateTime();
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
745 DateTime& operator++();
746
751 DateTime& operator--();
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
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
923 DateTimeString(const DateTimeString& object);
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
971class __EXPORT DateNumber : public Number, public Date
972{
973protected:
974 virtual void update(void) __OVERRIDE;
975
976public:
981 DateNumber(char *pointer);
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 __OVERRIDE;
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
1021typedef DateTime datetime_t;
1022
1026typedef DateTimeString datetimestring_t;
1027
1031typedef Date date_t;
1032
1036typedef Time tod_t;
1037
1038} // namespace ucommon
1039
1040#endif
struct tm tm_t
Convenience type for struct tm.
Definition datetime.h:62
A thread-safe atomic heap management system.
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition access.h:47
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
class __attribute__((visibility("default"))) DateTime class __attribute__((visibility("default"))) DateTimeString class __attribute__((visibility("default"))) DateNumber class __attribute__((visibility("default"))) isotime typedef DateTime datetime_t
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition datetime.h:994
Support classes for manipulation of numbers as strings.