1 ///////////////////////////////////////////////////////////////////////////////
\r
3 // File : $Id: time.cpp,v 1.1 2007/05/05 17:00:43 faust Exp $
\r
4 // Subject : IBPP, Time class implementation
\r
6 ///////////////////////////////////////////////////////////////////////////////
\r
8 // (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)
\r
10 // The contents of this file are subject to the IBPP License (the "License");
\r
11 // you may not use this file except in compliance with the License. You may
\r
12 // obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'
\r
13 // file which must have been distributed along with this file.
\r
15 // This software, distributed under the License, is distributed on an "AS IS"
\r
16 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
\r
17 // License for the specific language governing rights and limitations
\r
18 // under the License.
\r
20 ///////////////////////////////////////////////////////////////////////////////
\r
23 // * Tabulations should be set every four characters when editing this file.
\r
25 ///////////////////////////////////////////////////////////////////////////////
\r
28 #pragma warning(disable: 4786 4996)
\r
30 #pragma warning(disable: 4702)
\r
40 #include <time.h> // Can't use <ctime> thanks to MSVC6 buggy library
\r
42 using namespace ibpp_internals;
\r
44 void IBPP::Time::Now()
\r
46 time_t systime = time(0);
\r
47 tm* loctime = localtime(&systime);
\r
48 IBPP::itot(&mTime, loctime->tm_hour, loctime->tm_min, loctime->tm_sec, 0);
\r
51 void IBPP::Time::SetTime(int tm)
\r
53 if (tm < 0 || tm > 863999999)
\r
54 throw LogicExceptionImpl("Time::SetTime", _("Invalid time value"));
\r
58 void IBPP::Time::SetTime(int hour, int minute, int second, int tenthousandths)
\r
60 if (hour < 0 || hour > 23 ||
\r
61 minute < 0 || minute > 59 ||
\r
62 second < 0 || second > 59 ||
\r
63 tenthousandths < 0 || tenthousandths > 9999)
\r
64 throw LogicExceptionImpl("Time::SetTime",
\r
65 _("Invalid hour, minute, second values"));
\r
66 IBPP::itot(&mTime, hour, minute, second, tenthousandths);
\r
69 void IBPP::Time::GetTime(int& hour, int& minute, int& second) const
\r
71 IBPP::ttoi(mTime, &hour, &minute, &second, 0);
\r
74 void IBPP::Time::GetTime(int& hour, int& minute, int& second, int& tenthousandths) const
\r
76 IBPP::ttoi(mTime, &hour, &minute, &second, &tenthousandths);
\r
79 int IBPP::Time::Hours() const
\r
82 IBPP::ttoi(mTime, &hours, 0, 0, 0);
\r
86 int IBPP::Time::Minutes() const
\r
89 IBPP::ttoi(mTime, 0, &minutes, 0, 0);
\r
93 int IBPP::Time::Seconds() const
\r
96 IBPP::ttoi(mTime, 0, 0, &seconds, 0);
\r
100 int IBPP::Time::SubSeconds() const // Actually tenthousandths of seconds
\r
102 int tenthousandths;
\r
103 IBPP::ttoi(mTime, 0, 0, 0, &tenthousandths);
\r
104 return tenthousandths;
\r
107 IBPP::Time::Time(int hour, int minute, int second, int tenthousandths)
\r
109 SetTime(hour, minute, second, tenthousandths);
\r
112 IBPP::Time::Time(const IBPP::Time& copied)
\r
114 mTime = copied.mTime;
\r
117 IBPP::Time& IBPP::Time::operator=(const IBPP::Timestamp& assigned)
\r
119 mTime = assigned.GetTime();
\r
123 IBPP::Time& IBPP::Time::operator=(const IBPP::Time& assigned)
\r
125 mTime = assigned.mTime;
\r
129 // Time calculations. Internal format is the number of seconds elapsed since
\r
130 // midnight. Splits such a time in its hours, minutes, seconds components.
\r
132 void IBPP::ttoi(int itime, int *h, int *m, int *s, int* t)
\r
134 int hh, mm, ss, tt;
\r
136 hh = (int) (itime / 36000000); itime = itime - hh * 36000000;
\r
137 mm = (int) (itime / 600000); itime = itime - mm * 600000;
\r
138 ss = (int) (itime / 10000);
\r
139 tt = (int) (itime - ss * 10000);
\r
141 if (h != 0) *h = hh;
\r
142 if (m != 0) *m = mm;
\r
143 if (s != 0) *s = ss;
\r
144 if (t != 0) *t = tt;
\r
149 // Get the internal time format, given hour, minute, second.
\r
151 void IBPP::itot (int *ptime, int hour, int minute, int second, int tenthousandths)
\r
153 *ptime = hour * 36000000 + minute * 600000 + second * 10000 + tenthousandths;
\r
157 namespace ibpp_internals
\r
161 // The following functions are helper conversions functions between IBPP
\r
162 // Date, Time, Timestamp and ISC_DATE, ISC_TIME and ISC_TIMESTAMP.
\r
163 // (They must be maintained if the encoding used by Firebird evolve.)
\r
164 // These helper functions are used from row.cpp and from array.cpp.
\r
167 void encodeDate(ISC_DATE& isc_dt, const IBPP::Date& dt)
\r
169 // There simply has a shift of 15019 between the native Firebird
\r
170 // date model and the IBPP model.
\r
171 isc_dt = (ISC_DATE)(dt.GetDate() + 15019);
\r
174 void decodeDate(IBPP::Date& dt, const ISC_DATE& isc_dt)
\r
176 // There simply has a shift of 15019 between the native Firebird
\r
177 // date model and the IBPP model.
\r
178 dt.SetDate((int)isc_dt - 15019);
\r
181 void encodeTime(ISC_TIME& isc_tm, const IBPP::Time& tm)
\r
183 isc_tm = (ISC_TIME)tm.GetTime();
\r
186 void decodeTime(IBPP::Time& tm, const ISC_TIME& isc_tm)
\r
188 tm.SetTime((int)isc_tm);
\r
191 void encodeTimestamp(ISC_TIMESTAMP& isc_ts, const IBPP::Timestamp& ts)
\r
193 encodeDate(isc_ts.timestamp_date, ts);
\r
194 encodeTime(isc_ts.timestamp_time, ts);
\r
197 void decodeTimestamp(IBPP::Timestamp& ts, const ISC_TIMESTAMP& isc_ts)
\r
199 decodeDate(ts, isc_ts.timestamp_date);
\r
200 decodeTime(ts, isc_ts.timestamp_time);
\r