]> git.stg.codes - stg.git/blob - libs/ibpp/time.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / libs / ibpp / time.cpp
1 ///////////////////////////////////////////////////////////////////////////////\r
2 //\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
5 //\r
6 ///////////////////////////////////////////////////////////////////////////////\r
7 //\r
8 //      (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
9 //\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
14 //\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
19 //\r
20 ///////////////////////////////////////////////////////////////////////////////\r
21 //\r
22 //      COMMENTS\r
23 //      * Tabulations should be set every four characters when editing this file.\r
24 //\r
25 ///////////////////////////////////////////////////////////////////////////////\r
26 \r
27 #ifdef _MSC_VER\r
28 #pragma warning(disable: 4786 4996)\r
29 #ifndef _DEBUG\r
30 #pragma warning(disable: 4702)\r
31 #endif\r
32 #endif\r
33 \r
34 #include "_ibpp.h"\r
35 \r
36 #ifdef HAS_HDRSTOP\r
37 #pragma hdrstop\r
38 #endif\r
39 \r
40 #include <time.h>               // Can't use <ctime> thanks to MSVC6 buggy library\r
41 \r
42 using namespace ibpp_internals;\r
43 \r
44 void IBPP::Time::Now()\r
45 {\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
49 }\r
50 \r
51 void IBPP::Time::SetTime(int tm)\r
52 {\r
53         if (tm < 0 || tm > 863999999)\r
54                 throw LogicExceptionImpl("Time::SetTime", _("Invalid time value"));\r
55         mTime = tm;\r
56 }\r
57 \r
58 void IBPP::Time::SetTime(int hour, int minute, int second, int tenthousandths)\r
59 {\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
67 }\r
68 \r
69 void IBPP::Time::GetTime(int& hour, int& minute, int& second) const\r
70 {\r
71         IBPP::ttoi(mTime, &hour, &minute, &second, 0);\r
72 }\r
73 \r
74 void IBPP::Time::GetTime(int& hour, int& minute, int& second, int& tenthousandths) const\r
75 {\r
76         IBPP::ttoi(mTime, &hour, &minute, &second, &tenthousandths);\r
77 }\r
78 \r
79 int IBPP::Time::Hours() const\r
80 {\r
81         int hours;\r
82         IBPP::ttoi(mTime, &hours, 0, 0, 0);\r
83         return hours;\r
84 }\r
85 \r
86 int IBPP::Time::Minutes() const\r
87 {\r
88         int minutes;\r
89         IBPP::ttoi(mTime, 0, &minutes, 0, 0);\r
90         return minutes;\r
91 }\r
92 \r
93 int IBPP::Time::Seconds() const\r
94 {\r
95         int seconds;\r
96         IBPP::ttoi(mTime, 0, 0, &seconds, 0);\r
97         return seconds;\r
98 }\r
99 \r
100 int IBPP::Time::SubSeconds() const      // Actually tenthousandths of seconds\r
101 {\r
102         int tenthousandths;\r
103         IBPP::ttoi(mTime, 0, 0, 0, &tenthousandths);\r
104         return tenthousandths;\r
105 }\r
106 \r
107 IBPP::Time::Time(int hour, int minute, int second, int tenthousandths)\r
108 {\r
109         SetTime(hour, minute, second, tenthousandths);\r
110 }\r
111 \r
112 IBPP::Time::Time(const IBPP::Time& copied)\r
113 {\r
114         mTime = copied.mTime;\r
115 }\r
116 \r
117 IBPP::Time& IBPP::Time::operator=(const IBPP::Timestamp& assigned)\r
118 {\r
119         mTime = assigned.GetTime();\r
120         return *this;\r
121 }\r
122 \r
123 IBPP::Time& IBPP::Time::operator=(const IBPP::Time& assigned)\r
124 {\r
125         mTime = assigned.mTime;\r
126         return *this;\r
127 }\r
128 \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
131 \r
132 void IBPP::ttoi(int itime, int *h, int *m, int *s, int* t)\r
133 {\r
134         int hh, mm, ss, tt;\r
135 \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
140 \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
145 \r
146         return;\r
147 }\r
148 \r
149 //      Get the internal time format, given hour, minute, second.\r
150 \r
151 void IBPP::itot (int *ptime, int hour, int minute, int second, int tenthousandths)\r
152 {\r
153         *ptime = hour * 36000000 + minute * 600000 + second * 10000 + tenthousandths;\r
154         return;\r
155 }\r
156 \r
157 namespace ibpp_internals\r
158 {\r
159 \r
160 //\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
165 //\r
166 \r
167 void encodeDate(ISC_DATE& isc_dt, const IBPP::Date& dt)\r
168 {\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
172 }\r
173 \r
174 void decodeDate(IBPP::Date& dt, const ISC_DATE& isc_dt)\r
175 {\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
179 }\r
180 \r
181 void encodeTime(ISC_TIME& isc_tm, const IBPP::Time& tm)\r
182 {\r
183         isc_tm = (ISC_TIME)tm.GetTime();\r
184 }\r
185 \r
186 void decodeTime(IBPP::Time& tm, const ISC_TIME& isc_tm)\r
187 {\r
188         tm.SetTime((int)isc_tm);\r
189 }\r
190 \r
191 void encodeTimestamp(ISC_TIMESTAMP& isc_ts, const IBPP::Timestamp& ts)\r
192 {\r
193         encodeDate(isc_ts.timestamp_date, ts);\r
194         encodeTime(isc_ts.timestamp_time, ts);\r
195 }\r
196 \r
197 void decodeTimestamp(IBPP::Timestamp& ts, const ISC_TIMESTAMP& isc_ts)\r
198 {\r
199         decodeDate(ts, isc_ts.timestamp_date);\r
200         decodeTime(ts, isc_ts.timestamp_time);\r
201 }\r
202 \r
203 }\r
204 \r
205 //\r
206 //      EOF\r
207 //\r
208 \r