1 /*
2  *  Portable Agile C++ Classes (PACC)
3  *  Copyright (C) 2004 by Marc Parizeau
4  *  http://manitou.gel.ulaval.ca/~parizeau/PACC
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2.1 of the License, or (at your option) any later version.
10  *
11  *  This library 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 GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  Contact:
21  *  Laboratoire de Vision et Systemes Numeriques
22  *  Departement de genie electrique et de genie informatique
23  *  Universite Laval, Quebec, Canada, G1K 7P4
24  *  http://vision.gel.ulaval.ca
25  *
26  */
27 
28 /*!
29  * \file PACC/Util/Date.hpp
30  * \brief Class definition for the portable time and date.
31  * \author Marc Parizeau, Laboratoire de vision et systèmes numériques, Université Laval
32  * $Revision: 1.5.2.1 $
33  * $Date: 2007/09/10 18:24:10 $
34  */
35 
36 #ifndef PACC_Date_hpp
37 #define PACC_Date_hpp
38 
39 #include <string>
40 #include <ctime>
41 
42 namespace PACC
43 {
44 
45 using namespace std;
46 
47 /*! \brief Portable time and date class.
48 \author Marc Parizeau, Laboratoire de vision et syst&egrave;mes num&eacute;riques, Universit&eacute; Laval
49 \ingroup Util
50 */
51 class Date
52 {
53 public:
54 
55 	//! Construct using current system date and time.
Date(void)56 	Date(void) : mTime(::time(0))
57 	{}
58 
59 	//! Construct from \c inSeconds seconds since January 1st, 1970.
Date(unsigned int inSeconds)60 	explicit Date(unsigned int inSeconds) : mTime(inSeconds)
61 	{}
62 
63 	// Construct with specified date.
64 	Date(unsigned int inYear, unsigned int inMonth, unsigned int inDay);
65 
66 
67 	//! Return number of seconds between this date and date \c inDate.
operator -(const Date & inDate) const68 	unsigned int operator-(const Date& inDate) const
69 	{
70 		return mTime-inDate.mTime;
71 	}
72 
73 	//! Return this date less \c inSeconds seconds.
operator -(unsigned int inSeconds) const74 	Date operator-(unsigned int inSeconds) const
75 	{
76 		Date lDate(mTime);
77 		return lDate -= inSeconds;
78 	}
79 
80 	//! Remove \c inSeconds seconds from this date.
operator -=(unsigned int inSeconds)81 	Date& operator-=(unsigned int inSeconds)
82 	{
83 		mTime -= inSeconds;
84 		return *this;
85 	}
86 
87 	//! Return this date plus \c inSeconds seconds.
operator +(unsigned int inSeconds) const88 	Date operator+(unsigned int inSeconds) const
89 	{
90 		Date lDate(mTime);
91 		return lDate += inSeconds;
92 	}
93 
94 	//! Add \c inSeconds seconds to this date.
operator +=(unsigned int inSeconds)95 	Date& operator+=(unsigned int inSeconds)
96 	{
97 		mTime += inSeconds;
98 		return *this;
99 	}
100 
101 	// Return formatted date and time using the std::strftime string format \c inFormat.
102 	string get(const string& inFormat = "%a %b %d %T %Z %Y") const;
103 	// Return day of month for this date (1-31).
104 	unsigned int getDayOfMonth(void) const;
105 	// Return day of week for this date (sunday=0-6).
106 	unsigned int getDayOfWeek(void) const;
107 	// Return day of year for this date (1-365).
108 	unsigned int getDayOfYear(void) const;
109 	// Return hour of day for this date (0-23).
110 	unsigned int getHourOfDay(void) const;
111 	// Return minutes of hour for this date (0-59).
112 	unsigned int getMinutesOfHour(void) const;
113 	// Return seconds of minutes for this date (0-59).
114 	double getSecondsOfMinute(void) const;
115 	// Return month of year for this date (1-12).
116 	unsigned int getMonthOfYear(void) const;
117 	// Return this date expressed in seconds since January 1st, 1970.
118 	unsigned int getTimeInSeconds(void) const;
119 	// Return year of this date.
120 	unsigned int getYear(void) const;
121 	// Set date to year \c inyear, month \c inMonth, and day \c inDay (time is unchanged).
122 	void setDate(unsigned int inYear, unsigned int inMonth, unsigned int inDay);
123 	// Set time to \c inHour hour, \c inMinutes minutes, and \c inSeconds seconds (date is unchanged).
124 	void setTime(unsigned int inHour, unsigned int inMinutes, unsigned int inSeconds);
125 
126 protected:
127 
128 	time_t mTime; //!< Number of seconds since Jan 1st, 1970.
129 
130 };
131 
132 } // end of PACC namespace
133 
134 #endif // PACC_Date_hpp
135