|
| 1 | +#pragma once |
| 2 | +/* |
| 3 | +* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | +* |
| 5 | +* Licensed under the Apache License, Version 2.0 (the "License"). |
| 6 | +* You may not use this file except in compliance with the License. |
| 7 | +* A copy of the License is located at |
| 8 | +* |
| 9 | +* http://aws.amazon.com/apache2.0 |
| 10 | +* |
| 11 | +* or in the "license" file accompanying this file. This file is distributed |
| 12 | +* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 13 | +* express or implied. See the License for the specific language governing |
| 14 | +* permissions and limitations under the License. |
| 15 | +*/ |
| 16 | +#include <aws/crt/Exports.h> |
| 17 | + |
| 18 | +#include <aws/crt/Types.h> |
| 19 | + |
| 20 | +#include <aws/common/date_time.h> |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | + |
| 24 | +namespace Aws |
| 25 | +{ |
| 26 | + namespace Crt |
| 27 | + { |
| 28 | + enum class DateFormat |
| 29 | + { |
| 30 | + RFC822 = AWS_DATE_FORMAT_RFC822, |
| 31 | + ISO_8601 = AWS_DATE_FORMAT_ISO_8601, |
| 32 | + AutoDetect = AWS_DATE_FORMAT_AUTO_DETECT, |
| 33 | + }; |
| 34 | + |
| 35 | + enum class Month |
| 36 | + { |
| 37 | + January = AWS_DATE_MONTH_JANUARY, |
| 38 | + February = AWS_DATE_MONTH_FEBRUARY, |
| 39 | + March = AWS_DATE_MONTH_MARCH, |
| 40 | + April = AWS_DATE_MONTH_APRIL, |
| 41 | + May = AWS_DATE_MONTH_MAY, |
| 42 | + June = AWS_DATE_MONTH_JUNE, |
| 43 | + July = AWS_DATE_MONTH_JULY, |
| 44 | + August = AWS_DATE_MONTH_AUGUST, |
| 45 | + September = AWS_DATE_MONTH_SEPTEMBER, |
| 46 | + October = AWS_DATE_MONTH_OCTOBER, |
| 47 | + November = AWS_DATE_MONTH_NOVEMBER, |
| 48 | + December = AWS_DATE_MONTH_DECEMBER, |
| 49 | + }; |
| 50 | + |
| 51 | + enum class DayOfWeek |
| 52 | + { |
| 53 | + Sunday = AWS_DATE_DAY_OF_WEEK_SUNDAY, |
| 54 | + Monday = AWS_DATE_DAY_OF_WEEK_MONDAY, |
| 55 | + Tuesday = AWS_DATE_DAY_OF_WEEK_TUESDAY, |
| 56 | + Wednesday = AWS_DATE_DAY_OF_WEEK_WEDNESDAY, |
| 57 | + Thursday = AWS_DATE_DAY_OF_WEEK_THURSDAY, |
| 58 | + Friday = AWS_DATE_DAY_OF_WEEK_FRIDAY, |
| 59 | + Saturday = AWS_DATE_DAY_OF_WEEK_SATURDAY, |
| 60 | + }; |
| 61 | + |
| 62 | + class AWS_CRT_CPP_API DateTime final |
| 63 | + { |
| 64 | + public: |
| 65 | + /** |
| 66 | + * Initializes time point to epoch |
| 67 | + */ |
| 68 | + DateTime() noexcept; |
| 69 | + |
| 70 | + /** |
| 71 | + * Initializes time point to any other arbirtrary timepoint |
| 72 | + */ |
| 73 | + DateTime(const std::chrono::system_clock::time_point& timepointToAssign) noexcept; |
| 74 | + |
| 75 | + /** |
| 76 | + * Initializes time point to millis Since epoch |
| 77 | + */ |
| 78 | + DateTime(uint64_t millisSinceEpoch) noexcept; |
| 79 | + |
| 80 | + /** |
| 81 | + * Initializes time point to epoch time in seconds.millis |
| 82 | + */ |
| 83 | + DateTime(double epoch_millis) noexcept; |
| 84 | + |
| 85 | + /** |
| 86 | + * Initializes time point to value represented by timestamp and format. |
| 87 | + */ |
| 88 | + DateTime(const char* timestamp, DateFormat format) noexcept; |
| 89 | + |
| 90 | + bool operator == (const DateTime& other) const noexcept; |
| 91 | + bool operator < (const DateTime& other) const noexcept; |
| 92 | + bool operator > (const DateTime& other) const noexcept; |
| 93 | + bool operator != (const DateTime& other) const noexcept; |
| 94 | + bool operator <= (const DateTime& other) const noexcept; |
| 95 | + bool operator >= (const DateTime& other) const noexcept; |
| 96 | + |
| 97 | + DateTime operator+(const std::chrono::milliseconds& a) const noexcept; |
| 98 | + DateTime operator-(const std::chrono::milliseconds& a) const noexcept; |
| 99 | + |
| 100 | + /** |
| 101 | + * Assign from seconds.millis since epoch. |
| 102 | + */ |
| 103 | + DateTime& operator=(double secondsSinceEpoch) noexcept; |
| 104 | + |
| 105 | + /** |
| 106 | + * Assign from millis since epoch. |
| 107 | + */ |
| 108 | + DateTime& operator=(uint64_t millisSinceEpoch) noexcept; |
| 109 | + |
| 110 | + /** |
| 111 | + * Assign from another time_point |
| 112 | + */ |
| 113 | + DateTime& operator=(const std::chrono::system_clock::time_point& timepointToAssign) noexcept; |
| 114 | + |
| 115 | + /** |
| 116 | + * Assign from an ISO8601 or RFC822 formatted string |
| 117 | + */ |
| 118 | + DateTime& operator=(const char* timestamp) noexcept; |
| 119 | + |
| 120 | + explicit operator bool() const noexcept; |
| 121 | + int GetLastError() const noexcept; |
| 122 | + |
| 123 | + /** |
| 124 | + * Convert dateTime to local time string using predefined format. |
| 125 | + */ |
| 126 | + bool ToLocalTimeString(DateFormat format, ByteBuf& outputBuf) const noexcept; |
| 127 | + |
| 128 | + /** |
| 129 | + * Convert dateTime to GMT time string using predefined format. |
| 130 | + */ |
| 131 | + bool ToGmtString(DateFormat format, ByteBuf& outputBuf) const noexcept; |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the representation of this datetime as seconds.milliseconds since epoch |
| 135 | + */ |
| 136 | + double SecondsWithMSPrecision() const noexcept; |
| 137 | + |
| 138 | + /** |
| 139 | + * Milliseconds since epoch of this datetime. |
| 140 | + */ |
| 141 | + uint64_t Millis() const noexcept; |
| 142 | + |
| 143 | + /** |
| 144 | + * In the likely case this class doesn't do everything you need to do, here's a copy of the time_point structure. Have fun. |
| 145 | + */ |
| 146 | + std::chrono::system_clock::time_point UnderlyingTimestamp() const noexcept; |
| 147 | + |
| 148 | + /** |
| 149 | + * Get the Year portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 150 | + */ |
| 151 | + uint16_t GetYear(bool localTime = false) const noexcept; |
| 152 | + |
| 153 | + /** |
| 154 | + * Get the Month portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 155 | + */ |
| 156 | + Month GetMonth(bool localTime = false) const noexcept; |
| 157 | + |
| 158 | + /** |
| 159 | + * Get the Day of the Month portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 160 | + */ |
| 161 | + uint8_t GetDay(bool localTime = false) const noexcept; |
| 162 | + |
| 163 | + /** |
| 164 | + * Get the Day of the Week portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 165 | + */ |
| 166 | + DayOfWeek GetDayOfWeek(bool localTime = false) const noexcept; |
| 167 | + |
| 168 | + /** |
| 169 | + * Get the Hour portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 170 | + */ |
| 171 | + uint8_t GetHour(bool localTime = false) const noexcept; |
| 172 | + |
| 173 | + /** |
| 174 | + * Get the Minute portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 175 | + */ |
| 176 | + uint8_t GetMinute(bool localTime = false) const noexcept; |
| 177 | + |
| 178 | + /** |
| 179 | + * Get the Second portion of this dateTime. localTime if true, return local time, otherwise return UTC |
| 180 | + */ |
| 181 | + uint8_t GetSecond(bool localTime = false) const noexcept; |
| 182 | + |
| 183 | + /** |
| 184 | + * Get whether or not this dateTime is in Daylight savings time. localTime if true, return local time, otherwise return UTC |
| 185 | + */ |
| 186 | + bool IsDST(bool localTime = false) const noexcept; |
| 187 | + |
| 188 | + /** |
| 189 | + * Get an instance of DateTime representing this very instant. |
| 190 | + */ |
| 191 | + static DateTime Now() noexcept; |
| 192 | + |
| 193 | + /** |
| 194 | + * Computes the difference between two DateTime instances and returns the difference |
| 195 | + * in milliseconds. |
| 196 | + */ |
| 197 | + std::chrono::milliseconds operator - (const DateTime& other) const noexcept; |
| 198 | + |
| 199 | + private: |
| 200 | + aws_date_time m_date_time; |
| 201 | + bool m_good; |
| 202 | + }; |
| 203 | + } |
| 204 | +} |
0 commit comments