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.cpp
30  * \brief Class methods for the portable time and date.
31  * \author Marc Parizeau, Laboratoire de vision et systèmes numériques, Université Laval
32  * $Revision: 1.6.2.1 $
33  * $Date: 2007/09/10 18:24:10 $
34  */
35 
36 #include "Util/Date.hpp"
37 
38 using namespace std;
39 using namespace PACC;
40 
41 //! Construct with specified date \c inYear, \c inMonth, and \c inDay.
Date(unsigned int inYear,unsigned int inMonth,unsigned int inDay)42 Date::Date(unsigned int inYear, unsigned int inMonth, unsigned int inDay) : mTime(0)
43 {
44 	setDate(inYear, inMonth, inDay);
45 	setTime(0, 0, 0);
46 }
47 
48 //! Return formatted date and time using the std::strftime string format \c inFormat.
get(const string & inFormat) const49 std::string Date::get(const string& inFormat) const
50 {
51 #ifdef WIN32
52 	struct tm& lTime = *::localtime(&mTime);
53 #else
54     struct tm lTime;
55 	::localtime_r(&mTime, &lTime);
56 #endif
57 	int lBufSize = 128;
58 	char* lBuffer = new char[lBufSize];
59 	while(::strftime(lBuffer, lBufSize, inFormat.c_str(), &lTime) == 0) {
60 		// buffer needs to be enlarged
61 		delete[] lBuffer;
62 		lBuffer = new char[lBufSize*=2];
63 	}
64 	string lDate(lBuffer);
65 	delete[] lBuffer;
66 	return lDate;
67 }
68 
69 //! Return day of month for this date (1-31).
getDayOfMonth(void) const70 unsigned int Date::getDayOfMonth(void) const
71 {
72 #ifdef WIN32
73 	struct tm& lTime = *::localtime(&mTime);
74 #else
75     struct tm lTime;
76 	::localtime_r(&mTime, &lTime);
77 #endif
78 	return lTime.tm_mday;
79 }
80 
81 //! Return day of week for this date (sunday=0-6).
getDayOfWeek(void) const82 unsigned int Date::getDayOfWeek(void) const
83 {
84 #ifdef WIN32
85 	struct tm& lTime = *::localtime(&mTime);
86 #else
87     struct tm lTime;
88 	::localtime_r(&mTime, &lTime);
89 #endif
90 	return lTime.tm_wday;
91 }
92 
93 //! Return day of year for this date (1-365).
getDayOfYear(void) const94 unsigned int Date::getDayOfYear(void) const
95 {
96 #ifdef WIN32
97 	struct tm& lTime = *::localtime(&mTime);
98 #else
99     struct tm lTime;
100 	::localtime_r(&mTime, &lTime);
101 #endif
102 	return lTime.tm_yday+1;
103 }
104 
105 //! Return hour of day for this date (0-23).
getHourOfDay(void) const106 unsigned int Date::getHourOfDay(void) const
107 {
108 #ifdef WIN32
109 	struct tm& lTime = *::localtime(&mTime);
110 #else
111     struct tm lTime;
112 	::localtime_r(&mTime, &lTime);
113 #endif
114 	return lTime.tm_hour;
115 }
116 
117 //! Return minutes of hour for this date (0-59).
getMinutesOfHour(void) const118 unsigned int Date::getMinutesOfHour(void) const
119 {
120 #ifdef WIN32
121 	struct tm& lTime = *::localtime(&mTime);
122 #else
123     struct tm lTime;
124 	::localtime_r(&mTime, &lTime);
125 #endif
126 	return lTime.tm_min;
127 }
128 
129 //! Return seconds of minutes for this date (0-59).
getSecondsOfMinute(void) const130 double Date::getSecondsOfMinute(void) const
131 {
132 #ifdef WIN32
133 	struct tm& lTime = *::localtime(&mTime);
134 #else
135     struct tm lTime;
136 	::localtime_r(&mTime, &lTime);
137 #endif
138 	return lTime.tm_sec;
139 }
140 
141 //! Return month of year for this date (1-12).
getMonthOfYear(void) const142 unsigned int Date::getMonthOfYear(void) const
143 {
144 #ifdef WIN32
145 	struct tm& lTime = *::localtime(&mTime);
146 #else
147     struct tm lTime;
148 	::localtime_r(&mTime, &lTime);
149 #endif
150 	return lTime.tm_mon+1;
151 }
152 
153 //! Return this date expressed in seconds since January 1st, 1970.
getTimeInSeconds(void) const154 unsigned int Date::getTimeInSeconds(void) const
155 {
156 	return mTime;
157 }
158 
159 //! Return year of this date.
getYear(void) const160 unsigned int Date::getYear(void) const
161 {
162 #ifdef WIN32
163 	struct tm& lTime = *::localtime(&mTime);
164 #else
165     struct tm lTime;
166 	::localtime_r(&mTime, &lTime);
167 #endif
168 	return lTime.tm_year+1900;
169 }
170 
171 //! Set date to year \c inyear, month \c inMonth, and day \c inDay (time is unchanged).
setDate(unsigned int inYear,unsigned int inMonth,unsigned int inDay)172 void Date::setDate(unsigned int inYear, unsigned int inMonth, unsigned int inDay)
173 {
174 #ifdef WIN32
175 	struct tm& lTime = *::localtime(&mTime);
176 #else
177     struct tm lTime;
178 	::localtime_r(&mTime, &lTime);
179 #endif
180 	lTime.tm_year = inYear-1900;
181 	lTime.tm_mon = inMonth-1;
182 	lTime.tm_mday = inDay;
183 	mTime = ::mktime(&lTime);
184 }
185 
186 //! Set time to \c inHour hour, \c inMinutes minutes, and \c inSeconds seconds (date is unchanged).
setTime(unsigned int inHour,unsigned int inMinutes,unsigned int inSeconds)187 void Date::setTime(unsigned int inHour, unsigned int inMinutes, unsigned int inSeconds)
188 {
189 #ifdef WIN32
190 	struct tm& lTime = *::localtime(&mTime);
191 #else
192     struct tm lTime;
193 	::localtime_r(&mTime, &lTime);
194 #endif
195 	lTime.tm_hour = inHour;
196 	lTime.tm_min = inMinutes;
197 	lTime.tm_sec = inSeconds;
198 	mTime = ::mktime(&lTime);
199 }
200