root / CCV-HAND / libs / poco / include / Poco / LocalDateTime.h @ 59
View | Annotate | Download (11.7 KB)
| 1 | //
|
|---|---|
| 2 | // LocalDateTime.h
|
| 3 | //
|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/LocalDateTime.h#3 $
|
| 5 | //
|
| 6 | // Library: Foundation
|
| 7 | // Package: DateTime
|
| 8 | // Module: LocalDateTime
|
| 9 | //
|
| 10 | // Definition of the LocalDateTime 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_LocalDateTime_INCLUDED
|
| 40 | #define Foundation_LocalDateTime_INCLUDED
|
| 41 | |
| 42 | |
| 43 | #include "Poco/Foundation.h" |
| 44 | #include "Poco/DateTime.h" |
| 45 | |
| 46 | |
| 47 | namespace Poco {
|
| 48 | |
| 49 | |
| 50 | class Foundation_API LocalDateTime |
| 51 | /// This class represents an instant in local time
|
| 52 | /// (as opposed to UTC), expressed in years, months, days,
|
| 53 | /// hours, minutes, seconds and milliseconds based on the
|
| 54 | /// Gregorian calendar.
|
| 55 | ///
|
| 56 | /// In addition to the date and time, the class also
|
| 57 | /// maintains a time zone differential, which denotes
|
| 58 | /// the difference in seconds from UTC to local time,
|
| 59 | /// i.e. UTC = local time - time zone differential.
|
| 60 | ///
|
| 61 | /// Although LocalDateTime supports relational and arithmetic
|
| 62 | /// operators, all date/time comparisons and date/time arithmetics
|
| 63 | /// should be done in UTC, using the DateTime or Timestamp
|
| 64 | /// class for better performance. The relational operators
|
| 65 | /// normalize the dates/times involved to UTC before carrying out
|
| 66 | /// the comparison.
|
| 67 | ///
|
| 68 | /// The time zone differential is based on the input date and
|
| 69 | /// time and current time zone. A number of constructors accept
|
| 70 | /// an explicit time zone differential parameter. These should
|
| 71 | /// not be used since daylight savings time processing is impossible
|
| 72 | /// since the time zone is unknown. Each of the constructors
|
| 73 | /// accepting a tzd parameter have been marked as deprecated and
|
| 74 | /// may be removed in a future revision.
|
| 75 | {
|
| 76 | public:
|
| 77 | LocalDateTime(); |
| 78 | /// Creates a LocalDateTime with the current date/time
|
| 79 | /// for the current time zone.
|
| 80 | |
| 81 | LocalDateTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0); |
| 82 | /// Creates a DateTime for the given Gregorian local date and time.
|
| 83 | /// * year is from 0 to 9999.
|
| 84 | /// * month is from 1 to 12.
|
| 85 | /// * day is from 1 to 31.
|
| 86 | /// * hour is from 0 to 23.
|
| 87 | /// * minute is from 0 to 59.
|
| 88 | /// * second is from 0 to 59.
|
| 89 | /// * millisecond is from 0 to 999.
|
| 90 | /// * microsecond is from 0 to 999.
|
| 91 | |
| 92 | //@ deprecated
|
| 93 | LocalDateTime(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond); |
| 94 | /// Creates a DateTime for the given Gregorian date and time in the
|
| 95 | /// time zone denoted by the time zone differential in tzd.
|
| 96 | /// * tzd is in seconds.
|
| 97 | /// * year is from 0 to 9999.
|
| 98 | /// * month is from 1 to 12.
|
| 99 | /// * day is from 1 to 31.
|
| 100 | /// * hour is from 0 to 23.
|
| 101 | /// * minute is from 0 to 59.
|
| 102 | /// * second is from 0 to 59.
|
| 103 | /// * millisecond is from 0 to 999.
|
| 104 | /// * microsecond is from 0 to 999.
|
| 105 | |
| 106 | LocalDateTime(const DateTime& dateTime);
|
| 107 | /// Creates a LocalDateTime from the UTC time given in dateTime,
|
| 108 | /// using the time zone differential of the current time zone.
|
| 109 | |
| 110 | //@ deprecated
|
| 111 | LocalDateTime(int tzd, const DateTime& dateTime); |
| 112 | /// Creates a LocalDateTime from the UTC time given in dateTime,
|
| 113 | /// using the given time zone differential. Adjusts dateTime
|
| 114 | /// for the given time zone differential.
|
| 115 | |
| 116 | //@ deprecated
|
| 117 | LocalDateTime(int tzd, const DateTime& dateTime, bool adjust); |
| 118 | /// Creates a LocalDateTime from the UTC time given in dateTime,
|
| 119 | /// using the given time zone differential. If adjust is true,
|
| 120 | /// adjusts dateTime for the given time zone differential.
|
| 121 | |
| 122 | LocalDateTime(double julianDay);
|
| 123 | /// Creates a LocalDateTime for the given Julian day in the local time zone.
|
| 124 | |
| 125 | //@ deprecated
|
| 126 | LocalDateTime(int tzd, double julianDay); |
| 127 | /// Creates a LocalDateTime for the given Julian day in the time zone
|
| 128 | /// denoted by the time zone differential in tzd.
|
| 129 | |
| 130 | LocalDateTime(const LocalDateTime& dateTime);
|
| 131 | /// Copy constructor. Creates the LocalDateTime from another one.
|
| 132 | |
| 133 | ~LocalDateTime(); |
| 134 | /// Destroys the LocalDateTime.
|
| 135 | |
| 136 | LocalDateTime& operator = (const LocalDateTime& dateTime);
|
| 137 | /// Assigns another LocalDateTime.
|
| 138 | |
| 139 | LocalDateTime& operator = (const Timestamp& timestamp);
|
| 140 | /// Assigns a timestamp
|
| 141 | |
| 142 | LocalDateTime& operator = (double julianDay);
|
| 143 | /// Assigns a Julian day in the local time zone.
|
| 144 | |
| 145 | LocalDateTime& assign(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microseconds = 0); |
| 146 | /// Assigns a Gregorian local date and time.
|
| 147 | /// * year is from 0 to 9999.
|
| 148 | /// * month is from 1 to 12.
|
| 149 | /// * day is from 1 to 31.
|
| 150 | /// * hour is from 0 to 23.
|
| 151 | /// * minute is from 0 to 59.
|
| 152 | /// * second is from 0 to 59.
|
| 153 | /// * millisecond is from 0 to 999.
|
| 154 | /// * microsecond is from 0 to 999.
|
| 155 | |
| 156 | //@ deprecated
|
| 157 | LocalDateTime& assign(int tzd, int year, int month, int day, int hour, int minute, int second, int millisecond, int microseconds); |
| 158 | /// Assigns a Gregorian local date and time in the time zone denoted by
|
| 159 | /// the time zone differential in tzd.
|
| 160 | /// * tzd is in seconds.
|
| 161 | /// * year is from 0 to 9999.
|
| 162 | /// * month is from 1 to 12.
|
| 163 | /// * day is from 1 to 31.
|
| 164 | /// * hour is from 0 to 23.
|
| 165 | /// * minute is from 0 to 59.
|
| 166 | /// * second is from 0 to 59.
|
| 167 | /// * millisecond is from 0 to 999.
|
| 168 | /// * microsecond is from 0 to 999.
|
| 169 | |
| 170 | //@ deprecated
|
| 171 | LocalDateTime& assign(int tzd, double julianDay); |
| 172 | /// Assigns a Julian day in the time zone denoted by the
|
| 173 | /// time zone differential in tzd.
|
| 174 | |
| 175 | void swap(LocalDateTime& dateTime);
|
| 176 | /// Swaps the LocalDateTime with another one.
|
| 177 | |
| 178 | int year() const; |
| 179 | /// Returns the year.
|
| 180 | |
| 181 | int month() const; |
| 182 | /// Returns the month (1 to 12).
|
| 183 | |
| 184 | int week(int firstDayOfWeek = DateTime::MONDAY) const; |
| 185 | /// Returns the week number within the year.
|
| 186 | /// FirstDayOfWeek should be either SUNDAY (0) or MONDAY (1).
|
| 187 | /// The returned week number will be from 0 to 53. Week number 1 is the week
|
| 188 | /// containing January 4. This is in accordance to ISO 8601.
|
| 189 | ///
|
| 190 | /// The following example assumes that firstDayOfWeek is MONDAY. For 2005, which started
|
| 191 | /// on a Saturday, week 1 will be the week starting on Monday, January 3.
|
| 192 | /// January 1 and 2 will fall within week 0 (or the last week of the previous year).
|
| 193 | ///
|
| 194 | /// For 2007, which starts on a Monday, week 1 will be the week startung on Monday, January 1.
|
| 195 | /// There will be no week 0 in 2007.
|
| 196 | |
| 197 | int day() const; |
| 198 | /// Returns the day witin the month (1 to 31).
|
| 199 | |
| 200 | int dayOfWeek() const; |
| 201 | /// Returns the weekday (0 to 6, where
|
| 202 | /// 0 = Sunday, 1 = Monday, ..., 6 = Saturday).
|
| 203 | |
| 204 | int dayOfYear() const; |
| 205 | /// Returns the number of the day in the year.
|
| 206 | /// January 1 is 1, February 1 is 32, etc.
|
| 207 | |
| 208 | int hour() const; |
| 209 | /// Returns the hour (0 to 23).
|
| 210 | |
| 211 | int hourAMPM() const; |
| 212 | /// Returns the hour (0 to 12).
|
| 213 | |
| 214 | bool isAM() const; |
| 215 | /// Returns true if hour < 12;
|
| 216 | |
| 217 | bool isPM() const; |
| 218 | /// Returns true if hour >= 12.
|
| 219 | |
| 220 | int minute() const; |
| 221 | /// Returns the minute (0 to 59).
|
| 222 | |
| 223 | int second() const; |
| 224 | /// Returns the second (0 to 59).
|
| 225 | |
| 226 | int millisecond() const; |
| 227 | /// Returns the millisecond (0 to 999)
|
| 228 | |
| 229 | int microsecond() const; |
| 230 | /// Returns the microsecond (0 to 999)
|
| 231 | |
| 232 | double julianDay() const; |
| 233 | /// Returns the julian day for the date.
|
| 234 | |
| 235 | int tzd() const; |
| 236 | /// Returns the time zone differential.
|
| 237 | |
| 238 | DateTime utc() const;
|
| 239 | /// Returns the UTC equivalent for the local date and time.
|
| 240 | |
| 241 | Timestamp timestamp() const;
|
| 242 | /// Returns the date and time expressed as a Timestamp.
|
| 243 | |
| 244 | Timestamp::UtcTimeVal utcTime() const;
|
| 245 | /// Returns the UTC equivalent for the local date and time.
|
| 246 | |
| 247 | bool operator == (const LocalDateTime& dateTime) const; |
| 248 | bool operator != (const LocalDateTime& dateTime) const; |
| 249 | bool operator < (const LocalDateTime& dateTime) const; |
| 250 | bool operator <= (const LocalDateTime& dateTime) const; |
| 251 | bool operator > (const LocalDateTime& dateTime) const; |
| 252 | bool operator >= (const LocalDateTime& dateTime) const; |
| 253 | |
| 254 | LocalDateTime operator + (const Timespan& span) const; |
| 255 | LocalDateTime operator - (const Timespan& span) const; |
| 256 | Timespan operator - (const LocalDateTime& dateTime) const; |
| 257 | LocalDateTime& operator += (const Timespan& span);
|
| 258 | LocalDateTime& operator -= (const Timespan& span);
|
| 259 | |
| 260 | protected:
|
| 261 | LocalDateTime(Timestamp::UtcTimeVal utcTime, Timestamp::TimeDiff diff, int tzd);
|
| 262 | |
| 263 | void determineTzd(bool adjust = false); |
| 264 | /// Recalculate the tzd based on the _dateTime member based
|
| 265 | /// on the current timezone using the Standard C runtime functions.
|
| 266 | /// If adjust is true, then adjustForTzd() is called after the
|
| 267 | /// differential is calculated.
|
| 268 | |
| 269 | void adjustForTzd();
|
| 270 | /// Adjust the _dateTime member based on the _tzd member.
|
| 271 | |
| 272 | private:
|
| 273 | DateTime _dateTime; |
| 274 | int _tzd;
|
| 275 | |
| 276 | friend class DateTimeFormatter; |
| 277 | friend class DateTimeParser; |
| 278 | }; |
| 279 | |
| 280 | |
| 281 | //
|
| 282 | // inlines
|
| 283 | //
|
| 284 | inline int LocalDateTime::year() const |
| 285 | {
|
| 286 | return _dateTime.year();
|
| 287 | } |
| 288 | |
| 289 | |
| 290 | inline int LocalDateTime::month() const |
| 291 | {
|
| 292 | return _dateTime.month();
|
| 293 | } |
| 294 | |
| 295 | |
| 296 | inline int LocalDateTime::week(int firstDayOfWeek) const |
| 297 | {
|
| 298 | return _dateTime.week(firstDayOfWeek);
|
| 299 | } |
| 300 | |
| 301 | |
| 302 | inline int LocalDateTime::day() const |
| 303 | {
|
| 304 | return _dateTime.day();
|
| 305 | } |
| 306 | |
| 307 | |
| 308 | inline int LocalDateTime::dayOfWeek() const |
| 309 | {
|
| 310 | return _dateTime.dayOfWeek();
|
| 311 | } |
| 312 | |
| 313 | |
| 314 | inline int LocalDateTime::dayOfYear() const |
| 315 | {
|
| 316 | return _dateTime.dayOfYear();
|
| 317 | } |
| 318 | |
| 319 | |
| 320 | inline int LocalDateTime::hour() const |
| 321 | {
|
| 322 | return _dateTime.hour();
|
| 323 | } |
| 324 | |
| 325 | |
| 326 | inline int LocalDateTime::hourAMPM() const |
| 327 | {
|
| 328 | return _dateTime.hourAMPM();
|
| 329 | } |
| 330 | |
| 331 | |
| 332 | inline bool LocalDateTime::isAM() const |
| 333 | {
|
| 334 | return _dateTime.isAM();
|
| 335 | } |
| 336 | |
| 337 | |
| 338 | inline bool LocalDateTime::isPM() const |
| 339 | {
|
| 340 | return _dateTime.isPM();
|
| 341 | } |
| 342 | |
| 343 | |
| 344 | inline int LocalDateTime::minute() const |
| 345 | {
|
| 346 | return _dateTime.minute();
|
| 347 | } |
| 348 | |
| 349 | |
| 350 | inline int LocalDateTime::second() const |
| 351 | {
|
| 352 | return _dateTime.second();
|
| 353 | } |
| 354 | |
| 355 | |
| 356 | inline int LocalDateTime::millisecond() const |
| 357 | {
|
| 358 | return _dateTime.millisecond();
|
| 359 | } |
| 360 | |
| 361 | |
| 362 | inline int LocalDateTime::microsecond() const |
| 363 | {
|
| 364 | return _dateTime.microsecond();
|
| 365 | } |
| 366 | |
| 367 | |
| 368 | inline double LocalDateTime::julianDay() const |
| 369 | {
|
| 370 | return _dateTime.julianDay();
|
| 371 | } |
| 372 | |
| 373 | |
| 374 | inline int LocalDateTime::tzd() const |
| 375 | {
|
| 376 | return _tzd;
|
| 377 | } |
| 378 | |
| 379 | inline Timestamp LocalDateTime::timestamp() const |
| 380 | {
|
| 381 | return Timestamp::fromUtcTime(_dateTime.utcTime());
|
| 382 | } |
| 383 | |
| 384 | inline Timestamp::UtcTimeVal LocalDateTime::utcTime() const |
| 385 | {
|
| 386 | return _dateTime.utcTime() - ((Timestamp::TimeDiff) _tzd)*10000000; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | inline void LocalDateTime::adjustForTzd() |
| 391 | {
|
| 392 | _dateTime += Timespan(((Timestamp::TimeDiff) _tzd)*Timespan::SECONDS); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | inline void swap(LocalDateTime& d1, LocalDateTime& d2) |
| 397 | {
|
| 398 | d1.swap(d2); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | } // namespace Poco
|
| 403 | |
| 404 | |
| 405 | #endif // Foundation_LocalDateTime_INCLUDED |
