Ocean
DateTime.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_DATE_TIME_H
9 #define META_OCEAN_BASE_DATE_TIME_H
10 
11 #include "ocean/base/Base.h"
12 
13 namespace Ocean
14 {
15 
16 /**
17  * This class provides basic functionalities for date and time handling.<br>
18  * All functions are available for UTC and local time.
19  * @ingroup base
20  */
21 class OCEAN_BASE_EXPORT DateTime
22 {
23  public:
24 
25  /**
26  * Returns the seconds since 1970.01.01 00::00::00 to current UTC time
27  * @return Seconds
28  */
29  static double timestamp();
30 
31  /**
32  * Returns the microseconds since 1970.01.01 00::00::00 to current UTC time
33  * @return Microseconds
34  */
35  static long long microseconds();
36 
37  /**
38  * Returns the string of the UTC date and time.
39  * @param addMilliseconds True, to add the milliseconds to the string
40  * @return Date and time string
41  */
42  static std::string string(const bool addMilliseconds = false);
43 
44  /**
45  * Returns the string of a given unix timestamp.
46  * @param timestamp The unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
47  * @param addMilliseconds True, to add the milliseconds to the string
48  * @return Date and time string
49  */
50  static std::string string(const double timestamp, const bool addMilliseconds = false);
51 
52  /**
53  * Returns the string of the UTC date.
54  * @param delimiter The delimiter to be used
55  * @return Date string, with pattern YYYY.MM.DD, if delimiter is '.'
56  */
57  static std::string stringDate(const char delimiter = '.');
58 
59  /**
60  * Returns the string of a given unix timestamp.
61  * @param timestamp The unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
62  * @param nameMonths True, to receive a date with named months; False, to receive a date with digits only (but with constant length)
63  * @return Date string, with pattern YYYY.MM.DD (2016.08.05) for nameMonths=false; or MMMMMMMMMM D/DD, YYYY (May 5, 2016) for nameMonths=true
64  */
65  static std::string stringDate(const double timestamp, const bool nameMonths = false);
66 
67  /**
68  * Returns the string of the UTC time.
69  * The resulting string has the following format: HH-MM-SS, or HH-MM-SS-mmmm if the milliseconds are added.
70  * @param addMilliseconds True, to add the milliseconds to the string
71  * @param delimiter The delimiter to be used
72  * @return Time string of UTC time
73  */
74  static std::string stringTime(const bool addMilliseconds = false, const char delimiter = '-');
75 
76  /**
77  * Returns the string of a given unix timestamp.
78  * The resulting string has the following format: HH-MM-SS, or HH-MM-SS-mmmm if the milliseconds are added.
79  * @param timestamp The unix timestamp (seconds since 1970.01.01 00:00:00) to be converted, with range [0, infinity)
80  * @param addMilliseconds True, to add the milliseconds to the string
81  * @param delimiter The delimiter to be used
82  * @return Time string of the UTC time
83  */
84  static std::string stringTime(const double timestamp, const bool addMilliseconds = false, const char delimiter = '-');
85 
86  /**
87  * Returns the seconds since 1970.01.01 00::00::00 to current local time
88  * @return Seconds
89  */
90  static double localTimestamp();
91 
92  /**
93  * Returns the microseconds since 1970.01.01 00::00::00 to current local time
94  * @return Microseconds
95  */
96  static long long localMicroseconds();
97 
98  /**
99  * Returns the string of the local date and time.
100  * @return Date and time string
101  */
102  static std::string localString();
103 
104  /**
105  * Returns the string of the local date.
106  * @param delimiter The delimiter to be used
107  * @return Date string of local date, with format YYYY.MM.DD, if delimiter is '.'
108  */
109  static std::string localStringDate(const char delimiter = '.');
110 
111  /**
112  * Converts a date to the seconds since 1970.01.01 00:00:00.
113  * @param year The year, with range [1970, 2037)
114  * @param month The month, with range [1, 12]
115  * @param day The day, with range [1, 31]
116  * @param hour The hour, with range [0, 23]
117  * @param minute The minute, with range [0, 59]
118  * @param second The second, with range [0, 59]
119  * @param millisecond The millisecond, with range [0, 999]
120  * @return The unix timestamp, -1 if the given date is invalid
121  */
122  static double date2timestamp(const unsigned int year, const unsigned int month, const unsigned int day, const unsigned int hour, const unsigned int minute, const unsigned int second, const unsigned int millisecond = 0u);
123 
124  /**
125  * Converts a unix timestamp (the seconds since 1970.01.01 00:00:00) to a date.
126  * @param timestamp The unix timestamp to convert
127  * @param year The resulting year, with range [1970, 2037)
128  * @param month The resulting month, with range [1, 12]
129  * @param day The resulting date, with range [1, 31]
130  * @param hour The resulting hour, with range [0, 23]
131  * @param minute The resulting minute, with range [0, 59]
132  * @param second The resulting second, with range [0, 59]
133  * @param millisecond Optional resulting millisecond, with range [0, 999]
134  */
135  static void timestamp2date(const double timestamp, unsigned int& year, unsigned int& month, unsigned int& day, unsigned int& hour, unsigned int& minute, unsigned int& second, unsigned int* millisecond = nullptr);
136 
137  /**
138  * Converts value of seconds to a time string showing the hours, minutes and seconds.
139  * The resulting string has the following format: HH:MM:SS, or HH:MM:SS:mmmm if the milliseconds are added; or H hours, M minutes, S seconds or H hours, M minutes, S seconds, m milliseconds if the description is added.
140  * @param totalSeconds The number of seconds to convert, with range [0, infinity)
141  * @param addDescription True, to add the description text to the string
142  * @param addMilliseconds True, to add the milliseconds to the string
143  * @return Time string of local time
144  */
145  static std::string seconds2string(const double totalSeconds, const bool addDescription, const bool addMilliseconds = false);
146 
147  /**
148  * Returns the string of the local time.
149  * The resulting string has the following format: HH:MM:SS, or HH:MM:SS:mmmm if the milliseconds are added.
150  * @param addMilliseconds True, to add the milliseconds to the string
151  * @param delimiter The delimiter to be used
152  * @return Time string of local time
153  */
154  static std::string localStringTime(const bool addMilliseconds = false, const char delimiter = ':');
155 
156  /**
157  * Returns whether a given year is a leap year.
158  * @param year The year to check
159  */
160  static inline unsigned int isLeapYear(const unsigned int year);
161 
162  /**
163  * Returns whether a given date is a valid date (e.g., in no leap years the 29th February is an invalid date)
164  * @param year The year of the date, with range [0, infinity)
165  * @param month The month of the date, with range [1, 12]
166  * @param day The day of the date, with range [1, 31]
167  * @return True, if so
168  */
169  static bool isDateValid(const unsigned int year, const unsigned int month, const unsigned int day);
170 };
171 
172 inline unsigned int DateTime::isLeapYear(const unsigned int year)
173 {
174  return (year % 4u) == 0u && ((year % 100u != 0u) || (year % 400u == 0u));
175 }
176 
177 }
178 
179 #endif // META_OCEAN_BASE_DATE_TIME_H
This class provides basic functionalities for date and time handling.
Definition: DateTime.h:22
static std::string stringDate(const char delimiter='.')
Returns the string of the UTC date.
static bool isDateValid(const unsigned int year, const unsigned int month, const unsigned int day)
Returns whether a given date is a valid date (e.g., in no leap years the 29th February is an invalid ...
static long long microseconds()
Returns the microseconds since 1970.01.01 00::00::00 to current UTC time.
static std::string string(const bool addMilliseconds=false)
Returns the string of the UTC date and time.
static double timestamp()
Returns the seconds since 1970.01.01 00::00::00 to current UTC time.
static std::string stringTime(const bool addMilliseconds=false, const char delimiter='-')
Returns the string of the UTC time.
static std::string string(const double timestamp, const bool addMilliseconds=false)
Returns the string of a given unix timestamp.
static std::string localStringTime(const bool addMilliseconds=false, const char delimiter=':')
Returns the string of the local time.
static void timestamp2date(const double timestamp, unsigned int &year, unsigned int &month, unsigned int &day, unsigned int &hour, unsigned int &minute, unsigned int &second, unsigned int *millisecond=nullptr)
Converts a unix timestamp (the seconds since 1970.01.01 00:00:00) to a date.
static std::string stringDate(const double timestamp, const bool nameMonths=false)
Returns the string of a given unix timestamp.
static unsigned int isLeapYear(const unsigned int year)
Returns whether a given year is a leap year.
Definition: DateTime.h:172
static long long localMicroseconds()
Returns the microseconds since 1970.01.01 00::00::00 to current local time.
static std::string localString()
Returns the string of the local date and time.
static double localTimestamp()
Returns the seconds since 1970.01.01 00::00::00 to current local time.
static std::string stringTime(const double timestamp, const bool addMilliseconds=false, const char delimiter='-')
Returns the string of a given unix timestamp.
static std::string seconds2string(const double totalSeconds, const bool addDescription, const bool addMilliseconds=false)
Converts value of seconds to a time string showing the hours, minutes and seconds.
static std::string localStringDate(const char delimiter='.')
Returns the string of the local date.
static double date2timestamp(const unsigned int year, const unsigned int month, const unsigned int day, const unsigned int hour, const unsigned int minute, const unsigned int second, const unsigned int millisecond=0u)
Converts a date to the seconds since 1970.01.01 00:00:00.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15