]> git.stg.codes - stg.git/blobdiff - libs/ibpp/date.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / libs / ibpp / date.cpp
diff --git a/libs/ibpp/date.cpp b/libs/ibpp/date.cpp
new file mode 100644 (file)
index 0000000..f483824
--- /dev/null
@@ -0,0 +1,209 @@
+///////////////////////////////////////////////////////////////////////////////\r
+//\r
+//     File    : $Id: date.cpp,v 1.1 2007/05/05 17:00:42 faust Exp $\r
+//     Subject : IBPP, Date 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::Date::Today()\r
+{\r
+       time_t systime = time(0);\r
+       tm* loctime = localtime(&systime);\r
+\r
+       if (! IBPP::itod(&mDate, loctime->tm_year + 1900,\r
+               loctime->tm_mon + 1, loctime->tm_mday))\r
+                       throw LogicExceptionImpl("Date::Today", _("Out of range"));\r
+}\r
+\r
+void IBPP::Date::SetDate(int dt)\r
+{\r
+       if (! IBPP::dtoi(dt, 0, 0, 0))\r
+               throw LogicExceptionImpl("Date::SetDate", _("Out of range"));\r
+       mDate = dt;\r
+}\r
+\r
+void IBPP::Date::SetDate(int year, int month, int day)\r
+{\r
+       if (! IBPP::itod(&mDate, year, month, day))\r
+               throw LogicExceptionImpl("Date::SetDate", _("Out of range"));\r
+}\r
+\r
+void IBPP::Date::GetDate(int& year, int& month, int& day) const\r
+{\r
+       if (! IBPP::dtoi(mDate, &year, &month, &day))\r
+               throw LogicExceptionImpl("Date::GetDate", _("Out of range"));\r
+}\r
+\r
+int IBPP::Date::Year() const\r
+{\r
+       int year;\r
+       if (! IBPP::dtoi(mDate, &year, 0, 0))\r
+               throw LogicExceptionImpl("Date::Year", _("Out of range"));\r
+       return year;\r
+}\r
+\r
+int IBPP::Date::Month() const\r
+{\r
+       int month;\r
+       if (! IBPP::dtoi(mDate, 0, &month, 0))\r
+               throw LogicExceptionImpl("Date::Month", _("Out of range"));\r
+       return month;\r
+}\r
+\r
+int IBPP::Date::Day() const\r
+{\r
+       int day;\r
+       if (! IBPP::dtoi(mDate, 0, 0, &day))\r
+               throw LogicExceptionImpl("Date::Day", _("Out of range"));\r
+       return day;\r
+}\r
+\r
+void IBPP::Date::Add(int days)\r
+{\r
+       int newdate = mDate + days;             // days can be signed\r
+       if (! IBPP::dtoi(newdate, 0, 0, 0))\r
+               throw LogicExceptionImpl("Date::Add()", _("Out of range"));\r
+       mDate = newdate;\r
+}\r
+\r
+void IBPP::Date::StartOfMonth()\r
+{\r
+       int year, month;\r
+       if (! IBPP::dtoi(mDate, &year, &month, 0))\r
+               throw LogicExceptionImpl("Date::StartOfMonth()", _("Out of range"));\r
+       if (! IBPP::itod(&mDate, year, month, 1))               // First of same month\r
+               throw LogicExceptionImpl("Date::StartOfMonth()", _("Out of range"));\r
+}\r
+\r
+void IBPP::Date::EndOfMonth()\r
+{\r
+       int year, month;\r
+       if (! IBPP::dtoi(mDate, &year, &month, 0))\r
+               throw LogicExceptionImpl("Date::EndOfMonth()", _("Out of range"));\r
+       if (++month > 12) { month = 1; year++; }\r
+       if (! IBPP::itod(&mDate, year, month, 1))       // First of next month\r
+               throw LogicExceptionImpl("Date::EndOfMonth()", _("Out of range"));\r
+       mDate--;        // Last day of original month, all weird cases accounted for\r
+}\r
+\r
+IBPP::Date::Date(int year, int month, int day)\r
+{\r
+       SetDate(year, month, day);\r
+}\r
+\r
+IBPP::Date::Date(const IBPP::Date& copied)\r
+{\r
+       mDate = copied.mDate;\r
+}\r
+\r
+IBPP::Date& IBPP::Date::operator=(const IBPP::Timestamp& assigned)\r
+{\r
+       mDate = assigned.GetDate();\r
+       return *this;\r
+}\r
+\r
+IBPP::Date& IBPP::Date::operator=(const IBPP::Date& assigned)\r
+{\r
+       mDate = assigned.mDate;\r
+       return *this;\r
+}\r
+\r
+// The following date calculations were inspired by web pages found on\r
+// Peter Baum web homepage at 'http://www.capecod.net/~pbaum/'.\r
+// His contact info is at : 'http://home.capecod.net/~pbaum/contact.htm'.\r
+// Please, understand that Peter Baum is not related to this IBPP project.\r
+// So __please__, do not contact him regarding IBPP matters.\r
+\r
+//     Take a date, in its integer format as used in IBPP internals and splits\r
+//     it in year (4 digits), month (1-12), day (1-31)\r
+\r
+bool IBPP::dtoi (int date, int *y, int *m, int *d)\r
+{\r
+    int RataDie, Z, H, A, B, C;\r
+    int year, month, day;\r
+\r
+       // Validity control.\r
+       if (date < IBPP::MinDate || date > IBPP::MaxDate)\r
+               return false;\r
+\r
+       // The "Rata Die" is the date specified as the number of days elapsed since\r
+       // 31 Dec of year 0. So 1 Jan 0001 is 1.\r
+\r
+       RataDie = date + ibpp_internals::consts::Dec31_1899;    // Because IBPP sets the '0' on 31 Dec 1899.\r
+\r
+    Z = RataDie + 306;\r
+    H = 100*Z - 25;\r
+    A = H/3652425;\r
+    B = A - A/4;\r
+    year = (100*B + H) / 36525;\r
+    C = B + Z - 365*year - year / 4;\r
+    month = (5*C + 456) / 153;\r
+    day = C - (153*month - 457) / 5;\r
+    if (month > 12) { year += 1; month -= 12; }\r
+\r
+       if (y != 0) *y = (int)year;\r
+       if (m != 0) *m = (int)month;\r
+       if (d != 0) *d = (int)day;\r
+\r
+       return true;\r
+}\r
+\r
+//     Take a date from its components year, month, day and convert it to the\r
+//     integer representation used internally in IBPP.\r
+\r
+bool IBPP::itod (int *pdate, int year, int month, int day)\r
+{\r
+    int RataDie, result;\r
+       int y, m, d;\r
+\r
+       d = day;        m = month;              y = year;\r
+    if (m < 3) { m += 12; y -= 1; }\r
+    RataDie = d + (153*m - 457) / 5 + 365*y + y/4 - y/100 + y/400 - 306;\r
+\r
+       result = RataDie - ibpp_internals::consts::Dec31_1899;   // Because IBPP sets the '0' on 31 Dec 1899\r
+\r
+       // Validity control\r
+       if (result < IBPP::MinDate || result > IBPP::MaxDate)\r
+               return false;\r
+\r
+       *pdate = result;\r
+       return true;\r
+}\r
+\r
+//     Eof\r