1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/types/date.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/common.hpp" |
12 | |
13 | namespace duckdb { |
14 | |
15 | //! The Date class is a static class that holds helper functions for the Date |
16 | //! type. |
17 | class Date { |
18 | public: |
19 | //! Convert a string in the format "YYYY-MM-DD" to a date object |
20 | static date_t FromString(string str, bool strict = false); |
21 | //! Convert a string in the format "YYYY-MM-DD" to a date object |
22 | static date_t FromCString(const char *str, bool strict = false); |
23 | //! Convert a date object to a string in the format "YYYY-MM-DD" |
24 | static string ToString(date_t date); |
25 | |
26 | //! Create a string "YYYY-MM-DD" from a specified (year, month, day) |
27 | //! combination |
28 | static string Format(int32_t year, int32_t month, int32_t day); |
29 | |
30 | //! Extract the year, month and day from a given date object |
31 | static void Convert(date_t date, int32_t &out_year, int32_t &out_month, int32_t &out_day); |
32 | //! Create a Date object from a specified (year, month, day) combination |
33 | static date_t FromDate(int32_t year, int32_t month, int32_t day); |
34 | |
35 | //! Returns true if (year) is a leap year, and false otherwise |
36 | static bool IsLeapYear(int32_t year); |
37 | |
38 | //! Returns true if the specified (year, month, day) combination is a valid |
39 | //! date |
40 | static bool IsValidDay(int32_t year, int32_t month, int32_t day); |
41 | |
42 | //! Extract the epoch from the date (seconds since 1970-01-01) |
43 | static int64_t Epoch(date_t date); |
44 | //! Convert the epoch (seconds since 1970-01-01) to a date_t |
45 | static date_t EpochToDate(int64_t epoch); |
46 | //! Extract year of a date entry |
47 | static int32_t (date_t date); |
48 | //! Extract month of a date entry |
49 | static int32_t (date_t date); |
50 | //! Extract day of a date entry |
51 | static int32_t (date_t date); |
52 | //! Extract the day of the week (1-7) |
53 | static int32_t (date_t date); |
54 | //! Extract the day of the year |
55 | static int32_t (date_t date); |
56 | //! Extract the week number |
57 | static int32_t (date_t date); |
58 | //! Returns the date of the monday of the current week. |
59 | static date_t GetMondayOfCurrentWeek(date_t date); |
60 | }; |
61 | } // namespace duckdb |
62 | |