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  {
52  };
53 
56  DateTime(int year, int month, int day, int hour = 0, int minute = 0, int seconds = 0, int nanoseconds = 0, TimeZone timezone=utc_timezone);
58 
61 
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;
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;
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 
static DateTime get_current_utc_time()
Get current system time in UTC.
void set_timezone(TimeZone timezone)
DateTime & add_days(int days)
unsigned char get_seconds() const
std::string to_long_time_string() const
hh:mm:ss
void set_day(int day)
unsigned char get_week() const
Returns the ISO 8601 week number of the date.
unsigned int get_day_of_week() const
Get the day of the week.
void set_seconds(int seconds)
bool operator<=(const DateTime &other) const
std::string to_short_time_string() const
hh:mm
std::string to_long_date_string() const
Mon Mar 3 2007.
bool is_null() const
unsigned short get_year() const
@ utc_timezone
Definition: datetime.h:51
unsigned char get_day() const
bool operator!=(const DateTime &other) const
Date/Time class.
Definition: datetime.h:44
void set_year(int year)
std::string to_short_datetime_string() const
yyyy-mm-dd hh:mm:ss
DateTime()
Constructs a date/time object.
static DateTime from_short_date_string(const std::string &value)
DateTime to_local() const
TimeZone
Definition: datetime.h:49
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...
void set_nanoseconds(int nanoseconds)
bool operator<(const DateTime &other) const
std::string to_short_date_string() const
yyyy-mm-dd
int64_t to_ticks() const
Converts the date to the number of 100-nanosecond intervals since January 1, 1601 UTC.
@ local_timezone
Definition: datetime.h:50
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...
bool operator>=(const DateTime &other) const
static DateTime get_current_local_time()
Get current system time in local time zone.
DateTime & add_years(int years)
static int get_days_in_month(int month, int year)
Returns the number of days in the given month.
Definition: clanapp.h:36
DateTime to_utc() const
unsigned char get_minutes() const
void set_minutes(int minutes)
DateTime & add_months(int months)
unsigned char get_month() const
Returns the month number in range 1-12.
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...
void set_month(int month)
unsigned int get_nanoseconds() const
TimeZone get_timezone() const
void set_hour(int hour)
std::string to_string() const
Mon Feb 3 12:32:54 2008.
DateTime(int year, int month, int day, int hour=0, int minute=0, int seconds=0, int nanoseconds=0, TimeZone timezone=utc_timezone)
bool operator>(const DateTime &other) const
bool operator==(const DateTime &other) const
unsigned char get_hour() const
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)