1 /*****************************************************************
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
39 #include "NptStrings.h"
41 /*----------------------------------------------------------------------
43 +---------------------------------------------------------------------*/
44 #define NPT_DATETIME_YEAR_MIN 1901
45 #define NPT_DATETIME_YEAR_MAX 2262
47 /*----------------------------------------------------------------------
49 +---------------------------------------------------------------------*/
54 NPT_TimeStamp(const NPT_TimeStamp
& timestamp
);
55 NPT_TimeStamp() : m_NanoSeconds(0) {}
56 NPT_TimeStamp(NPT_Int64 nanoseconds
) : m_NanoSeconds(nanoseconds
) {}
57 NPT_TimeStamp(float seconds
);
58 NPT_TimeStamp(double seconds
);
59 NPT_TimeStamp
& operator=(const NPT_TimeStamp
&) = default;
60 NPT_TimeStamp
& operator+=(const NPT_TimeStamp
& time_stamp
);
61 NPT_TimeStamp
& operator-=(const NPT_TimeStamp
& time_stamp
);
62 bool operator==(const NPT_TimeStamp
& t
) const { return m_NanoSeconds
== t
.m_NanoSeconds
; }
63 bool operator!=(const NPT_TimeStamp
& t
) const { return m_NanoSeconds
!= t
.m_NanoSeconds
; }
64 bool operator> (const NPT_TimeStamp
& t
) const { return m_NanoSeconds
> t
.m_NanoSeconds
; }
65 bool operator< (const NPT_TimeStamp
& t
) const { return m_NanoSeconds
< t
.m_NanoSeconds
; }
66 bool operator>=(const NPT_TimeStamp
& t
) const { return m_NanoSeconds
>= t
.m_NanoSeconds
; }
67 bool operator<=(const NPT_TimeStamp
& t
) const { return m_NanoSeconds
<= t
.m_NanoSeconds
; }
70 void SetNanos(NPT_Int64 nanoseconds
) { m_NanoSeconds
= nanoseconds
; }
71 void SetMicros(NPT_Int64 micros
) { m_NanoSeconds
= micros
* 1000; }
72 void SetMillis(NPT_Int64 millis
) { m_NanoSeconds
= millis
* 1000000; }
73 void SetSeconds(NPT_Int64 seconds
) { m_NanoSeconds
= seconds
* 1000000000; }
76 operator double() const { return (double)m_NanoSeconds
/1E9
; }
77 void FromNanos(NPT_Int64 nanoseconds
) { m_NanoSeconds
= nanoseconds
; }
78 NPT_Int64
ToNanos() const { return m_NanoSeconds
; }
79 NPT_Int64
ToMicros() const { return m_NanoSeconds
/1000; }
80 NPT_Int64
ToMillis() const { return m_NanoSeconds
/1000000; }
81 NPT_Int64
ToSeconds() const { return m_NanoSeconds
/1000000000; }
85 NPT_Int64 m_NanoSeconds
;
88 /*----------------------------------------------------------------------
90 +---------------------------------------------------------------------*/
93 operator+(const NPT_TimeStamp
& t1
, const NPT_TimeStamp
& t2
)
99 /*----------------------------------------------------------------------
101 +---------------------------------------------------------------------*/
104 operator-(const NPT_TimeStamp
& t1
, const NPT_TimeStamp
& t2
)
106 NPT_TimeStamp t
= t1
;
110 /*----------------------------------------------------------------------
112 +---------------------------------------------------------------------*/
113 typedef NPT_TimeStamp NPT_TimeInterval
;
115 /*----------------------------------------------------------------------
117 +---------------------------------------------------------------------*/
124 FORMAT_RFC_1123
, // RFC 822 updated by RFC 1123
125 FORMAT_RFC_1036
// RFC 850 updated by RFC 1036
129 FLAG_EMIT_FRACTION
= 1,
130 FLAG_EXTENDED_PRECISION
= 2
134 NPT_Int32
GetLocalTimeZone();
138 NPT_DateTime(const NPT_TimeStamp
& timestamp
, bool local
=false);
141 NPT_Result
ChangeTimeZone(NPT_Int32 timezone
);
142 NPT_Result
FromTimeStamp(const NPT_TimeStamp
& timestamp
, bool local
=false);
143 NPT_Result
ToTimeStamp(NPT_TimeStamp
& timestamp
) const;
144 NPT_Result
FromString(const char* date
, Format format
= FORMAT_ANSI
);
145 NPT_String
ToString(Format format
= FORMAT_ANSI
, NPT_Flags flags
=0) const;
148 NPT_Int32 m_Year
; // year
149 NPT_Int32 m_Month
; // month of the year (1-12)
150 NPT_Int32 m_Day
; // day of the month (1-31)
151 NPT_Int32 m_Hours
; // hours (0-23)
152 NPT_Int32 m_Minutes
; // minutes (0-59)
153 NPT_Int32 m_Seconds
; // seconds (0-59)
154 NPT_Int32 m_NanoSeconds
; // nanoseconds (0-999999999)
155 NPT_Int32 m_TimeZone
; // minutes offset from GMT
158 #endif // _NPT_TIME_H_