Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

datetime.cpp

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2005 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00029 #include "precomp.h"
00030 #include "datetime.h"
00031 #include "exception.h"
00032 #include "string_help.h"
00033 
00035 // CL_DateTime Construction:
00036 
00037 CL_DateTime::CL_DateTime()
00038 : unix_time(0), tm_local(0), tm_utc(0)
00039 {
00040 }
00041 
00042 CL_DateTime::CL_DateTime(time_t unix_time)
00043 : unix_time(unix_time), tm_local(0), tm_utc(0)
00044 {
00045 }
00046 
00047 CL_DateTime::CL_DateTime(const CL_DateTime &other)
00048 : unix_time(other.unix_time), tm_local(0), tm_utc(0)
00049 {
00050 }
00051 
00052 CL_DateTime::~CL_DateTime()
00053 {
00054         delete tm_local;
00055         delete tm_utc;
00056 }
00057 
00058 CL_DateTime CL_DateTime::get_system_time()
00059 {
00060         return CL_DateTime(time(0));
00061 }
00062 
00064 // CL_DateTime Attributes:
00065 
00066 time_t CL_DateTime::get_unix_time() const
00067 {
00068         return unix_time;
00069 }
00070 
00071 int CL_DateTime::get_seconds_local() const
00072 {
00073         load_local();
00074         return tm_local->tm_sec;
00075 }
00076 
00077 int CL_DateTime::get_seconds_utc() const
00078 {
00079         load_utc();
00080         return tm_utc->tm_sec;
00081 }
00082 
00083 int CL_DateTime::get_minutes_local() const
00084 {
00085         load_local();
00086         return tm_local->tm_min;
00087 }
00088 
00089 int CL_DateTime::get_minutes_utc() const
00090 {
00091         load_utc();
00092         return tm_utc->tm_min;
00093 }
00094 
00095 int CL_DateTime::get_hours_local() const
00096 {
00097         load_local();
00098         return tm_local->tm_hour;
00099 }
00100         
00101 int CL_DateTime::get_hours_utc() const
00102 {
00103         load_utc();
00104         return tm_utc->tm_hour;
00105 }
00106         
00107 int CL_DateTime::get_month_day_local() const
00108 {
00109         load_local();
00110         return tm_local->tm_mday;
00111 }
00112 
00113 int CL_DateTime::get_month_day_utc() const
00114 {
00115         load_utc();
00116         return tm_utc->tm_mday;
00117 }
00118 
00119 int CL_DateTime::get_month_local() const
00120 {
00121         load_local();
00122         return tm_local->tm_mon;
00123 }
00124         
00125 int CL_DateTime::get_month_utc() const
00126 {
00127         load_utc();
00128         return tm_utc->tm_mon;
00129 }
00130         
00131 int CL_DateTime::get_year_local() const
00132 {
00133         load_local();
00134         return tm_local->tm_year + 1900;
00135 }
00136         
00137 int CL_DateTime::get_year_utc() const
00138 {
00139         load_utc();
00140         return tm_utc->tm_year + 1900;
00141 }
00142 
00143 int CL_DateTime::get_week_day_local() const
00144 {
00145         load_local();
00146         return tm_local->tm_wday;
00147 }
00148 
00149 int CL_DateTime::get_week_day_utc() const
00150 {
00151         load_utc();
00152         return tm_utc->tm_wday;
00153 }
00154 
00155 int CL_DateTime::get_year_day_local() const
00156 {
00157         load_local();
00158         return tm_local->tm_yday;
00159 }
00160 
00161 int CL_DateTime::get_year_day_utc() const
00162 {
00163         load_utc();
00164         return tm_utc->tm_yday;
00165 }
00166 
00167 int CL_DateTime::get_daylight_saving_time_local() const
00168 {
00169         load_local();
00170         return tm_local->tm_isdst;
00171 }
00172 
00173 CL_String CL_DateTime::get_zone_abbreviation() const
00174 {
00175         load_local();
00176 #ifdef WIN32
00177         return CL_String();
00178 #else
00179         return CL_StringHelp::local8_to_text(tm_local->tm_zone);
00180 #endif
00181 }
00182 
00184 // CL_DateTime Operations:
00185 
00186 void CL_DateTime::set_unix_time(time_t t)
00187 {
00188         unix_time = t;
00189 }
00190 
00191 void CL_DateTime::set_seconds_local(int seconds)
00192 {
00193         load_local();
00194         tm_local->tm_sec = seconds;
00195 }
00196         
00197 void CL_DateTime::set_minutes_local(int minutes)
00198 {
00199         load_local();
00200         tm_local->tm_min = minutes;
00201 }
00202         
00203 void CL_DateTime::set_hours_local(int hours)
00204 {
00205         load_local();
00206         tm_local->tm_hour = hours;
00207 }
00208         
00209 void CL_DateTime::set_month_day_local(int day)
00210 {
00211         load_local();
00212         tm_local->tm_mday = day;
00213 }
00214 
00215 void CL_DateTime::set_month_local(int month)
00216 {
00217         load_local();
00218         tm_local->tm_mon = month;
00219 }
00220         
00221 void CL_DateTime::set_year_local(int year)
00222 {
00223         load_local();
00224         tm_local->tm_year = year-1900;
00225 }
00226 
00227 void CL_DateTime::save_local()
00228 {
00229         delete tm_utc;
00230         tm_utc = 0;
00231         
00232         if (tm_local == 0)
00233                 return;
00234 
00235         time_t result = mktime(tm_local);
00236         if (result == (time_t) -1)
00237         {
00238                 delete tm_local;
00239                 tm_local = 0;
00240                 throw CL_Exception(TEXT("mktime failed"));
00241         }
00242         unix_time = result;
00243 }
00244         
00245 CL_DateTime &CL_DateTime::operator =(const CL_DateTime &other)
00246 {
00247         delete tm_local;
00248         tm_local = 0;
00249         delete tm_utc;
00250         tm_utc = 0;
00251         unix_time = other.unix_time;
00252         return *this;
00253 }
00254 
00255 bool CL_DateTime::operator <(const CL_DateTime &other) const
00256 {
00257         return unix_time < other.unix_time;
00258 }
00259 
00260 bool CL_DateTime::operator <=(const CL_DateTime &other) const
00261 {
00262         return unix_time <= other.unix_time;
00263 }
00264         
00265 bool CL_DateTime::operator >(const CL_DateTime &other) const
00266 {
00267         return unix_time > other.unix_time;
00268 }
00269 
00270 bool CL_DateTime::operator >=(const CL_DateTime &other) const
00271 {
00272         return unix_time >= other.unix_time;
00273 }
00274         
00275 bool CL_DateTime::operator ==(const CL_DateTime &other) const
00276 {
00277         return unix_time == other.unix_time;
00278 }
00279 
00280 bool CL_DateTime::operator !=(const CL_DateTime &other) const
00281 {
00282         return unix_time != other.unix_time;
00283 }
00284 
00286 // CL_DateTime Implementation:
00287 
00288 #ifdef WIN32
00289 CL_Mutex CL_DateTime::mutex;
00290 #endif
00291 
00292 void CL_DateTime::load_local() const
00293 {
00294         if (tm_local)
00295                 return;
00296 
00297 #ifdef WIN32
00298         CL_MutexSection mutexLock(&mutex);
00299         tm_local = new tm;
00300         memset(tm_local, 0, sizeof(tm));
00301         tm *result = localtime(&unix_time);
00302         if (result == 0)
00303         {
00304                 delete tm_local;
00305                 tm_local = 0;
00306                 throw CL_Exception(TEXT("localtime_r failed"));
00307         }
00308         else
00309         {
00310                 memcpy(tm_local, result, sizeof(tm));
00311         }
00312 #else
00313         tm_local = new tm;
00314         memset(tm_local, 0, sizeof(tm));
00315         tm *result = localtime_r(&unix_time, tm_local);
00316         if (result == 0)
00317         {
00318                 delete tm_local;
00319                 tm_local = 0;
00320                 throw CL_Exception(TEXT("localtime_r failed"));
00321         }
00322 #endif
00323 }
00324 
00325 void CL_DateTime::load_utc() const
00326 {
00327         if (tm_utc)
00328                 return;
00329 
00330 #ifdef WIN32
00331         CL_MutexSection mutexLock(&mutex);
00332         tm_utc = new tm;
00333         memset(tm_utc, 0, sizeof(tm));
00334         tm *result = gmtime(&unix_time);
00335         if (result == 0)
00336         {
00337                 delete tm_utc;
00338                 tm_utc = 0;
00339                 throw CL_Exception(TEXT("gmtime_r failed"));
00340         }
00341         else
00342         {
00343                 memcpy(tm_utc, result, sizeof(tm));
00344         }
00345 #else
00346         tm_utc = new tm;
00347         memset(tm_utc, 0, sizeof(tm));
00348         tm *result = gmtime_r(&unix_time, tm_utc);
00349         if (result == 0)
00350         {
00351                 delete tm_utc;
00352                 tm_utc = 0;
00353                 throw CL_Exception(TEXT("gmtime_r failed"));
00354         }
00355 #endif
00356 }
00357 

Generated on Sat Feb 19 22:51:15 2005 for npcore by  doxygen 1.4.1