| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __COMMON_BASE_DATE_H__ |
|---|
| 33 |
#define __COMMON_BASE_DATE_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <time.h> |
|---|
| 36 |
#include <iostream> |
|---|
| 37 |
#include <string> |
|---|
| 38 |
#include <whisperlib/common/base/types.h> |
|---|
| 39 |
|
|---|
| 40 |
namespace timer { |
|---|
| 41 |
|
|---|
| 42 |
class Date { |
|---|
| 43 |
protected: |
|---|
| 44 |
// the number of milliseconds since January 1, 1970, 00:00:00 UTC |
|---|
| 45 |
int64 time_; |
|---|
| 46 |
|
|---|
| 47 |
// cached broken down time |
|---|
| 48 |
// FYI: |
|---|
| 49 |
// struct tm { |
|---|
| 50 |
// int tm_sec; // seconds (0..59) |
|---|
| 51 |
// int tm_min; // minutes (0..59) |
|---|
| 52 |
// int tm_hour; // hours (0..23) |
|---|
| 53 |
// int tm_mday; // day of the month (1..31) |
|---|
| 54 |
// int tm_mon; // month (0..11) |
|---|
| 55 |
// int tm_year; // number of years since 1900 |
|---|
| 56 |
// int tm_wday; // day of the week (0..6) |
|---|
| 57 |
// int tm_yday; // day in the year (0..365) |
|---|
| 58 |
// int tm_isdst; // daylight saving time (flag: |
|---|
| 59 |
// // - 1 is daylight saving time is in effect, |
|---|
| 60 |
// // - 0 if it's not, |
|---|
| 61 |
// // - negative if the information is not available) |
|---|
| 62 |
// }; |
|---|
| 63 |
// |
|---|
| 64 |
struct tm broken_down_time_; |
|---|
| 65 |
|
|---|
| 66 |
// because struct tm does not have milisecond |
|---|
| 67 |
int broken_down_milisecond_; |
|---|
| 68 |
|
|---|
| 69 |
// true = the broken_down_time is expressed in Coordinated |
|---|
| 70 |
// Universal Time (UTC). |
|---|
| 71 |
// The logging and printing is also UTC. |
|---|
| 72 |
// false = the broken_down_time is relative to local user's time zone. |
|---|
| 73 |
// The logging and printing is also local. |
|---|
| 74 |
bool is_utc_; |
|---|
| 75 |
|
|---|
| 76 |
bool has_errors_; |
|---|
| 77 |
|
|---|
| 78 |
public: |
|---|
| 79 |
// 0 -> January |
|---|
| 80 |
// 1 -> February |
|---|
| 81 |
// .... |
|---|
| 82 |
static const char* MonthName(int month_number); |
|---|
| 83 |
|
|---|
| 84 |
// 0 -> Sunday |
|---|
| 85 |
// 1 -> Monday |
|---|
| 86 |
// 2 -> Tuesday |
|---|
| 87 |
// .... |
|---|
| 88 |
static const char* DayOfTheWeekName(int day_of_the_week_number); |
|---|
| 89 |
|
|---|
| 90 |
// returns: the number of milliseconds since January 1, 1970, 00:00:00 UTC |
|---|
| 91 |
static int64 Now(); |
|---|
| 92 |
|
|---|
| 93 |
// returns: the time on a different day at the same 'time' (hour, minute, second). |
|---|
| 94 |
// For UTC time this is simple: just shift time by 'days' * 24 * 3600000 ms . |
|---|
| 95 |
// For local time: takes care of daylight saving time. |
|---|
| 96 |
static int64 ShiftDay(int64 time, int32 days, bool is_utc); |
|---|
| 97 |
|
|---|
| 98 |
public: |
|---|
| 99 |
// Initialize to current (now!) date & time. |
|---|
| 100 |
// If use_utc = true the broken down time will be stored in UTC format, |
|---|
| 101 |
// else local timezone is used. |
|---|
| 102 |
explicit Date(bool use_utc = false); |
|---|
| 103 |
|
|---|
| 104 |
// Initialize to the given moment. |
|---|
| 105 |
// time = the number of milliseconds since the Epoch |
|---|
| 106 |
// (January 1, 1970, 00:00:00 UTC) |
|---|
| 107 |
// use_utc = true: the broken down time will be stored in UTC format, |
|---|
| 108 |
// also Print and LogTo will write the time in UTC format. |
|---|
| 109 |
// false: local timezone is used. |
|---|
| 110 |
// Print and LogTo will write the localtime. |
|---|
| 111 |
Date(int64 time, bool use_utc = false); |
|---|
| 112 |
|
|---|
| 113 |
// Same as above, but time is expressed in seconds since the Epoch. |
|---|
| 114 |
#if __WORDSIZE != 64 |
|---|
| 115 |
Date(time_t time, bool use_utc = false); |
|---|
| 116 |
#endif |
|---|
| 117 |
Date(int year, int month, int day, int hour, |
|---|
| 118 |
int minute, int second, int milisecond, bool use_utc = false); |
|---|
| 119 |
|
|---|
| 120 |
Date(const Date& date); |
|---|
| 121 |
|
|---|
| 122 |
virtual ~Date() { |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
// |
|---|
| 126 |
// Accessors |
|---|
| 127 |
// |
|---|
| 128 |
int GetYear() const { return 1900 + broken_down_time_.tm_year; } |
|---|
| 129 |
int GetMonth() const { return broken_down_time_.tm_mon; } |
|---|
| 130 |
int GetDay() const { return broken_down_time_.tm_mday; } |
|---|
| 131 |
int GetHour() const { return broken_down_time_.tm_hour; } |
|---|
| 132 |
int GetMinute() const { return broken_down_time_.tm_min; } |
|---|
| 133 |
int GetSecond() const { return broken_down_time_.tm_sec; } |
|---|
| 134 |
int GetMilisecond() const { return broken_down_milisecond_; } |
|---|
| 135 |
|
|---|
| 136 |
int GetDayOfTheWeek() const { return broken_down_time_.tm_wday; } |
|---|
| 137 |
int GetDayOfTheYear() const { return broken_down_time_.tm_yday; } |
|---|
| 138 |
|
|---|
| 139 |
const char* GetTimezoneName() const { |
|---|
| 140 |
return broken_down_time_.tm_zone; |
|---|
| 141 |
} |
|---|
| 142 |
const char* GetMonthName() const { |
|---|
| 143 |
return MonthName(GetMonth()); |
|---|
| 144 |
} |
|---|
| 145 |
const char* GetDayOfTheWeekName() const { |
|---|
| 146 |
return DayOfTheWeekName(GetDayOfTheWeek()); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
// Returns ture iff the current broken down representation is set to UTC. |
|---|
| 150 |
bool IsUTC() const { return is_utc_; } |
|---|
| 151 |
|
|---|
| 152 |
// Returns if the value set in the last operation was OK (all values were |
|---|
| 153 |
// valid); |
|---|
| 154 |
bool HasErrors() const { return has_errors_; } |
|---|
| 155 |
|
|---|
| 156 |
// returns: the number of milliseconds since January 1, 1970, 00:00:00 UTC |
|---|
| 157 |
// represented by this date. |
|---|
| 158 |
int64 GetTime() const { return time_; } |
|---|
| 159 |
|
|---|
| 160 |
// |
|---|
| 161 |
// Settors |
|---|
| 162 |
// |
|---|
| 163 |
|
|---|
| 164 |
// Set the date to represent the given broken-down-time in UTC |
|---|
| 165 |
// or local timezone format. |
|---|
| 166 |
// Returns the succes value |
|---|
| 167 |
bool Set(int year, int month, int day, int hour, |
|---|
| 168 |
int minute, int second, int milisecond, bool use_utc = false); |
|---|
| 169 |
|
|---|
| 170 |
// Set the date to represent the specified number of milliseconds since the |
|---|
| 171 |
// standard base time known as "the epoch", namely (1/1/1970, 00:00:00 UTC) |
|---|
| 172 |
void SetTime(int64 time); |
|---|
| 173 |
|
|---|
| 174 |
// Assigns the current moment of time as the date & time in this object |
|---|
| 175 |
void SetNow(); |
|---|
| 176 |
|
|---|
| 177 |
// sets the current broken down representation to UTC or Local Time. |
|---|
| 178 |
void SetUTC(bool use_utc); |
|---|
| 179 |
|
|---|
| 180 |
// Builds a date from a short string |
|---|
| 181 |
bool SetFromShortString(const string& s, bool is_utc); |
|---|
| 182 |
// |
|---|
| 183 |
// Operators |
|---|
| 184 |
// |
|---|
| 185 |
Date& operator=(const Date&); |
|---|
| 186 |
bool operator==(const Date&) const; |
|---|
| 187 |
|
|---|
| 188 |
string ToShortString() const; |
|---|
| 189 |
string ToString() const; |
|---|
| 190 |
}; |
|---|
| 191 |
|
|---|
| 192 |
ostream& operator<<(ostream& os, const Date& date); |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
#endif // __COMMON_BASE_DATE_H__ |
|---|