E-MailRelay
gdate.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2024 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 "gexception.h"
27#include "glog.h"
28#include <ctime>
29#include <string>
30#include <new>
31
32namespace G
33{
34 class Date ;
35}
36
37//| \class G::Date
38/// A day-month-year date class.
39/// \see G::Time, G::DateTime
40///
42{
43public:
44 G_EXCEPTION( DateError , tx("invalid date") )
45 class LocalTime /// An overload discriminator class for Date constructors.
46 {} ;
47
48 enum class Weekday { sunday, monday, tuesday, wednesday, thursday, friday, saturday } ;
49
50 enum class Month { january = 1 , february , march , april , may , june , july , august , september , october , november , december } ;
51
52 enum class Format { yyyy_mm_dd_slash , yyyy_mm_dd , mm_dd } ;
53
54 static int yearUpperLimit() noexcept ;
55 ///< Returns the largest supported year value.
56
57 static int yearLowerLimit() noexcept ;
58 ///< Returns the smallest supported year value.
59
60 Date() ;
61 ///< Default constructor for the current date
62 ///< in the UTC timezone.
63
64 explicit Date( const LocalTime & ) ;
65 ///< Constructor for the current date
66 ///< in the local timezone.
67
68 explicit Date( const BrokenDownTime & tm ) ;
69 ///< Constructor for the specified date.
70
71 explicit Date( SystemTime t ) ;
72 ///< Constructor for the date in the UTC
73 ///< timezone as at the given epoch time.
74
75 Date( SystemTime t , const LocalTime & ) ;
76 ///< Constructor for the date in the local
77 ///< timezone as at the given epoch time.
78
79 Date( int year , Month month , int day_of_month ) ;
80 ///< Constructor for the specified date.
81 ///< Throws if out of range.
82
83 Date( int year , Month month , int day_of_month , std::nothrow_t ) noexcept ;
84 ///< Constructor for the specified date.
85
86 std::string str( Format format = Format::yyyy_mm_dd_slash ) const ;
87 ///< Returns a string representation of the date.
88
89 Weekday weekday() const ;
90 ///< Returns the day of the week.
91
92 std::string weekdayName( bool brief = false ) const ;
93 ///< Returns an english string representation of
94 ///< the day of the week.
95
96 int monthday() const ;
97 ///< Returns the day of the month.
98
99 std::string dd() const ;
100 ///< Returns the day of the month as a two-digit decimal string.
101
102 Month month() const ;
103 ///< Returns the month.
104
105 std::string monthName( bool brief = false ) const ;
106 ///< Returns the month as a string (in english).
107
108 std::string mm() const ;
109 ///< Returns the month as a two-digit decimal string.
110
111 int year() const ;
112 ///< Returns the year.
113
114 std::string yyyy() const ;
115 ///< Returns the year as a four-digit decimal string.
116
117 Date next() const ;
118 ///< Returns the next date.
119
120 Date previous() const ;
121 ///< Returns the previous date.
122
123 Date & operator++() ;
124 ///< Increments the date by one day.
125
126 Date & operator--() ;
127 ///< Decrements the date by one day.
128
129 bool operator==( const Date & rhs ) const ;
130 ///< Comparison operator.
131
132 bool operator!=( const Date & rhs ) const ;
133 ///< Comparison operator.
134
135private:
136 void init( const BrokenDownTime & ) ;
137 void check() const ;
138 static int lastDay( int month , int year ) ;
139 static bool isLeapYear( int y ) ;
140
141private:
142 int m_day {1} ;
143 int m_month {1} ;
144 int m_year {2000} ;
145 bool m_weekday_set {false} ;
146 Weekday m_weekday {Weekday::sunday} ;
147} ;
148
149#endif
An overload discriminator class for Date constructors.
Definition: gdate.h:46
A day-month-year date class.
Definition: gdate.h:42
Date()
Default constructor for the current date in the UTC timezone.
Definition: gdate.cpp:40
std::string monthName(bool brief=false) const
Returns the month as a string (in english).
Definition: gdate.cpp:193
std::string weekdayName(bool brief=false) const
Returns an english string representation of the day of the week.
Definition: gdate.cpp:174
Month month() const
Returns the month.
Definition: gdate.cpp:188
static int yearUpperLimit() noexcept
Returns the largest supported year value.
Definition: gdate.cpp:28
int monthday() const
Returns the day of the month.
Definition: gdate.cpp:143
std::string yyyy() const
Returns the year as a four-digit decimal string.
Definition: gdate.cpp:158
std::string dd() const
Returns the day of the month as a two-digit decimal string.
Definition: gdate.cpp:148
std::string str(Format format=Format::yyyy_mm_dd_slash) const
Returns a string representation of the date.
Definition: gdate.cpp:120
Weekday weekday() const
Returns the day of the week.
Definition: gdate.cpp:163
Date next() const
Returns the next date.
Definition: gdate.cpp:218
static int yearLowerLimit() noexcept
Returns the smallest supported year value.
Definition: gdate.cpp:34
Date previous() const
Returns the previous date.
Definition: gdate.cpp:250
int year() const
Returns the year.
Definition: gdate.cpp:212
std::string mm() const
Returns the month as a two-digit decimal string.
Definition: gdate.cpp:153
Low-level classes.
Definition: garg.h:36
constexpr const char * tx(const char *p) noexcept
A briefer alternative to G::gettext_noop().
Definition: ggettext.h:84
STL namespace.