00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanTimestamp.h"
00028
00029
00030 #include "CeylanSystem.h"
00031 #include "CeylanStringUtils.h"
00032 #include "CeylanOperators.h"
00033
00034
00035 #include <ctime>
00036
00037 #include <list>
00038 using std::list ;
00039
00040 using std::string ;
00041
00042 using namespace Ceylan ;
00043 using namespace Ceylan::System ;
00044
00045
00046
00047 #ifdef CEYLAN_USES_CONFIG_H
00048 #include "CeylanConfig.h"
00049 #endif // CEYLAN_USES_CONFIG_H
00050
00051
00052 #if CEYLAN_ARCH_NINTENDO_DS
00053 #include "CeylanConfigForNintendoDS.h"
00054 #endif // CEYLAN_ARCH_NINTENDO_DS
00055
00056
00057
00058
00059 Timestamp::Timestamp()
00060 {
00061
00062 #if CEYLAN_ARCH_NINTENDO_DS
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 #ifdef CEYLAN_RUNS_ON_ARM7
00074
00075
00076 _year = IPC->time.rtc.year + 2000 ;
00077 _month = IPC->time.rtc.month ;
00078 _day = IPC->time.rtc.day ;
00079
00080
00081 _hour = ( IPC->time.rtc.hours < 12 ) ?
00082 IPC->time.rtc.hours: IPC->time.rtc.hours - 40 ;
00083
00084 _minute = IPC->time.rtc.minutes ;
00085 _second = IPC->time.rtc.seconds ;
00086
00087
00088 #elif defined(CEYLAN_RUNS_ON_ARM9)
00089
00090
00091
00092
00093
00094
00095 _year = 0 ;
00096 _month = 0 ;
00097 _day = 0 ;
00098 _hour = 0 ;
00099 _minute = 0 ;
00100 _second = 0 ;
00101
00102 #endif // CEYLAN_RUNS_ON_ARM7
00103
00104
00105 #else // CEYLAN_ARCH_NINTENDO_DS
00106
00107
00108 #if CEYLAN_USES_LOCALTIME
00109
00110 time_t currentMeasuredTime = System::getTime() ;
00111
00112 #if CEYLAN_ARCH_WINDOWS
00113
00114 struct tm currentTime ;
00115
00116 if ( ::localtime_s( & currentTime, & currentMeasuredTime ) != 0 )
00117 throw UtilsException(
00118 "Timestamp constructor: unable to determine local time: "
00119 + System::explainError() ) ;
00120
00121 _year = currentTime.tm_year + 1900 ;
00122 _month = currentTime.tm_mon + 1 ;
00123 _day = currentTime.tm_mday ;
00124 _hour = currentTime.tm_hour ;
00125 _minute = currentTime.tm_min ;
00126 _second = currentTime.tm_sec ;
00127
00128 #else // CEYLAN_ARCH_WINDOWS
00129
00130
00131 struct tm * currentTime =::localtime( & currentMeasuredTime ) ;
00132
00133 if ( currentTime == 0 )
00134 throw UtilsException(
00135 "Timestamp constructor: unable to determine local time: "
00136 + System::explainError() ) ;
00137
00138 _year = currentTime->tm_year + 1900 ;
00139 _month = currentTime->tm_mon + 1 ;
00140 _day = currentTime->tm_mday ;
00141 _hour = currentTime->tm_hour ;
00142 _minute = currentTime->tm_min ;
00143 _second = currentTime->tm_sec ;
00144
00145
00146 #endif // CEYLAN_ARCH_WINDOWS
00147
00148
00149
00150 #else // CEYLAN_USES_LOCALTIME
00151
00152 throw UtilsException( "Timestamp constructor: "
00153 "::localtime function not available" ) ;
00154
00155 #endif // CEYLAN_USES_LOCALTIME
00156
00157 #endif // CEYLAN_ARCH_NINTENDO_DS
00158
00159 }
00160
00161
00162
00163 Timestamp::~Timestamp() throw()
00164 {
00165
00166
00167
00168 }
00169
00170
00171
00172 const string Timestamp::toString( Ceylan::VerbosityLevels level ) const
00173 {
00174
00175 string result = '['
00176 + Ceylan::toString( _year ) + '/'
00177 + Ceylan::toNumericalString( _month ) + '/'
00178 + Ceylan::toNumericalString( _day ) + ' '
00179 + Ceylan::toNumericalString( _hour ) + ':' ;
00180
00181 if ( _minute < 10 )
00182 result += '0' ;
00183
00184 result += Ceylan::toNumericalString( _minute ) + ':' ;
00185
00186 if ( _second < 10 )
00187 result += '0' ;
00188
00189 result += Ceylan::toNumericalString( _second ) + ']' ;
00190
00191 return result ;
00192
00193 }
00194
00195
00196
00197 string Timestamp::DescribeDuration( Second duration )
00198 {
00199
00200
00201
00202
00203
00204
00205
00206 const Second aMinute = 60 ;
00207 const Second anHour = 60 * aMinute ;
00208 const Second aDay = 24 * anHour ;
00209 const Second aYear = 365 * aDay ;
00210
00211 list<string> res ;
00212
00213 if ( duration >= aYear )
00214 {
00215
00216 Ceylan::Uint32 years = duration / aYear ;
00217
00218 if ( years == 1 )
00219 res.push_back( "1 year" ) ;
00220 else
00221 res.push_back( Ceylan::toString( duration / aYear ) + " years" ) ;
00222
00223 }
00224
00225 duration = duration % aYear ;
00226
00227
00228 if ( duration >= aDay )
00229 {
00230
00231 Ceylan::Uint32 days = duration / aDay ;
00232
00233 if ( days == 1 )
00234 res.push_back( "1 day" ) ;
00235 else
00236 res.push_back( Ceylan::toString( duration / aDay ) + " days" ) ;
00237
00238 }
00239
00240 duration = duration % aDay ;
00241
00242
00243 if ( duration >= anHour )
00244 {
00245
00246 Ceylan::Uint32 hours = duration / anHour ;
00247
00248 if ( hours == 1 )
00249 res.push_back( "1 hour" ) ;
00250 else
00251 res.push_back( Ceylan::toString( duration / anHour ) + " hours" ) ;
00252
00253 }
00254
00255 duration = duration % anHour ;
00256
00257
00258 if ( duration >= aMinute )
00259 {
00260
00261 Ceylan::Uint32 minutes = duration / aMinute ;
00262
00263 if ( minutes == 1 )
00264 res.push_back( "1 minute" ) ;
00265 else
00266 res.push_back(
00267 Ceylan::toString( duration / aMinute ) + " minutes" ) ;
00268
00269 }
00270
00271 duration = duration % aMinute ;
00272
00273
00274 if ( duration >= 1 )
00275 {
00276
00277 if ( duration == 1 )
00278 res.push_back( "1 second" ) ;
00279 else
00280 res.push_back( Ceylan::toString( duration ) + " seconds" ) ;
00281
00282 }
00283 else if ( res.empty() )
00284 res.push_back( "0 second" ) ;
00285
00286 return join( res, ", " ) ;
00287
00288 }
00289
00290
00291
00292 bool Timestamp::operator < (Timestamp & second )
00293 {
00294
00295
00296
00297
00298
00299
00300 if ( _year < second._year )
00301 return true ;
00302
00303 if ( _year > second._year )
00304 return false ;
00305
00306
00307 if ( _month < second._month )
00308 return true ;
00309
00310 if ( _month > second._month )
00311 return false ;
00312
00313
00314 if ( _day < second._day )
00315 return true ;
00316
00317 if ( _day > second._day )
00318 return false ;
00319
00320
00321 if ( _hour < second._hour )
00322 return true ;
00323
00324 if ( _hour > second._hour )
00325 return false ;
00326
00327
00328 if ( _minute < second._minute )
00329 return true ;
00330
00331 if ( _minute > second._minute )
00332 return false ;
00333
00334
00335 if ( _second < second._second )
00336 return true ;
00337
00338 if ( _second > second._second )
00339 return false ;
00340
00341 return false ;
00342
00343 }
00344