blocxx
DateTime.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
38
39#ifndef BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
40#define BLOCXX_DATETIME_HPP_INCLUDE_GUARD_
41#include "blocxx/BLOCXX_config.h"
42#include "blocxx/Exception.hpp"
43#include "blocxx/Types.hpp"
44#include "blocxx/CommonFwd.hpp"
45
46extern "C"
47{
48#include <time.h>
49}
50
51namespace BLOCXX_NAMESPACE
52{
53
55
56
80class BLOCXX_COMMON_API DateTime
81{
82public:
95
100 DateTime();
178 explicit DateTime(const String& str);
189 explicit DateTime(time_t t, UInt32 microseconds=0);
205 DateTime(
206 int year,
207 int month,
208 int day,
209 int hour=0,
210 int minute=0,
211 int second=0,
212 UInt32 microsecond=0,
213 ETimeOffset timeOffset = E_LOCAL_TIME);
217 ~DateTime();
224 int getHour(ETimeOffset timeOffset = E_LOCAL_TIME) const;
231 int getMinute(ETimeOffset timeOffset = E_LOCAL_TIME) const;
239 int getSecond(ETimeOffset timeOffset = E_LOCAL_TIME) const;
247 UInt32 getMicrosecond() const;
254 int getDay(ETimeOffset timeOffset = E_LOCAL_TIME) const;
260 int getDow(ETimeOffset timeOffset = E_LOCAL_TIME) const;
265 int getMonth(ETimeOffset timeOffset = E_LOCAL_TIME) const;
270 int getYear(ETimeOffset timeOffset = E_LOCAL_TIME) const;
274 time_t get() const;
282 void setHour(int hour, ETimeOffset timeOffset = E_LOCAL_TIME);
290 void setMinute(int minute, ETimeOffset timeOffset = E_LOCAL_TIME);
298 void setSecond(int second, ETimeOffset timeOffset = E_LOCAL_TIME);
305 void setMicrosecond(UInt32 microsecond);
315 void setTime(
316 int hour,
317 int minute,
318 int second,
319 ETimeOffset timeOffset = E_LOCAL_TIME);
327 void setDay(int day, ETimeOffset timeOffset = E_LOCAL_TIME);
336 void setMonth(int month, ETimeOffset timeOffset = E_LOCAL_TIME);
345 void setYear(int year, ETimeOffset timeOffset = E_LOCAL_TIME);
355 void set(time_t t, UInt32 microseconds=0);
371 void set(
372 int year,
373 int month,
374 int day,
375 int hour,
376 int minute,
377 int second,
378 UInt32 microseconds,
379 ETimeOffset timeOffset = E_LOCAL_TIME);
383 void setToCurrent();
390 void addDays(int days);
397 void addWeeks(int weeks)
398 {
399 addDays(weeks * 7);
400 }
401
407 void addMonths(int months);
414 void addYears(int years);
421 void addSeconds(long seconds)
422 {
423 m_time += seconds;
424 }
425
429 void addMinutes(long minutes)
430 {
431 m_time += minutes * 60;
432 }
433
437 void addMicroseconds(long microseconds)
438 {
439 m_microseconds += microseconds;
440 m_time += m_microseconds / 1000000;
441 m_microseconds %= 1000000;
442 }
443
447 void addMilliseconds(long milliseconds)
448 {
449 this->addMicroseconds(milliseconds * 1000);
450 }
451
455 void addHours(long hours) { m_time += hours * 60 * 60; }
461 bool operator< ( const DateTime& tm ) const
462 {
463 if (m_time == tm.m_time)
464 {
465 return m_microseconds < tm.m_microseconds;
466 }
467 return m_time < tm.m_time;
468 }
469
475 bool operator> ( const DateTime& tm ) const
476 {
477 return tm < *this;
478 }
479
484 bool operator== ( const DateTime& tm ) const
485 {
486 return m_time == tm.m_time && m_microseconds == tm.m_microseconds;
487 }
488
493 bool operator!= ( const DateTime& tm ) const
494 {
495 return !(*this == tm);
496 }
497
503 bool operator<= ( const DateTime& tm ) const
504 {
505 return !(tm < *this);
506 }
507
513 bool operator>= ( const DateTime& tm ) const
514 {
515 return !(*this < tm);
516 }
517
522 DateTime& operator+= (long seconds)
523 {
524 addSeconds(seconds);
525 return *this;
526 }
527
532 DateTime& operator-= (long seconds)
533 {
534 addSeconds(-seconds);
535 return *this;
536 }
537
543 String toString(ETimeOffset timeOffset = E_LOCAL_TIME) const;
544
555 String toString(
556 char const * format, ETimeOffset timeOffset = E_LOCAL_TIME) const;
557
563 static char const DEFAULT_FORMAT[];
564
569 String toStringGMT() const BLOCXX_DEPRECATED; // in 3.0.0
570
571#if 0
578 static Int16 getGMTOffset();
579 // Removed due to the above problems. Use toLocal() or
580 // getGMTOffsetMinutesNow() instead.
581#endif
582
589 {
590 time_t t = time(0);
591 struct tm tt;
592 return DateTime::localTimeAndOffset(t, tt);
593 }
594
601 Int16 toLocal(struct tm & tt) const
602 {
604 }
605
609 static DateTime getCurrent();
610
611private:
612 time_t m_time;
614 tm getTm(ETimeOffset timeOffset) const;
615 void setTime(tm& tmarg, ETimeOffset timeOffset);
616 static Int16 localTimeAndOffset(time_t t, struct tm & tt);
617};
618
624BLOCXX_COMMON_API DateTime operator-(DateTime const & x, DateTime const & y);
625
626} // end namespace BLOCXX_NAMESPACE
627
628#endif
629
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named <NAME>Exception that derives from Exception This macro is typical...
The DateTime class is an abstraction for date time data.
Definition DateTime.hpp:81
ETimeOffset
The values of this enum are passed to various functions to as a flag to indicate the timezone context...
Definition DateTime.hpp:91
static char const DEFAULT_FORMAT[]
A default date/time format to use with toString().
Definition DateTime.hpp:563
void addMilliseconds(long milliseconds)
Add milliseconds to the date represented by this object.
Definition DateTime.hpp:447
void addMinutes(long minutes)
Add minutes to the date represented by this object.
Definition DateTime.hpp:429
DateTime()
Create a new DateTime object that represents the Epoch (00:00:00 UTC, January 1, 1970)
Definition DateTime.cpp:122
void addHours(long hours)
Add hours to the date represented by this object.
Definition DateTime.hpp:455
String toStringGMT() const BLOCXX_DEPRECATED
This is the same as toString(E_UTC_TIME).
void setTime(int hour, int minute, int second, ETimeOffset timeOffset=E_LOCAL_TIME)
Set the time component of this DateTime object.
static Int16 localTimeAndOffset(time_t t, struct tm &tt)
Int16 toLocal(struct tm &tt) const
Converts date/time specified by *this to local time, stored in tt as per the C localtime function,...
Definition DateTime.hpp:601
static Int16 getGMTOffsetMinutesNow()
Returns the GMT offset (number of minutes) of the system's timezone at the current moment.
Definition DateTime.hpp:588
void addWeeks(int weeks)
Add week to the date represented by this object.
Definition DateTime.hpp:397
void addDays(int days)
Add days to the date represented by this object.
tm getTm(ETimeOffset timeOffset) const
Definition DateTime.cpp:982
void addMicroseconds(long microseconds)
Add microseconds to the date represented by this object.
Definition DateTime.hpp:437
void addSeconds(long seconds)
Add seconds to the date represented by this object.
Definition DateTime.hpp:421
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
Taken from RFC 1321.
Char16 operator-(const Char16 &arg1, const Char16 &arg2)
Definition Char16.hpp:320
bool operator<(const Array< T > &x, const Array< T > &y)
bool operator<=(const Array< T > &x, const Array< T > &y)
Determine if one Array is less than or equal to another.
Definition Array.hpp:469
bool operator>(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than another.
Definition Array.hpp:515
bool operator==(const Array< T > &x, const Array< T > &y)
bool operator>=(const Array< T > &x, const Array< T > &y)
Determine if one Array is greater than or equal to another.
Definition Array.hpp:492
bool operator!=(const Array< T > &x, const Array< T > &y)
Determine two Arrays are not equal.
Definition Array.hpp:446