E-MailRelay
gdate.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gdate.h
19///
20
21#ifndef G_DATE_H
22#define G_DATE_H
23
24#include "gdef.h"
25#include "gdatetime.h"
26#include "glog.h"
27#include <ctime>
28#include <string>
29
30namespace G
31{
32 class Date ;
33}
34
35//| \class G::Date
36/// A day-month-year date class.
37/// \see G::Time, G::DateTime
38///
40{
41public:
42 class LocalTime /// An overload discriminator class for Date constructors.
43 {} ;
44
45 enum class Weekday { sunday, monday, tuesday, wednesday, thursday, friday, saturday } ;
46
47 enum class Month { january = 1 , february , march , april , may , june , july , august , september , october , november , december } ;
48
49 enum class Format { yyyy_mm_dd_slash , yyyy_mm_dd , mm_dd } ;
50
51 static int yearUpperLimit() ;
52 ///< Returns the largest supported year value.
53
54 static int yearLowerLimit() ;
55 ///< Returns the smallest supported year value.
56
57 Date() ;
58 ///< Default constructor for the current date
59 ///< in the UTC timezone.
60
61 explicit Date( const LocalTime & ) ;
62 ///< Constructor for the current date
63 ///< in the local timezone.
64
65 explicit Date( const BrokenDownTime & tm ) ;
66 ///< Constructor for the specified date.
67
68 explicit Date( SystemTime t ) ;
69 ///< Constructor for the date in the UTC
70 ///< timezone as at the given epoch time.
71
72 Date( SystemTime t , const LocalTime & ) ;
73 ///< Constructor for the date in the local
74 ///< timezone as at the given epoch time.
75
76 Date( int year , Month month , int day_of_month ) ;
77 ///< Constructor for the specified date.
78
79 std::string str( Format format = Format::yyyy_mm_dd_slash ) const ;
80 ///< Returns a string representation of the date.
81
82 Weekday weekday() const ;
83 ///< Returns the day of the week.
84
85 std::string weekdayName( bool brief = false ) const ;
86 ///< Returns an english string representation of
87 ///< the day of the week.
88
89 int monthday() const ;
90 ///< Returns the day of the month.
91
92 std::string dd() const ;
93 ///< Returns the day of the month as a two-digit decimal string.
94
95 Month month() const ;
96 ///< Returns the month.
97
98 std::string monthName( bool brief = false ) const ;
99 ///< Returns the month as a string (in english).
100
101 std::string mm() const ;
102 ///< Returns the month as a two-digit decimal string.
103
104 int year() const ;
105 ///< Returns the year.
106
107 std::string yyyy() const ;
108 ///< Returns the year as a four-digit decimal string.
109
110 Date & operator++() ;
111 ///< Increments the date by one day.
112
113 Date & operator--() ;
114 ///< Decrements the date by one day.
115
116 bool operator==( const Date & rhs ) const ;
117 ///< Comparison operator.
118
119 bool operator!=( const Date & rhs ) const ;
120 ///< Comparison operator.
121
122private:
123 void init( const BrokenDownTime & ) ;
124 static int lastDay( int month , int year ) ;
125 static bool isLeapYear( int y ) ;
126
127private:
128 int m_day{1} ;
129 int m_month{1} ;
130 int m_year{2000} ;
131 bool m_weekday_set{false} ;
132 Weekday m_weekday{Weekday::sunday} ;
133} ;
134
135#endif
An overload discriminator class for Date constructors.
Definition: gdate.h:43
A day-month-year date class.
Definition: gdate.h:40
Date()
Default constructor for the current date in the UTC timezone.
Definition: gdate.cpp:39
Date & operator--()
Decrements the date by one day.
Definition: gdate.cpp:205
std::string monthName(bool brief=false) const
Returns the month as a string (in english).
Definition: gdate.cpp:153
std::string weekdayName(bool brief=false) const
Returns an english string representation of the day of the week.
Definition: gdate.cpp:136
bool operator==(const Date &rhs) const
Comparison operator.
Definition: gdate.cpp:260
Month month() const
Returns the month.
Definition: gdate.cpp:148
static int yearUpperLimit()
Returns the largest supported year value.
Definition: gdate.cpp:29
int monthday() const
Returns the day of the month.
Definition: gdate.cpp:106
Date & operator++()
Increments the date by one day.
Definition: gdate.cpp:182
std::string yyyy() const
Returns the year as a four-digit decimal string.
Definition: gdate.cpp:175
std::string dd() const
Returns the day of the month as a two-digit decimal string.
Definition: gdate.cpp:111
std::string str(Format format=Format::yyyy_mm_dd_slash) const
Returns a string representation of the date.
Definition: gdate.cpp:84
Weekday weekday() const
Returns the day of the week.
Definition: gdate.cpp:125
bool operator!=(const Date &rhs) const
Comparison operator.
Definition: gdate.cpp:268
static int yearLowerLimit()
Returns the smallest supported year value.
Definition: gdate.cpp:34
int year() const
Returns the year.
Definition: gdate.cpp:170
std::string mm() const
Returns the month as a two-digit decimal string.
Definition: gdate.cpp:118
Low-level classes.
Definition: galign.h:28