root / branches / tbeta / ccv_ofx_0061 / Windows / libs / poco / include / Poco / DateTime.h @ 214
View | Annotate | Download (11.3 KB)
| 1 | //
|
|---|---|
| 2 | // DateTime.h
|
| 3 | //
|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/DateTime.h#2 $
|
| 5 | //
|
| 6 | // Library: Foundation
|
| 7 | // Package: DateTime
|
| 8 | // Module: DateTime
|
| 9 | //
|
| 10 | // Definition of the DateTime class.
|
| 11 | //
|
| 12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
| 13 | // and Contributors.
|
| 14 | //
|
| 15 | // Permission is hereby granted, free of charge, to any person or organization
|
| 16 | // obtaining a copy of the software and accompanying documentation covered by
|
| 17 | // this license (the "Software") to use, reproduce, display, distribute,
|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the
|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to
|
| 20 | // do so, all subject to the following:
|
| 21 | //
|
| 22 | // The copyright notices in the Software and this entire statement, including
|
| 23 | // the above license grant, this restriction and the following disclaimer,
|
| 24 | // must be included in all copies of the Software, in whole or in part, and
|
| 25 | // all derivative works of the Software, unless such copies or derivative
|
| 26 | // works are solely in the form of machine-executable object code generated by
|
| 27 | // a source language processor.
|
| 28 | //
|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 35 | // DEALINGS IN THE SOFTWARE.
|
| 36 | //
|
| 37 | |
| 38 | |
| 39 | #ifndef Foundation_DateTime_INCLUDED
|
| 40 | #define Foundation_DateTime_INCLUDED
|
| 41 | |
| 42 | |
| 43 | #include "Poco/Foundation.h" |
| 44 | #include "Poco/Timestamp.h" |
| 45 | #include "Poco/Timespan.h" |
| 46 | |
| 47 | |
| 48 | namespace Poco {
|
| 49 | |
| 50 | |
| 51 | class Foundation_API DateTime |
| 52 | /// This class represents an instant in time, expressed
|
| 53 | /// in years, months, days, hours, minutes, seconds
|
| 54 | /// and milliseconds based on the Gregorian calendar.
|
| 55 | /// The class is mainly useful for conversions between
|
| 56 | /// UTC, Julian day and Gregorian calendar dates.
|
| 57 | ///
|
| 58 | /// Conversion calculations are based on algorithms
|
| 59 | /// collected and described by Peter Baum at
|
| 60 | /// http://vsg.cape.com/~pbaum/date/date0.htm
|
| 61 | ///
|
| 62 | /// Internally, this class stores a date/time in two
|
| 63 | /// forms (UTC and broken down) for performance reasons. Only use
|
| 64 | /// this class for conversions between date/time representations.
|
| 65 | /// Use the Timestamp class for everything else.
|
| 66 | ///
|
| 67 | /// Notes:
|
| 68 | /// * Zero is a valid year (in accordance with ISO 8601 and astronomical year numbering)
|
| 69 | /// * Year zero (0) is a leap year
|
| 70 | /// * Negative years (years preceding 1 BC) are not supported
|
| 71 | ///
|
| 72 | /// For more information, please see:
|
| 73 | /// * http://en.wikipedia.org/wiki/Gregorian_Calendar
|
| 74 | /// * http://en.wikipedia.org/wiki/Julian_day
|
| 75 | /// * http://en.wikipedia.org/wiki/UTC
|
| 76 | /// * http://en.wikipedia.org/wiki/ISO_8601
|
| 77 | {
|
| 78 | public:
|
| 79 | enum Months
|
| 80 | /// Symbolic names for month numbers (1 to 12).
|
| 81 | {
|
| 82 | JANUARY = 1,
|
| 83 | FEBRUARY, |
| 84 | MARCH, |
| 85 | APRIL, |
| 86 | MAY, |
| 87 | JUNE, |
| 88 | JULY, |
| 89 | AUGUST, |
| 90 | SEPTEMBER, |
| 91 | OCTOBER, |
| 92 | NOVEMBER, |
| 93 | DECEMBER |
| 94 | }; |
| 95 | |
| 96 | enum DaysOfWeek
|
| 97 | /// Symbolic names for week day numbers (0 to 6).
|
| 98 | {
|
| 99 | SUNDAY = 0,
|
| 100 | MONDAY, |
| 101 | TUESDAY, |
| 102 | WEDNESDAY, |
| 103 | THURSDAY, |
| 104 | FRIDAY, |
| 105 | SATURDAY |
| 106 | }; |
| 107 | |
| 108 | DateTime(); |
| 109 | /// Creates a DateTime for the current date and time.
|
| 110 | |
| 111 | DateTime(const Timestamp& timestamp);
|
| 112 | /// Creates a DateTime for the date and time given in
|
| 113 | /// a Timestamp.
|
| 114 | |
| 115 | DateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0); |
| 116 | /// Creates a DateTime for the given Gregorian date and time.
|
| 117 | /// * year is from 0 to 9999.
|
| 118 | /// * month is from 1 to 12.
|
| 119 | /// * day is from 1 to 31.
|
| 120 | /// * hour is from 0 to 23.
|
| 121 | /// * minute is from 0 to 59.
|
| 122 | /// * second is from 0 to 59.
|
| 123 | /// * millisecond is from 0 to 999.
|
| 124 | /// * microsecond is from 0 to 999.
|
| 125 | |
| 126 | DateTime(double julianDay);
|
| 127 | /// Creates a DateTime for the given Julian day.
|
| 128 | |
| 129 | DateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff); |
| 130 | /// Creates a DateTime from an UtcTimeVal and a TimeDiff.
|
| 131 | ///
|
| 132 | /// Mainly used internally by DateTime and friends.
|
| 133 | |
| 134 | DateTime(const DateTime& dateTime);
|
| 135 | /// Copy constructor. Creates the DateTime from another one.
|
| 136 | |
| 137 | ~DateTime(); |
| 138 | /// Destroys the DateTime.
|
| 139 | |
| 140 | DateTime& operator = (const DateTime& dateTime);
|
| 141 | /// Assigns another DateTime.
|
| 142 | |
| 143 | DateTime& operator = (const Timestamp& timestamp);
|
| 144 | /// Assigns a Timestamp.
|
| 145 | |
| 146 | DateTime& operator = (double julianDay);
|
| 147 | /// Assigns a Julian day.
|
| 148 | |
| 149 | DateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0); |
| 150 | /// Assigns a Gregorian date and time.
|
| 151 | /// * year is from 0 to 9999.
|
| 152 | /// * month is from 1 to 12.
|
| 153 | /// * day is from 1 to 31.
|
| 154 | /// * hour is from 0 to 23.
|
| 155 | /// * minute is from 0 to 59.
|
| 156 | /// * second is from 0 to 59.
|
| 157 | /// * millisecond is from 0 to 999.
|
| 158 | /// * microsecond is from 0 to 999.
|
| 159 | |
| 160 | void swap(DateTime& dateTime);
|
| 161 | /// Swaps the DateTime with another one.
|
| 162 | |
| 163 | int year() const; |
| 164 | /// Returns the year.
|
| 165 | |
| 166 | int month() const; |
| 167 | /// Returns the month (1 to 12).
|
| 168 | |
| 169 | int week(int firstDayOfWeek = MONDAY) const; |
| 170 | /// Returns the week number within the year.
|
| 171 | /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
|
| 172 | /// The returned week number will be from 0 to 53. Week number 1 is the week
|
| 173 | /// containing January 4. This is in accordance to ISO 8601.
|
| 174 | ///
|
| 175 | /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
|
| 176 | /// on a Saturday, week 1 will be the week starting on Monday, January 3.
|
| 177 | /// January 1 and 2 will fall within week 0 (or the last week of the previous year).
|
| 178 | ///
|
| 179 | /// For 2007, which starts on a Monday, week 1 will be the week startung on Monday, January 1.
|
| 180 | /// There will be no week 0 in 2007.
|
| 181 | |
| 182 | int day() const; |
| 183 | /// Returns the day witin the month (1 to 31).
|
| 184 | |
| 185 | int dayOfWeek() const; |
| 186 | /// Returns the weekday (0 to 6, where
|
| 187 | /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
|
| 188 | |
| 189 | int dayOfYear() const; |
| 190 | /// Returns the number of the day in the year.
|
| 191 | /// January 1 is 1, February 1 is 32, etc.
|
| 192 | |
| 193 | int hour() const; |
| 194 | /// Returns the hour (0 to 23).
|
| 195 | |
| 196 | int hourAMPM() const; |
| 197 | /// Returns the hour (0 to 12).
|
| 198 | |
| 199 | bool isAM() const; |
| 200 | /// Returns true if hour < 12;
|
| 201 | |
| 202 | bool isPM() const; |
| 203 | /// Returns true if hour >= 12.
|
| 204 | |
| 205 | int minute() const; |
| 206 | /// Returns the minute (0 to 59).
|
| 207 | |
| 208 | int second() const; |
| 209 | /// Returns the second (0 to 59).
|
| 210 | |
| 211 | int millisecond() const; |
| 212 | /// Returns the millisecond (0 to 999)
|
| 213 | |
| 214 | int microsecond() const; |
| 215 | /// Returns the microsecond (0 to 999)
|
| 216 | |
| 217 | double julianDay() const; |
| 218 | /// Returns the julian day for the date and time.
|
| 219 | |
| 220 | Timestamp timestamp() const;
|
| 221 | /// Returns the date and time expressed as a Timestamp.
|
| 222 | |
| 223 | Timestamp::UtcTimeVal utcTime() const;
|
| 224 | /// Returns the date and time expressed in UTC-based
|
| 225 | /// time. UTC base time is midnight, October 15, 1582.
|
| 226 | /// Resolution is 100 nanoseconds.
|
| 227 | |
| 228 | bool operator == (const DateTime& dateTime) const; |
| 229 | bool operator != (const DateTime& dateTime) const; |
| 230 | bool operator < (const DateTime& dateTime) const; |
| 231 | bool operator <= (const DateTime& dateTime) const; |
| 232 | bool operator > (const DateTime& dateTime) const; |
| 233 | bool operator >= (const DateTime& dateTime) const; |
| 234 | |
| 235 | DateTime operator + (const Timespan& span) const; |
| 236 | DateTime operator - (const Timespan& span) const; |
| 237 | Timespan operator - (const DateTime& dateTime) const; |
| 238 | DateTime& operator += (const Timespan& span);
|
| 239 | DateTime& operator -= (const Timespan& span);
|
| 240 | |
| 241 | void makeUTC(int tzd); |
| 242 | /// Converts a local time into UTC, by applying the given time zone differential.
|
| 243 | |
| 244 | void makeLocal(int tzd); |
| 245 | /// Converts a UTC time into a local time, by applying the given time zone differential.
|
| 246 | |
| 247 | static bool isLeapYear(int year); |
| 248 | /// Returns true if the given year is a leap year;
|
| 249 | /// false otherwise.
|
| 250 | |
| 251 | static int daysOfMonth(int year, int month); |
| 252 | /// Returns the number of days in the given month
|
| 253 | /// and year. Month is from 1 to 12.
|
| 254 | |
| 255 | static bool isValid(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0); |
| 256 | /// Checks if the given date and time is valid
|
| 257 | /// (all arguments are within a proper range).
|
| 258 | ///
|
| 259 | /// Returns true if all arguments are valid, false otherwise.
|
| 260 | |
| 261 | protected:
|
| 262 | static double toJulianDay(Timestamp::UtcTimeVal utcTime); |
| 263 | /// Computes the Julian day for an UTC time.
|
| 264 | |
| 265 | static double toJulianDay(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0); |
| 266 | /// Computes the Julian day for a gregorian calendar date and time.
|
| 267 | /// See <http://vsg.cape.com/~pbaum/date/jdimp.htm>, section 2.3.1 for the algorithm.
|
| 268 | |
| 269 | static Timestamp::UtcTimeVal toUtcTime(double julianDay); |
| 270 | /// Computes the UTC time for a Julian day.
|
| 271 | |
| 272 | void computeGregorian(double julianDay); |
| 273 | /// Computes the Gregorian date for the given Julian day.
|
| 274 | /// See <http://vsg.cape.com/~pbaum/date/injdimp.htm>, section 3.3.1 for the algorithm.
|
| 275 | |
| 276 | void computeDaytime();
|
| 277 | /// Extracts the daytime (hours, minutes, seconds, etc.) from the stored utcTime.
|
| 278 | |
| 279 | private:
|
| 280 | void checkLimit(short& lower, short& higher, short limit); |
| 281 | void normalize();
|
| 282 | ///utility functions used to correct the overflow in computeGregorian
|
| 283 | |
| 284 | Timestamp::UtcTimeVal _utcTime; |
| 285 | short _year;
|
| 286 | short _month;
|
| 287 | short _day;
|
| 288 | short _hour;
|
| 289 | short _minute;
|
| 290 | short _second;
|
| 291 | short _millisecond;
|
| 292 | short _microsecond;
|
| 293 | }; |
| 294 | |
| 295 | |
| 296 | //
|
| 297 | // inlines
|
| 298 | //
|
| 299 | inline Timestamp DateTime::timestamp() const |
| 300 | {
|
| 301 | return Timestamp::fromUtcTime(_utcTime);
|
| 302 | } |
| 303 | |
| 304 | |
| 305 | inline Timestamp::UtcTimeVal DateTime::utcTime() const |
| 306 | {
|
| 307 | return _utcTime;
|
| 308 | } |
| 309 | |
| 310 | |
| 311 | inline int DateTime::year() const |
| 312 | {
|
| 313 | return _year;
|
| 314 | } |
| 315 | |
| 316 | |
| 317 | inline int DateTime::month() const |
| 318 | {
|
| 319 | return _month;
|
| 320 | } |
| 321 | |
| 322 | |
| 323 | inline int DateTime::day() const |
| 324 | {
|
| 325 | return _day;
|
| 326 | } |
| 327 | |
| 328 | |
| 329 | inline int DateTime::hour() const |
| 330 | {
|
| 331 | return _hour;
|
| 332 | } |
| 333 | |
| 334 | |
| 335 | inline int DateTime::hourAMPM() const |
| 336 | {
|
| 337 | if (_hour < 1) |
| 338 | return 12; |
| 339 | else if (_hour > 12) |
| 340 | return _hour - 12; |
| 341 | else
|
| 342 | return _hour;
|
| 343 | } |
| 344 | |
| 345 | |
| 346 | inline bool DateTime::isAM() const |
| 347 | {
|
| 348 | return _hour < 12; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | inline bool DateTime::isPM() const |
| 353 | {
|
| 354 | return _hour >= 12; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | inline int DateTime::minute() const |
| 359 | {
|
| 360 | return _minute;
|
| 361 | } |
| 362 | |
| 363 | |
| 364 | inline int DateTime::second() const |
| 365 | {
|
| 366 | return _second;
|
| 367 | } |
| 368 | |
| 369 | |
| 370 | inline int DateTime::millisecond() const |
| 371 | {
|
| 372 | return _millisecond;
|
| 373 | } |
| 374 | |
| 375 | |
| 376 | inline int DateTime::microsecond() const |
| 377 | {
|
| 378 | return _microsecond;
|
| 379 | } |
| 380 | |
| 381 | |
| 382 | inline bool DateTime::operator == (const DateTime& dateTime) const |
| 383 | {
|
| 384 | return _utcTime == dateTime._utcTime;
|
| 385 | } |
| 386 | |
| 387 | |
| 388 | inline bool DateTime::operator != (const DateTime& dateTime) const |
| 389 | {
|
| 390 | return _utcTime != dateTime._utcTime;
|
| 391 | } |
| 392 | |
| 393 | |
| 394 | inline bool DateTime::operator < (const DateTime& dateTime) const |
| 395 | {
|
| 396 | return _utcTime < dateTime._utcTime;
|
| 397 | } |
| 398 | |
| 399 | |
| 400 | inline bool DateTime::operator <= (const DateTime& dateTime) const |
| 401 | {
|
| 402 | return _utcTime <= dateTime._utcTime;
|
| 403 | } |
| 404 | |
| 405 | |
| 406 | inline bool DateTime::operator > (const DateTime& dateTime) const |
| 407 | {
|
| 408 | return _utcTime > dateTime._utcTime;
|
| 409 | } |
| 410 | |
| 411 | |
| 412 | inline bool DateTime::operator >= (const DateTime& dateTime) const |
| 413 | {
|
| 414 | return _utcTime >= dateTime._utcTime;
|
| 415 | } |
| 416 | |
| 417 | |
| 418 | inline bool DateTime::isLeapYear(int year) |
| 419 | {
|
| 420 | return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | inline void swap(DateTime& d1, DateTime& d2) |
| 425 | {
|
| 426 | d1.swap(d2); |
| 427 | } |
| 428 | |
| 429 | |
| 430 | } // namespace Poco
|
| 431 | |
| 432 | |
| 433 | #endif // Foundation_DateTime_INCLUDED |
