AWS SDK for C++  0.12.9
AWS SDK for C++
DateTime.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
20 #include <iomanip>
21 #include <ctime>
22 #include <mutex>
23 
24 namespace Aws
25 {
26  namespace Utils
27  {
28  enum class DateFormat
29  {
30  RFC822, //for http headers
31  ISO_8601 //for query and xml payloads
32  };
33 
34  enum class Month
35  {
36  January = 0,
37  February,
38  March,
39  April,
40  May,
41  June,
42  July,
43  August,
44  September,
45  October,
46  November,
47  December
48  };
49 
50  enum class DayOfWeek
51  {
52  Sunday = 0,
53  Monday,
54  Tuesday,
55  Wednesday,
56  Thursday,
57  Friday,
58  Saturday
59  };
60 
65  {
66  public:
70  DateTime();
71 
75  DateTime(const std::chrono::system_clock::time_point& timepointToAssign);
76 
80  DateTime(int64_t millisSinceEpoch);
81 
85  DateTime(double epoch_millis);
86 
90  DateTime(const Aws::String& timestamp, DateFormat format);
91 
95  DateTime(const char* timestamp, DateFormat format);
96 
97  bool operator == (const DateTime& other);
98  bool operator < (const DateTime& other);
99  bool operator > (const DateTime& other);
100  bool operator != (const DateTime& other);
101  bool operator <= (const DateTime& other);
102  bool operator >= (const DateTime& other);
103 
107  DateTime& operator=(double secondsSinceEpoch);
108 
112  DateTime& operator=(int64_t millisSinceEpoch);
113 
117  DateTime& operator=(const std::chrono::system_clock::time_point& timepointToAssign);
118 
122  inline bool WasParseSuccessful() { return m_valid; }
123 
127  Aws::String ToLocalTimeString(DateFormat format) const;
128 
132  Aws::String ToLocalTimeString(const char* formatStr) const;
133 
137  Aws::String ToGmtString(DateFormat format) const;
138 
142  Aws::String ToGmtString(const char* formatStr) const;
143 
147  double SecondsWithMSPrecision() const;
148 
152  int64_t Millis() const;
153 
157  std::chrono::system_clock::time_point UnderlyingTimestamp() const;
158 
162  int GetYear(bool localTime = false) const;
163 
167  Month GetMonth(bool localTime = false) const;
168 
172  int GetDay(bool localTime = false) const;
173 
177  DayOfWeek GetDayOfWeek(bool localTime = false) const;
178 
182  int GetHour(bool localTime = false) const;
183 
187  int GetMinute(bool localTime = false) const;
188 
192  int GetSecond(bool localTime = false) const;
193 
197  bool IsDST(bool localTime = false) const;
198 
202  static DateTime Now();
203 
207  static int64_t CurrentTimeMillis();
208 
212  static Aws::String CalculateLocalTimestampAsString(const char* formatStr);
213 
217  static Aws::String CalculateGmtTimestampAsString(const char* formatStr);
218 
222  static int CalculateCurrentHour();
223 
227  static double ComputeCurrentTimestampInAmazonFormat();
228 
229  /*
230  * A platform-agnostic implementation of the timegm function from gnu extensions
231  */
232  static time_t TimeGM(tm* const t);
233 
234  private:
235  std::chrono::system_clock::time_point m_time;
236  bool m_valid;
237 
238  void ConvertTimestampStringToTimePoint(const char* timestamp, DateFormat format);
239  tm GetTimeStruct(bool localTime) const;
240  tm ConvertTimestampToLocalTimeStruct() const;
241  tm ConvertTimestampToGmtStruct() const;
242  };
243 
244  } // namespace Utils
245 } // namespace Aws
bool WasParseSuccessful()
Definition: DateTime.h:122
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
#define AWS_CORE_API
Definition: Core_EXPORTS.h:30
JSON (JavaScript Object Notation).