1#include "duckdb/common/enums/date_part_specifier.hpp"
2#include "duckdb/common/string_util.hpp"
3
4namespace duckdb {
5
6bool TryGetDatePartSpecifier(const string &specifier_p, DatePartSpecifier &result) {
7 auto specifier = StringUtil::Lower(str: specifier_p);
8 if (specifier == "year" || specifier == "yr" || specifier == "y" || specifier == "years" || specifier == "yrs") {
9 result = DatePartSpecifier::YEAR;
10 } else if (specifier == "month" || specifier == "mon" || specifier == "months" || specifier == "mons") {
11 result = DatePartSpecifier::MONTH;
12 } else if (specifier == "day" || specifier == "days" || specifier == "d" || specifier == "dayofmonth") {
13 result = DatePartSpecifier::DAY;
14 } else if (specifier == "decade" || specifier == "dec" || specifier == "decades" || specifier == "decs") {
15 result = DatePartSpecifier::DECADE;
16 } else if (specifier == "century" || specifier == "cent" || specifier == "centuries" || specifier == "c") {
17 result = DatePartSpecifier::CENTURY;
18 } else if (specifier == "millennium" || specifier == "mil" || specifier == "millenniums" ||
19 specifier == "millennia" || specifier == "mils" || specifier == "millenium") {
20 result = DatePartSpecifier::MILLENNIUM;
21 } else if (specifier == "microseconds" || specifier == "microsecond" || specifier == "us" || specifier == "usec" ||
22 specifier == "usecs" || specifier == "usecond" || specifier == "useconds") {
23 result = DatePartSpecifier::MICROSECONDS;
24 } else if (specifier == "milliseconds" || specifier == "millisecond" || specifier == "ms" || specifier == "msec" ||
25 specifier == "msecs" || specifier == "msecond" || specifier == "mseconds") {
26 result = DatePartSpecifier::MILLISECONDS;
27 } else if (specifier == "second" || specifier == "sec" || specifier == "seconds" || specifier == "secs" ||
28 specifier == "s") {
29 result = DatePartSpecifier::SECOND;
30 } else if (specifier == "minute" || specifier == "min" || specifier == "minutes" || specifier == "mins" ||
31 specifier == "m") {
32 result = DatePartSpecifier::MINUTE;
33 } else if (specifier == "hour" || specifier == "hr" || specifier == "hours" || specifier == "hrs" ||
34 specifier == "h") {
35 result = DatePartSpecifier::HOUR;
36 } else if (specifier == "epoch") {
37 // seconds since 1970-01-01
38 result = DatePartSpecifier::EPOCH;
39 } else if (specifier == "dow" || specifier == "dayofweek" || specifier == "weekday") {
40 // day of the week (Sunday = 0, Saturday = 6)
41 result = DatePartSpecifier::DOW;
42 } else if (specifier == "isodow") {
43 // isodow (Monday = 1, Sunday = 7)
44 result = DatePartSpecifier::ISODOW;
45 } else if (specifier == "week" || specifier == "weeks" || specifier == "w" || specifier == "weekofyear") {
46 // ISO week number
47 result = DatePartSpecifier::WEEK;
48 } else if (specifier == "doy" || specifier == "dayofyear") {
49 // day of the year (1-365/366)
50 result = DatePartSpecifier::DOY;
51 } else if (specifier == "quarter" || specifier == "quarters") {
52 // quarter of the year (1-4)
53 result = DatePartSpecifier::QUARTER;
54 } else if (specifier == "yearweek") {
55 // Combined isoyear and isoweek YYYYWW
56 result = DatePartSpecifier::YEARWEEK;
57 } else if (specifier == "isoyear") {
58 // ISO year (first week of the year may be in previous year)
59 result = DatePartSpecifier::ISOYEAR;
60 } else if (specifier == "era") {
61 result = DatePartSpecifier::ERA;
62 } else if (specifier == "timezone") {
63 result = DatePartSpecifier::TIMEZONE;
64 } else if (specifier == "timezone_hour") {
65 result = DatePartSpecifier::TIMEZONE_HOUR;
66 } else if (specifier == "timezone_minute") {
67 result = DatePartSpecifier::TIMEZONE_MINUTE;
68 } else {
69 return false;
70 }
71 return true;
72}
73
74DatePartSpecifier GetDatePartSpecifier(const string &specifier) {
75 DatePartSpecifier result;
76 if (!TryGetDatePartSpecifier(specifier_p: specifier, result)) {
77 throw ConversionException("extract specifier \"%s\" not recognized", specifier);
78 }
79 return result;
80}
81
82} // namespace duckdb
83