Gorgon Game Engine
Time.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <string>
6 #include <sstream>
7 #include <iomanip>
8 #include <stdexcept>
9 
10 namespace Gorgon {
12  namespace Time {
13 
15  void Initialize();
16 
17  class Date;
18 
21  Date GetDate();
22 
26  class Date {
27  public:
29  enum WeekdayType {
30  Sunday=0,
36  Saturday
37  };
38 
40  enum MonthType {
41  Empty=0,
42  Jan = 1,
43  Feb,
44  Mar,
45  Apr,
46  May,
47  Jun,
48  Jul,
49  Agu,
50  Sep,
51  Oct,
52  Nov,
53  Dec
54  };
55 
58  Date() : Day(0), Month(Empty), Year(0), Hour(0), Minute(0),
60  { }
61 
64  Date(const std::string &isodate) { if(!Parse(isodate)) throw std::runtime_error("Invalid date"); }
65 
68  Date(std::istream &source) { if(!Load(source)) throw std::runtime_error("Invalid date"); }
69 
77  bool Save(std::ostream &target);
78 
81  bool Load(std::istream &source);
82 
97  bool Parse(std::string isodate);
98 
100  std::string ISODate() const;
101 
103  std::string ISODateTime(bool timezone = true) const;
104 
108  const std::string &MonthName_En() const;
109 
113  const std::string &ShortMonthName_En() const;
114 
117  const std::string &WeekdayName_En() const;
118 
121  const std::string &ShortWeekdayName_En() const;
122 
125  std::string Date_En() const;
126 
129  std::string ShortDate_En() const;
130 
132  std::string Time() const;
133 
135  std::string ShortTime() const;
136 
142  std::string Timezone_GMT() const;
143 
145  static int LocalTimezone();
146 
148  void AddYears(int years);
149 
151  void AddMonths(int months);
152 
154  void AddDays(int days);
155 
157  void AddHours(int hours);
158 
160  void AddMinutes(int minutes);
161 
163  void AddSeconds(int seconds);
164 
166  bool IsSet() const {
167  return Month==Empty;
168  }
169 
171  static Date Now() {
172  return GetDate();
173  }
174 
176  double operator - (const Date &other);
177 
179  bool operator == (const Date &other) const;
180 
182  void Unset() {
183  Year=0;
184  Month=Empty;
185  Day=0;
186  Weekday=Sunday;
187  Hour=0;
188  Minute=0;
189  Second=0;
190  Millisecond=0;
191  Timezone=0;
192  }
193 
196  bool DetermineWeekday();
197 
199  unsigned int Year;
200 
203 
205  unsigned int Day;
206 
209 
211  unsigned int Hour;
212 
214  unsigned int Minute;
215 
217  unsigned int Second;
218 
220  unsigned int Millisecond;
221 
224  int Timezone;
225  };
226 
228  inline std::ostream &operator << (std::ostream &out, const Date &date) {
229  out<<date.ISODateTime(true);
230 
231  return out;
232  }
233 
238  unsigned long GetTime();
239 
240 
242  namespace internal {
243  extern unsigned long framestart;
244  extern unsigned long deltatime;
245  }
247 
248 
252  inline unsigned long FrameStart() {
253  return internal::framestart;
254  }
255 
258  inline unsigned long DeltaTime() {
259  return internal::deltatime;
260  }
261 
262 
263  }
264 }
Gorgon::Time::Date::AddMonths
void AddMonths(int months)
Adds specified amount of months to the date.
Definition: Time.cpp:359
Gorgon::Time::Date::Thursday
@ Thursday
Definition: Time.h:34
Gorgon::Time::Date::Saturday
@ Saturday
Definition: Time.h:36
Gorgon::Time::GetDate
Date GetDate()
Returns the current date.
Definition: Linux.cpp:9
Gorgon::Time::Date::Agu
@ Agu
Definition: Time.h:49
Gorgon::Time::Date::Date
Date(std::istream &source)
Reads a new date object from a binary stream.
Definition: Time.h:68
Gorgon::Time::Date::May
@ May
Definition: Time.h:46
Gorgon::Time::internal::framestart
unsigned long framestart
Definition: Time.cpp:406
Gorgon::Time::Date::ISODate
std::string ISODate() const
ISO compliant date format. Contains only date.
Definition: Time.cpp:147
Gorgon::Time::GetTime
unsigned long GetTime()
Returns current time in milliseconds.
Definition: Linux.cpp:34
Gorgon::Time::Date::Empty
@ Empty
Definition: Time.h:41
Gorgon::Time::Date::WeekdayType
WeekdayType
Days of week. Starts from sunday.
Definition: Time.h:29
Gorgon::Time::Date::ShortWeekdayName_En
const std::string & ShortWeekdayName_En() const
Returns currently stored week day's int name in English.
Definition: Time.cpp:233
Gorgon::Time::Date::Oct
@ Oct
Definition: Time.h:51
Gorgon::Time::operator<<
std::ostream & operator<<(std::ostream &out, const Date &date)
Output stream operator overload.
Definition: Time.h:228
Gorgon::Time::internal::deltatime
unsigned long deltatime
Definition: Time.cpp:407
Gorgon::Time::Date::operator-
double operator-(const Date &other)
Gives the difference between two dates.
Definition: Time.cpp:332
Gorgon::Time::Date::Jun
@ Jun
Definition: Time.h:47
Gorgon::Time::Date::LocalTimezone
static int LocalTimezone()
Returns the system timezone in minutes. Might be negative.
Definition: Time.cpp:306
Gorgon::Time::Date::Millisecond
unsigned int Millisecond
This value is from the last second tick.
Definition: Time.h:220
Gorgon::Time::Date::Date
Date(const std::string &isodate)
Creates a new date object from the given ISO-8601 date string.
Definition: Time.h:64
Gorgon::Time::Date::Hour
unsigned int Hour
Hour in 24 hour format.
Definition: Time.h:211
Gorgon::Time::Date::ShortTime
std::string ShortTime() const
Returns stored time in hour:minute format.
Definition: Time.cpp:274
Gorgon::Time::Date::ShortMonthName_En
const std::string & ShortMonthName_En() const
Returns currently stored month's int name in English.
Definition: Time.cpp:202
Gorgon::Time::DeltaTime
unsigned long DeltaTime()
Returns the time passed since the last frame.
Definition: Time.h:258
Gorgon::Time::Date::Sunday
@ Sunday
Definition: Time.h:30
Gorgon::Time::Date::Minute
unsigned int Minute
Minute.
Definition: Time.h:214
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Time::Date::Unset
void Unset()
Unsets the stored time.
Definition: Time.h:182
Gorgon::Time::Date::Load
bool Load(std::istream &source)
Loads date object from a binary stream.
Definition: Time.cpp:83
Gorgon::Time::Date::Tuesday
@ Tuesday
Definition: Time.h:32
Gorgon::Time::Date::Month
MonthType Month
Month starts from jan = 1.
Definition: Time.h:202
Gorgon::Time::Date::Save
bool Save(std::ostream &target)
Writes date object to a binary stream.
Definition: Time.cpp:112
Gorgon::Time::Date::Monday
@ Monday
Definition: Time.h:31
Gorgon::Time::Date::Dec
@ Dec
Definition: Time.h:53
Gorgon::Time::Date::ISODateTime
std::string ISODateTime(bool timezone=true) const
ISO compliant date/time. This format should be used to serialize as text.
Definition: Time.cpp:160
Gorgon::Time::Date::ShortDate_En
std::string ShortDate_En() const
Returns stored date in day intmonthname year format with English names for month.
Definition: Time.cpp:256
Gorgon::Time::Date::Timezone_GMT
std::string Timezone_GMT() const
Returns stored timezone.
Definition: Time.cpp:284
Gorgon::Time::Date::Day
unsigned int Day
Day in month.
Definition: Time.h:205
Gorgon::Time::Date::Mar
@ Mar
Definition: Time.h:44
Gorgon::Time::Date::MonthType
MonthType
Months, january is 1.
Definition: Time.h:40
Gorgon::Time::Date::Now
static Date Now()
Returns current time.
Definition: Time.h:171
Gorgon::Time::Date::Timezone
int Timezone
Timezone in minutes, can be negative.
Definition: Time.h:224
Gorgon::Time::Date::Friday
@ Friday
Definition: Time.h:35
Gorgon::Time::Date::Parse
bool Parse(std::string isodate)
Creates a new date object from the given ISO-8601 date string.
Definition: Time.cpp:44
Gorgon::Time::Date
This class represents a specific date including time information.
Definition: Time.h:26
Gorgon::Time::Date::Wednesday
@ Wednesday
Definition: Time.h:33
Gorgon::Time::Date::Nov
@ Nov
Definition: Time.h:52
Gorgon::Time::Date::AddSeconds
void AddSeconds(int seconds)
Adds specified amount of seconds to the date.
Definition: Time.cpp:395
Gorgon::Time::Date::Weekday
WeekdayType Weekday
Day of the week, starts from sunday = 0.
Definition: Time.h:208
Gorgon::Empty
A class that has no members and can be used as placeholder.
Definition: Types.h:51
Gorgon::Time::Date::Apr
@ Apr
Definition: Time.h:45
Gorgon::Time::Date::Time
std::string Time() const
Returns stored time in hour:minute:second format.
Definition: Time.cpp:264
Gorgon::Time::Date::Second
unsigned int Second
Second.
Definition: Time.h:217
Gorgon::Time::Initialize
void Initialize()
Initializes Time module.
Definition: Time.cpp:15
Gorgon::Time::Date::WeekdayName_En
const std::string & WeekdayName_En() const
Returns currently stored week day's name in English.
Definition: Time.cpp:219
Gorgon::Time::Date::AddDays
void AddDays(int days)
Adds specified amount of days to the date.
Definition: Time.cpp:368
Gorgon::Time::Date::DetermineWeekday
bool DetermineWeekday()
Determines the weekday from the stored date.
Definition: Time.cpp:319
Gorgon::Time::FrameStart
unsigned long FrameStart()
Returns start time of the current frame in milliseconds.
Definition: Time.h:252
Gorgon::Time::Date::MonthName_En
const std::string & MonthName_En() const
Returns currently stored month's name in English.
Definition: Time.cpp:185
Gorgon::Time::Date::operator==
bool operator==(const Date &other) const
Compares 2 dates.
Definition: Time.cpp:341
Gorgon::Time::Date::AddYears
void AddYears(int years)
Adds specified amount of years to the date.
Definition: Time.cpp:350
Gorgon::Time::Date::Date_En
std::string Date_En() const
Returns stored date in day monthname year, weekday format with English names for month and weekday.
Definition: Time.cpp:247
Gorgon::Time::Date::Year
unsigned int Year
Full year.
Definition: Time.h:199
Gorgon::Time::Date::Jul
@ Jul
Definition: Time.h:48
Gorgon::Time::Date::AddHours
void AddHours(int hours)
Adds specified amount of hours to the date.
Definition: Time.cpp:377
Gorgon::Time::Date::Feb
@ Feb
Definition: Time.h:43
Gorgon::Time::Date::IsSet
bool IsSet() const
Checks whether the stored time is actually set.
Definition: Time.h:166
Gorgon::Time::Date::Date
Date()
Default constructor, zero initializes the class, making it an unset time.
Definition: Time.h:58
Gorgon::Time::Date::Sep
@ Sep
Definition: Time.h:50
Gorgon::Time::Date::Jan
@ Jan
Definition: Time.h:42
Gorgon::Time::Date::AddMinutes
void AddMinutes(int minutes)
Adds specified amount of minutes to the date.
Definition: Time.cpp:386