datetime.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Kenneth Gangstoe
28 ** Harry Storbacka
29 */
30
31
32
33 #pragma once
34
35 #include "../System/cl_platform.h"
36
37 namespace clan
38 {
41
43 class DateTime
44 {
47 public:
48 enum TimeZone
49 {
50 local_timezone,
51 utc_timezone
52 };
53
55 DateTime();
56 DateTime(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone=utc_timezone);
57 ~DateTime();
58
60 static DateTime get_current_local_time();
61
63 static DateTime get_current_utc_time();
64
66 static DateTime get_local_time_from_ticks(int64_t ticks);
67
69 static DateTime get_utc_time_from_ticks(int64_t ticks);
70
71 static DateTime from_short_date_string(const std::string &value);
72
74
77 public:
78 bool is_null() const;
79 unsigned short get_year() const;
80
84 unsigned char get_month() const;
85 unsigned char get_day() const;
86 unsigned char get_hour() const;
87 unsigned char get_minutes() const;
88 unsigned char get_seconds() const;
89 unsigned int get_nanoseconds() const;
90 TimeZone get_timezone() const;
91
95 unsigned char get_week() const;
96
100 int get_difference_in_days(const DateTime &other) const;
101
105 unsigned int get_day_of_week() const;
106
110 static int get_days_in_month(int month, int year);
112
115 public:
116 void set_null();
117 void set_date(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone = utc_timezone);
118 void set_year(int year);
119 void set_month(int month);
120 void set_day(int day);
121 void set_hour(int hour);
122 void set_minutes(int minutes);
123 void set_seconds(int seconds);
124 void set_nanoseconds(int nanoseconds);
125 void set_timezone(TimeZone timezone);
126
127 DateTime &add_years(int years);
128 DateTime &add_days(int days);
129 DateTime &add_months(int months);
130 /*
131 void add_hours(int hours);
132 void add_minutes(int minutes);
133 void add_seconds(int seconds);
134 void add_nanoseconds(int nanoseconds);
135 */
136 DateTime to_utc() const;
137 DateTime to_local() const;
138
140 int64_t to_ticks() const;
141
143 std::string to_long_date_string() const;
144
146 std::string to_short_date_string() const;
147
149 std::string to_short_datetime_string() const;
150
152 std::string to_long_time_string() const;
153
155 std::string to_short_time_string() const;
156
158 std::string to_string() const;
159
160 bool operator <(const DateTime &other) const;
161 bool operator <=(const DateTime &other) const;
162 bool operator >(const DateTime &other) const;
163 bool operator >=(const DateTime &other) const;
164 bool operator ==(const DateTime &other) const;
165 bool operator !=(const DateTime &other) const;
167
170 private:
171 void throw_if_invalid_date(int year, int month, int day, int hour, int minute, int seconds, int nanoseconds) const;
172 void throw_if_null() const;
173
174 int get_day_number() const;
175 void set_date_from_daynumber(int g);
176
177 unsigned short year;
178 unsigned char month;
179 unsigned char day;
180 unsigned char hour;
181 unsigned char minute;
182 unsigned char seconds;
183 unsigned int nanoseconds;
184
185 TimeZone timezone;
186 static const int64_t ticks_from_1601_to_1900;
188 };
189
190 }
191
clan::DateTime::operator>
bool operator>(const DateTime &other) const
clan::DateTime::~DateTime
~DateTime()
clan::DateTime::set_seconds
void set_seconds(int seconds)
clan::DateTime::to_ticks
int64_t to_ticks() const
Converts the date to the number of 100-nanosecond intervals since January 1, 1601 UTC...
clan
Definition: clanapp.h:35
clan::DateTime::to_string
std::string to_string() const
Mon Feb 3 12:32:54 2008.
clan::DateTime::add_months
DateTime & add_months(int months)
clan::DateTime::to_short_datetime_string
std::string to_short_datetime_string() const
yyyy-mm-dd hh:mm:ss
clan::DateTime
Date/Time class.
Definition: datetime.h:43
clan::DateTime::TimeZone
TimeZone
Definition: datetime.h:48
clan::DateTime::operator==
bool operator==(const DateTime &other) const
clan::DateTime::get_minutes
unsigned char get_minutes() const
clan::Key::g
clan::DateTime::to_utc
DateTime to_utc() const
clan::DateTime::is_null
bool is_null() const
clan::DateTime::operator<=
bool operator<=(const DateTime &other) const
clan::DateTime::set_timezone
void set_timezone(TimeZone timezone)
clan::DateTime::get_utc_time_from_ticks
static DateTime get_utc_time_from_ticks(int64_t ticks)
Converts a time tick value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a date t...
clan::DateTime::operator!=
bool operator!=(const DateTime &other) const
clan::DateTime::to_long_time_string
std::string to_long_time_string() const
hh:mm:ss
clan::DateTime::utc_timezone
Definition: datetime.h:51
clan::DateTime::operator<
bool operator<(const DateTime &other) const
clan::DateTime::set_nanoseconds
void set_nanoseconds(int nanoseconds)
clan::StyleTokenType::string
@ keyword
clan::DateTime::get_current_local_time
static DateTime get_current_local_time()
Get current system time in local time zone.
clan::DateTime::get_timezone
TimeZone get_timezone() const
clan::DateTime::add_years
DateTime & add_years(int years)
clan::DateTime::to_short_time_string
std::string to_short_time_string() const
hh:mm
clan::DateTime::DateTime
DateTime()
Constructs a date/time object.
clan::DateTime::from_short_date_string
static DateTime from_short_date_string(const std::string &value)
clan::DateTime::set_null
void set_null()
clan::DateTime::to_short_date_string
std::string to_short_date_string() const
yyyy-mm-dd
clan::DateTime::add_days
DateTime & add_days(int days)
clan::DateTime::get_nanoseconds
unsigned int get_nanoseconds() const
clan::DateTime::set_year
void set_year(int year)
clan::DateTime::local_timezone
Definition: datetime.h:50
clan::DateTime::get_month
unsigned char get_month() const
Returns the month number in range 1-12.
clan::DateTime::get_days_in_month
static int get_days_in_month(int month, int year)
Returns the number of days in the given month.
clan::DateTime::get_local_time_from_ticks
static DateTime get_local_time_from_ticks(int64_t ticks)
Converts a time tick value (number of 100-nanosecond intervals since January 1, 1601 UTC) to a date t...
clan::DateTime::to_local
DateTime to_local() const
clan::DateTime::get_hour
unsigned char get_hour() const
clan::DateTime::get_day_of_week
unsigned int get_day_of_week() const
Get the day of the week.
clan::DateTime::operator>=
bool operator>=(const DateTime &other) const
clan::DateTime::set_hour
void set_hour(int hour)
clan::DateTime::set_day
void set_day(int day)
clan::DateTime::get_week
unsigned char get_week() const
Returns the ISO 8601 week number of the date.
clan::DateTime::set_date
void set_date(int year, int month, int day, int hour=0, int minute=0, int seconds=0, int nanoseconds=0, TimeZone timezone=utc_timezone)
clan::DateTime::set_minutes
void set_minutes(int minutes)
clan::DateTime::set_month
void set_month(int month)
clan::DateTime::to_long_date_string
std::string to_long_date_string() const
Mon Mar 3 2007.
clan::DateTime::get_day
unsigned char get_day() const
clan::DateTime::get_difference_in_days
int get_difference_in_days(const DateTime &other) const
Returns the difference in days between two dates. This function is only accurate for the next few mil...
clan::DateTime::get_seconds
unsigned char get_seconds() const
clan::DateTime::get_current_utc_time
static DateTime get_current_utc_time()
Get current system time in UTC.
clan::DateTime::get_year
unsigned short get_year() const