1 /***************************************************************** 2 | 3 | Neptune - Time 4 | 5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC. 6 | All rights reserved. 7 | 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. 18 | 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. 29 | 30 ****************************************************************/ 31 32 #ifndef _NPT_TIME_H_ 33 #define _NPT_TIME_H_ 34 35 /*---------------------------------------------------------------------- 36 | includes 37 +---------------------------------------------------------------------*/ 38 #include "NptTypes.h" 39 #include "NptStrings.h" 40 41 /*---------------------------------------------------------------------- 42 | constants 43 +---------------------------------------------------------------------*/ 44 #define NPT_DATETIME_YEAR_MIN 1901 45 #define NPT_DATETIME_YEAR_MAX 2262 46 47 /*---------------------------------------------------------------------- 48 | NPT_TimeStamp 49 +---------------------------------------------------------------------*/ 50 class NPT_TimeStamp 51 { 52 public: 53 // methods 54 NPT_TimeStamp(const NPT_TimeStamp& timestamp); NPT_TimeStamp()55 NPT_TimeStamp() : m_NanoSeconds(0) {} NPT_TimeStamp(NPT_Int64 nanoseconds)56 NPT_TimeStamp(NPT_Int64 nanoseconds) : m_NanoSeconds(nanoseconds) {} 57 NPT_TimeStamp(double seconds); 58 NPT_TimeStamp& operator+=(const NPT_TimeStamp& time_stamp); 59 NPT_TimeStamp& operator-=(const NPT_TimeStamp& time_stamp); 60 bool operator==(const NPT_TimeStamp& t) const { return m_NanoSeconds == t.m_NanoSeconds; } 61 bool operator!=(const NPT_TimeStamp& t) const { return m_NanoSeconds != t.m_NanoSeconds; } 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 67 // accessors SetNanos(NPT_Int64 nanoseconds)68 void SetNanos(NPT_Int64 nanoseconds) { m_NanoSeconds = nanoseconds; } SetMicros(NPT_Int64 micros)69 void SetMicros(NPT_Int64 micros) { m_NanoSeconds = micros * 1000; } SetMillis(NPT_Int64 millis)70 void SetMillis(NPT_Int64 millis) { m_NanoSeconds = millis * 1000000; } SetSeconds(NPT_Int64 seconds)71 void SetSeconds(NPT_Int64 seconds) { m_NanoSeconds = seconds * 1000000000; } 72 73 // conversion 74 operator double() const { return (double)m_NanoSeconds/1E9; } FromNanos(NPT_Int64 nanoseconds)75 void FromNanos(NPT_Int64 nanoseconds) { m_NanoSeconds = nanoseconds; } ToNanos()76 NPT_Int64 ToNanos() const { return m_NanoSeconds; } ToMicros()77 NPT_Int64 ToMicros() const { return m_NanoSeconds/1000; } ToMillis()78 NPT_Int64 ToMillis() const { return m_NanoSeconds/1000000; } ToSeconds()79 NPT_Int64 ToSeconds() const { return m_NanoSeconds/1000000000; } 80 81 private: 82 // members 83 NPT_Int64 m_NanoSeconds; 84 }; 85 86 /*---------------------------------------------------------------------- 87 | operator+ 88 +---------------------------------------------------------------------*/ 89 inline 90 NPT_TimeStamp 91 operator+(const NPT_TimeStamp& t1, const NPT_TimeStamp& t2) 92 { 93 NPT_TimeStamp t = t1; 94 return t += t2; 95 } 96 97 /*---------------------------------------------------------------------- 98 | operator- 99 +---------------------------------------------------------------------*/ 100 inline 101 NPT_TimeStamp 102 operator-(const NPT_TimeStamp& t1, const NPT_TimeStamp& t2) 103 { 104 NPT_TimeStamp t = t1; 105 return t -= t2; 106 } 107 108 /*---------------------------------------------------------------------- 109 | NPT_TimeInterval 110 +---------------------------------------------------------------------*/ 111 typedef NPT_TimeStamp NPT_TimeInterval; 112 113 /*---------------------------------------------------------------------- 114 | NPT_DateTime 115 +---------------------------------------------------------------------*/ 116 class NPT_DateTime { 117 public: 118 // types 119 enum Format { 120 FORMAT_ANSI, 121 FORMAT_W3C, 122 FORMAT_RFC_1123, // RFC 822 updated by RFC 1123 123 FORMAT_RFC_1036 // RFC 850 updated by RFC 1036 124 }; 125 126 enum FormatFlags { 127 FLAG_EMIT_FRACTION = 1, 128 FLAG_EXTENDED_PRECISION = 2 129 }; 130 131 // class methods 132 NPT_Int32 GetLocalTimeZone(); 133 134 // constructors 135 NPT_DateTime(); 136 NPT_DateTime(const NPT_TimeStamp& timestamp, bool local=false); 137 138 // methods 139 NPT_Result ChangeTimeZone(NPT_Int32 timezone); 140 NPT_Result FromTimeStamp(const NPT_TimeStamp& timestamp, bool local=false); 141 NPT_Result ToTimeStamp(NPT_TimeStamp& timestamp) const; 142 NPT_Result FromString(const char* date, Format format = FORMAT_ANSI); 143 NPT_String ToString(Format format = FORMAT_ANSI, NPT_Flags flags=0) const; 144 145 // members 146 NPT_Int32 m_Year; // year 147 NPT_Int32 m_Month; // month of the year (1-12) 148 NPT_Int32 m_Day; // day of the month (1-31) 149 NPT_Int32 m_Hours; // hours (0-23) 150 NPT_Int32 m_Minutes; // minutes (0-59) 151 NPT_Int32 m_Seconds; // seconds (0-59) 152 NPT_Int32 m_NanoSeconds; // nanoseconds (0-999999999) 153 NPT_Int32 m_TimeZone; // minutes offset from GMT 154 }; 155 156 #endif // _NPT_TIME_H_ 157