]> git.stg.codes - stg.git/blobdiff - libs/ibpp/time.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / libs / ibpp / time.cpp
diff --git a/libs/ibpp/time.cpp b/libs/ibpp/time.cpp
new file mode 100644 (file)
index 0000000..ffdd5f6
--- /dev/null
@@ -0,0 +1,208 @@
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+//     File    : $Id: time.cpp,v 1.1 2007/05/05 17:00:43 faust Exp $\r
+//     Subject : IBPP, Time class implementation\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+//     (C) Copyright 2000-2006 T.I.P. Group S.A. and the IBPP Team (www.ibpp.org)\r
+//\r
+//     The contents of this file are subject to the IBPP License (the "License");\r
+//     you may not use this file except in compliance with the License.  You may\r
+//     obtain a copy of the License at http://www.ibpp.org or in the 'license.txt'\r
+//     file which must have been distributed along with this file.\r
+//\r
+//     This software, distributed under the License, is distributed on an "AS IS"\r
+//     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the\r
+//     License for the specific language governing rights and limitations\r
+//     under the License.\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+//     COMMENTS\r
+//     * Tabulations should be set every four characters when editing this file.\r
+//\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+#ifdef _MSC_VER\r
+#pragma warning(disable: 4786 4996)\r
+#ifndef _DEBUG\r
+#pragma warning(disable: 4702)\r
+#endif\r
+#endif\r
+\r
+#include "_ibpp.h"\r
+\r
+#ifdef HAS_HDRSTOP\r
+#pragma hdrstop\r
+#endif\r
+\r
+#include <time.h>              // Can't use <ctime> thanks to MSVC6 buggy library\r
+\r
+using namespace ibpp_internals;\r
+\r
+void IBPP::Time::Now()\r
+{\r
+       time_t systime = time(0);\r
+       tm* loctime = localtime(&systime);\r
+       IBPP::itot(&mTime, loctime->tm_hour, loctime->tm_min, loctime->tm_sec, 0);\r
+}\r
+\r
+void IBPP::Time::SetTime(int tm)\r
+{\r
+       if (tm < 0 || tm > 863999999)\r
+               throw LogicExceptionImpl("Time::SetTime", _("Invalid time value"));\r
+       mTime = tm;\r
+}\r
+\r
+void IBPP::Time::SetTime(int hour, int minute, int second, int tenthousandths)\r
+{\r
+       if (hour < 0 || hour > 23 ||\r
+               minute < 0 || minute > 59 ||\r
+                       second < 0 || second > 59 ||\r
+                               tenthousandths < 0 || tenthousandths > 9999)\r
+                                       throw LogicExceptionImpl("Time::SetTime",\r
+                                               _("Invalid hour, minute, second values"));\r
+       IBPP::itot(&mTime, hour, minute, second, tenthousandths);\r
+}\r
+\r
+void IBPP::Time::GetTime(int& hour, int& minute, int& second) const\r
+{\r
+       IBPP::ttoi(mTime, &hour, &minute, &second, 0);\r
+}\r
+\r
+void IBPP::Time::GetTime(int& hour, int& minute, int& second, int& tenthousandths) const\r
+{\r
+       IBPP::ttoi(mTime, &hour, &minute, &second, &tenthousandths);\r
+}\r
+\r
+int IBPP::Time::Hours() const\r
+{\r
+       int hours;\r
+       IBPP::ttoi(mTime, &hours, 0, 0, 0);\r
+       return hours;\r
+}\r
+\r
+int IBPP::Time::Minutes() const\r
+{\r
+       int minutes;\r
+       IBPP::ttoi(mTime, 0, &minutes, 0, 0);\r
+       return minutes;\r
+}\r
+\r
+int IBPP::Time::Seconds() const\r
+{\r
+       int seconds;\r
+       IBPP::ttoi(mTime, 0, 0, &seconds, 0);\r
+       return seconds;\r
+}\r
+\r
+int IBPP::Time::SubSeconds() const     // Actually tenthousandths of seconds\r
+{\r
+       int tenthousandths;\r
+       IBPP::ttoi(mTime, 0, 0, 0, &tenthousandths);\r
+       return tenthousandths;\r
+}\r
+\r
+IBPP::Time::Time(int hour, int minute, int second, int tenthousandths)\r
+{\r
+       SetTime(hour, minute, second, tenthousandths);\r
+}\r
+\r
+IBPP::Time::Time(const IBPP::Time& copied)\r
+{\r
+       mTime = copied.mTime;\r
+}\r
+\r
+IBPP::Time& IBPP::Time::operator=(const IBPP::Timestamp& assigned)\r
+{\r
+       mTime = assigned.GetTime();\r
+       return *this;\r
+}\r
+\r
+IBPP::Time& IBPP::Time::operator=(const IBPP::Time& assigned)\r
+{\r
+       mTime = assigned.mTime;\r
+       return *this;\r
+}\r
+\r
+//     Time calculations. Internal format is the number of seconds elapsed since\r
+//     midnight. Splits such a time in its hours, minutes, seconds components.\r
+\r
+void IBPP::ttoi(int itime, int *h, int *m, int *s, int* t)\r
+{\r
+       int hh, mm, ss, tt;\r
+\r
+       hh = (int) (itime / 36000000);  itime = itime - hh * 36000000;\r
+       mm = (int) (itime / 600000);    itime = itime - mm * 600000;\r
+       ss = (int) (itime / 10000);\r
+       tt = (int) (itime - ss * 10000);\r
+\r
+       if (h != 0) *h = hh;\r
+       if (m != 0) *m = mm;\r
+       if (s != 0) *s = ss;\r
+       if (t != 0) *t = tt;\r
+\r
+       return;\r
+}\r
+\r
+//     Get the internal time format, given hour, minute, second.\r
+\r
+void IBPP::itot (int *ptime, int hour, int minute, int second, int tenthousandths)\r
+{\r
+       *ptime = hour * 36000000 + minute * 600000 + second * 10000 + tenthousandths;\r
+       return;\r
+}\r
+\r
+namespace ibpp_internals\r
+{\r
+\r
+//\r
+//     The following functions are helper conversions functions between IBPP\r
+//     Date, Time, Timestamp and ISC_DATE, ISC_TIME and ISC_TIMESTAMP.\r
+//     (They must be maintained if the encoding used by Firebird evolve.)\r
+//     These helper functions are used from row.cpp and from array.cpp.\r
+//\r
+\r
+void encodeDate(ISC_DATE& isc_dt, const IBPP::Date& dt)\r
+{\r
+       // There simply has a shift of 15019 between the native Firebird\r
+       // date model and the IBPP model.\r
+       isc_dt = (ISC_DATE)(dt.GetDate() + 15019);\r
+}\r
+\r
+void decodeDate(IBPP::Date& dt, const ISC_DATE& isc_dt)\r
+{\r
+       // There simply has a shift of 15019 between the native Firebird\r
+       // date model and the IBPP model.\r
+       dt.SetDate((int)isc_dt - 15019);\r
+}\r
+\r
+void encodeTime(ISC_TIME& isc_tm, const IBPP::Time& tm)\r
+{\r
+       isc_tm = (ISC_TIME)tm.GetTime();\r
+}\r
+\r
+void decodeTime(IBPP::Time& tm, const ISC_TIME& isc_tm)\r
+{\r
+       tm.SetTime((int)isc_tm);\r
+}\r
+\r
+void encodeTimestamp(ISC_TIMESTAMP& isc_ts, const IBPP::Timestamp& ts)\r
+{\r
+       encodeDate(isc_ts.timestamp_date, ts);\r
+       encodeTime(isc_ts.timestamp_time, ts);\r
+}\r
+\r
+void decodeTimestamp(IBPP::Timestamp& ts, const ISC_TIMESTAMP& isc_ts)\r
+{\r
+       decodeDate(ts, isc_ts.timestamp_date);\r
+       decodeTime(ts, isc_ts.timestamp_time);\r
+}\r
+\r
+}\r
+\r
+//\r
+//     EOF\r
+//\r
+\r