1 //
2 // Copyright (C) 2001-2013 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 "gdebug.h"
27 #include <ctime>
28 #include <string>
29 
30 /// \namespace G
31 namespace G
32 {
33 	class Date ;
34 }
35 
36 /// \class G::Date
37 /// A date (dd/mm/yyyy) class.
38 /// \see G::Time, G::DateTime
39 ///
40 class G::Date
41 {
42 public:
43 	/// An overload discriminator class for Date constructors.
44 	class LocalTime
45 		{} ;
46 
47 	enum Weekday
48 		{ sunday, monday, tuesday, wednesday, thursday, friday, saturday } ;
49 
50 	enum Month
51 		{ january = 1 , february , march , april , may , june , july ,
52 		august , september , october , november , december } ;
53 
54 	enum Format
55 		{ yyyy_mm_dd_slash , yyyy_mm_dd , mm_dd } ;
56 
57 	static int yearUpperLimit() ;
58 		///< Returns the largest supported year value.
59 
60 	static int yearLowerLimit() ;
61 		///< Returns the smallest supported year value.
62 
63 	Date() ;
64 		///< Default constructor for the current date
65 		///< in the UTC timezone.
66 
67 	explicit Date( const LocalTime & ) ;
68 		///< Constructor for the current date
69 		///< in the local timezone.
70 
71 	Date( const G::DateTime::BrokenDownTime & tm ) ;
72 		///< Constructor for the specified date.
73 
74 	explicit Date( G::DateTime::EpochTime t ) ;
75 		///< Constructor for the date in the UTC
76 		///< timezone as at the given epoch time.
77 
78 	Date( G::DateTime::EpochTime t , const LocalTime & ) ;
79 		///< Constructor for the date in the local
80 		///< timezone as at the given epoch time.
81 
82 	Date( int year , Month month , int day_of_month ) ;
83 		///< Constructor for the specified date.
84 
85 	std::string string( Format format = yyyy_mm_dd_slash ) const ;
86 		///< Returns a string representation of the date.
87 
88 	Weekday weekday() const ;
89 		///< Returns the day of the week.
90 
91 	std::string weekdayName( bool brief = false ) const ;
92 		///< Returns an english string representation of
93 		///< the day of the week.
94 		///< (Was weekdayString().)
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 		///< (Was monthdayString().)
102 
103 	Month month() const ;
104 		///< Returns the month.
105 
106 	std::string monthName( bool brief = false ) const ;
107 		///< Returns the month as a string (in english).
108 
109 	std::string mm() const ;
110 		///< Returns the month as a two-digit decimal string.
111 
112 	int year() const ;
113 		///< Returns the year.
114 
115 	std::string yyyy() const ;
116 		///< Returns the year as a four-digit decimal string.
117 		///< (Was yearString().)
118 
119 	Date & operator++() ;
120 		///< Increments the date by one day.
121 
122 	Date & operator--() ;
123 		///< Decrements the date by one day.
124 
125 	bool operator==( const Date & rhs ) const ;
126 		///< Comparison operator.
127 
128 	bool operator!=( const Date & rhs ) const ;
129 		///< Comparison operator.
130 
131 private:
132 	void init( const G::DateTime::BrokenDownTime & ) ;
133 	static int lastDay( int month , int year ) ;
134 	static bool isLeapYear( int y ) ;
135 
136 private:
137 	int m_day ;
138 	int m_month ;
139 	int m_year ;
140 	bool m_weekday_set ;
141 	Weekday m_weekday ;
142 } ;
143 
144 #endif
145