TrueReality  v0.1.1912
DateTime.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Class Inspired by the Delta3D Engine
22 * http://delta3dengine.org/
23 *
24 * @author Bradley Anderegg
25 */
26 #pragma once
27 
28 #include "Export.h"
29 
31 
37 namespace trUtil
38 {
45  {
46  public:
47 
56  {
58 
59  public:
61  static const TimeOrigin LOCAL_TIME;
63  static const TimeOrigin GMT_TIME;
64 
65  protected:
66 
72  /*virtual*/ ~TimeOrigin(){}
73 
74  private:
75 
83  TimeOrigin(const std::string& name) : trUtil::EnumerationString(name)
84  {
85  AddInstance(this);
86  }
87  };
88 
95  {
97 
98  public:
100  static const TimeType CLOCK_TIME;
102  static const TimeType SIMULATION_TIME;
104  static const TimeType SCENARIO_TIME;
106  static const TimeType TRIP_TIME;
108  static const TimeType TIME_STAMP;
110  static const TimeType TIME_TYPE_OTHER;
111 
112  protected:
113 
119  /*virtual*/ ~TimeType(){}
120 
121  private:
122 
130  TimeType(const std::string& name) : trUtil::EnumerationString(name)
131  {
132  AddInstance(this);
133  }
134  };
135 
143  {
145 
146  public:
147 
150 
153 
156 
159 
162 
165 
168 
171 
174 
175  protected:
176 
182  /*virtual*/ ~TimeFormat(){}
183 
184  private:
185 
193  TimeFormat(const std::string& name) : trUtil::EnumerationString(name)
194  {
195  AddInstance(this);
196  }
197  };
198 
199  public:
200 
206  DateTime();
207 
215  DateTime(const TimeOrigin& initAs);
216 
224  DateTime(time_t);
225 
233  DateTime(const struct tm&);
234 
242  DateTime(const DateTime&);
243 
253  DateTime& operator=(const DateTime&);
254 
260  virtual ~DateTime();
261 
271  static float GetLocalGMTOffset(bool accountForDST = false);
272 
278  void SetToLocalTime();
279 
285  void SetToGMTTime();
286 
297  void IncrementClock(double seconds);
298 
309  void AdjustTimeZone(float newGMTOffset);
310 
327  void GetTime(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& min, float& sec) const;
328 
341  void GetTime(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& min, unsigned& sec) const;
342 
359  void GetGMTTime(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& min, float& sec) const;
360 
373  void GetGMTTime(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& min, unsigned& sec) const;
374 
389  void SetTime(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned min, float sec);
390 
403  void SetTime(unsigned year, unsigned month, unsigned day, unsigned hour, unsigned min, unsigned sec);
404 
414  time_t GetTime() const;
415 
424  time_t GetGMTTime() const;
425 
434  void SetTime(time_t);
435 
444  double GetTimeInSeconds() const;
445 
453  void GetTime(tm&) const;
454 
463  void GetGMTTime(tm&) const;
464 
472  void SetTime(const tm&);
473 
485  void SetGMTOffset(float hourOffset, bool dayLightSavings);
486 
504  static float CalcGMTOffset(tm& timeParts, bool factorLocalDayLightSavingsIntoGMTOffset);
505 
518  void SetGMTOffset(double lattitude, double longitude, bool dayLightSavings);
519 
528  float GetGMTOffset() const;
529 
538  float GetTimeScale() const;
539 
548  void SetTimeScale(float percentScaleInSeconds);
549 
557  const TimeType& GetTimeType() const;
558 
566  void SetTimeType(const TimeType&);
567 
576  const TimeOrigin& GetTimeOrigin() const;
577 
586  void SetTimeOrigin(const TimeOrigin&);
587 
597  const TimeFormat& GetTimeFormat() const;
598 
608  void SetTimeFormat(const TimeFormat&);
609 
617  float GetSecond() const;
618 
626  void SetSecond(float sec);
627 
635  unsigned GetMinute() const;
636 
644  void SetMinute(unsigned min);
645 
653  unsigned GetHour() const;
654 
662  void SetHour(unsigned hour);
663 
671  unsigned GetDay() const;
672 
680  void SetDay(unsigned day);
681 
689  unsigned GetMonth() const;
690 
698  void SetMonth(unsigned month);
699 
707  unsigned GetYear() const;
708 
716  void SetYear(unsigned year);
717 
726  std::string ToString() const;
727 
738  std::string ToString(const TimeFormat&) const;
739 
751  static std::string ToString(const DateTime&, const TimeFormat&);
752 
760  operator time_t() const;
761 
769  operator tm() const;
770 
778  operator std::string() const;
779 
780  //TODO- add operators
781  //DateTime& operator +=(const DateTime&);
782  //DateTime& operator -=(const DateTime&);
783 
784  //bool operator<(const DateTime&) const;
785  //bool operator>(const DateTime&) const;
786 
787  //std::ostream& operator >>(std::ostream&);
788 
789  private:
790 
796  void ResetToDefaultValues();
797 
806  static void GetGMTTime(time_t* t, tm& timeParts);
807 
816  static void GetLocalTime(time_t* t, tm& timeParts);
817 
818 
820  float mGMTOffset;
822  float mTimeScale;
825 
833  unsigned mSeconds, mMinutes, mHours, mDays, mMonths, mYears;
834 
841  };
842 }
static const TimeFormat CALENDAR_DATE_AND_TIME_FORMAT
CALENDAR_DATE_AND_TIME_FORMAT: 2008-04-18T13:22:50-05:00.
Definition: DateTime.h:173
static const TimeType TRIP_TIME
The trip time.
Definition: DateTime.h:106
TimeOrigin(const std::string &name)
Constructor.
Definition: DateTime.h:83
float mTimeScale
The time scale.
Definition: DateTime.h:822
const TimeFormat * mStringFormat
The string format.
Definition: DateTime.h:840
~TimeFormat()
Destructor.
Definition: DateTime.h:182
TimeType(const std::string &name)
Constructor.
Definition: DateTime.h:130
~TimeOrigin()
Destructor.
Definition: DateTime.h:72
static const TimeType TIME_STAMP
The time stamp.
Definition: DateTime.h:108
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
static const TimeFormat LOCAL_DATE_AND_TIME_FORMAT
LOCAL_DATE_AND_TIME_FORMAT: 04-18-08 13:22:50.
Definition: DateTime.h:149
const TimeType * mTimeType
Type of the time.
Definition: DateTime.h:838
static const TimeFormat LOCAL_DATE_FORMAT
LOCAL_DATE_FORMAT: 04/18/08.
Definition: DateTime.h:152
static const TimeType CLOCK_TIME
The clock time.
Definition: DateTime.h:100
#define DECLARE_ENUM(EnumType)
Helper macros used to create the static data and methods needed to enumerate an EnumerationString.
static const TimeFormat CLOCK_TIME_12_HOUR_FORMAT
CLOCK_TIME_12_HOUR_FORMAT: 01:22:50 PM.
Definition: DateTime.h:155
This class represents a type-safe EnumerationString pattern.
A class that represents date time utility.
float mGMTOffset
The GMT offset.
Definition: DateTime.h:820
static const TimeOrigin LOCAL_TIME
The local time.
Definition: DateTime.h:61
static const TimeFormat CALENDAR_DATE_FORMAT
CALENDAR_DATE_FORMAT: 2008-04-18.
Definition: DateTime.h:164
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
static const TimeFormat WEEK_DATE_FORMAT
WEEK_DATE_FORMAT: 2008-W15-5.
Definition: DateTime.h:170
static const TimeOrigin GMT_TIME
The GMT time.
Definition: DateTime.h:63
static const TimeFormat CLOCK_TIME_24_HOUR_FORMAT
CLOCK_TIME_24_HOUR_FORMAT: 13:22:50.
Definition: DateTime.h:158
unsigned mYears
Store parts of time.
Definition: DateTime.h:833
static const TimeType TIME_TYPE_OTHER
The time type other.
Definition: DateTime.h:110
static const TimeFormat LEXICAL_DATE_FORMAT
LEXICAL_DATE_FORMAT: April 18, 2008.
Definition: DateTime.h:161
const TimeOrigin * mTimeOrigin
The time origin.
Definition: DateTime.h:836
~TimeType()
Destructor.
Definition: DateTime.h:119
TimeFormat(const std::string &name)
Constructor.
Definition: DateTime.h:193
double mFractionalSeconds
The fractional in seconds.
Definition: DateTime.h:824
static const TimeType SIMULATION_TIME
The simulation time.
Definition: DateTime.h:102
static const TimeType SCENARIO_TIME
The scenario time.
Definition: DateTime.h:104
static const TimeFormat ORDINAL_DATE_FORMAT
ORDINAL_DATE_FORMAT: 2008-109.
Definition: DateTime.h:167