From 29869385554b6816699b3ac890c850b8f52d0eda Mon Sep 17 00:00:00 2001 From: Pierre MOREAU Date: Wed, 21 Jun 2023 16:47:17 +0200 Subject: [PATCH 1/2] deploy new bigfun --- .../nb_workdays_between_datetimes.yaml | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 bigfunctions/nb_workdays_between_datetimes.yaml diff --git a/bigfunctions/nb_workdays_between_datetimes.yaml b/bigfunctions/nb_workdays_between_datetimes.yaml new file mode 100644 index 000000000..06733c38f --- /dev/null +++ b/bigfunctions/nb_workdays_between_datetimes.yaml @@ -0,0 +1,54 @@ +type: function_sql +category: transform_date +author: + name: Pierre Moreau + url: https://www.linkedin.com/in/pierre-moreau-in85and44/ + avatar_url: "https://media.licdn.com/dms/image/D4E03AQHUppkwV7fwUw/profile-displayphoto-shrink_800_800/0/1684960816011?e=1692835200&v=beta&t=Bi3uOXaJYJtonTIMExONdiBjVSL_N7llloF7ZWLvAFI" +description: | + Return number of workdays (in float64 value) between two datetime values + + - `country_code` must be among `[AO, AR, AW, AU, AT, AZ, BD, BY, BE, BO, BW, BR, BG, BI, CA, CL, CN, CO, HR, CU, CW, CY, CZ, DK, DJ, DO, EG, EE, ET, FI, FR, GE, DE, GR, HN, HK, HU, IS, IN, IE, IL, IT, JM, JP, KZ, KE, KR, LV, LS, LT, LU, MG, MW, MY, MT, MX, MD, MA, MZ, NA, NL, NZ, NI, NG, MK, NO, PY, PE, PL, PT, RO, RU, SA, RS, SG, SK, SI, ZA, ES, SZ, SE, CH, TW, TN, TR, UA, AE, GB, US, UY, UZ, VE, VN, ZM, ZW]` + - Holiday dates come from `python-holidays`. + - `weekend_days` must be a string like `'SATURDAY|SUNDAY'` +arguments: + - name: start_datetime + type: datetime + - name: end_datetime + type: datetime + - name: country_code + type: string + - name: weekend_days + type: string +output: + name: nb_workdays + type: float64 +examples: + - description: "" + arguments: + - "datetime('2023-05-01 12:00:00')" + - "datetime('2023-05-09 21:00:00')" + - "'FR'" + - "'SATURDAY|SUNDAY'" + output: "4.875" +code: | + ( + + with dates as ( + select date, + case when date = date(start_datetime) then 1 - (datetime_diff(start_datetime, datetime_trunc(start_datetime, day), second) / 86400) + when date = date(end_datetime) then (datetime_diff(end_datetime, datetime_trunc(end_datetime, day), second) / 86400) + else 1 + end as day_ratio + from bigfunctions.eu.dates + where country = country_code + and date between date(start_datetime) and date(end_datetime) + and is_public_holiday is false + and not regexp_contains(upper(format_date('%A', date)), upper(weekend_days)) + ) + + select case when date(start_datetime) = date(end_datetime) then (datetime_diff(end_datetime, start_datetime, second) / 86400) * if(sum(day_ratio) is not null, 1, 0) + else sum(day_ratio) + end as n_days + from dates + + ) From b17cbb763d5422e7a8317921e05c53f5f5c144e4 Mon Sep 17 00:00:00 2001 From: Pierre MOREAU Date: Wed, 21 Jun 2023 17:06:15 +0200 Subject: [PATCH 2/2] deploy new bigfun --- bigfunctions/nb_workdays_between_dates.yaml | 53 +++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 bigfunctions/nb_workdays_between_dates.yaml diff --git a/bigfunctions/nb_workdays_between_dates.yaml b/bigfunctions/nb_workdays_between_dates.yaml new file mode 100644 index 000000000..f75fe4b2d --- /dev/null +++ b/bigfunctions/nb_workdays_between_dates.yaml @@ -0,0 +1,53 @@ +type: function_sql +category: transform_date +author: + name: Pierre Moreau + url: https://www.linkedin.com/in/pierre-moreau-in85and44/ + avatar_url: "https://media.licdn.com/dms/image/D4E03AQHUppkwV7fwUw/profile-displayphoto-shrink_800_800/0/1684960816011?e=1692835200&v=beta&t=Bi3uOXaJYJtonTIMExONdiBjVSL_N7llloF7ZWLvAFI" +description: | + Return number of workdays (in int64 value) between two date values + + - `country_code` must be among `[AO, AR, AW, AU, AT, AZ, BD, BY, BE, BO, BW, BR, BG, BI, CA, CL, CN, CO, HR, CU, CW, CY, CZ, DK, DJ, DO, EG, EE, ET, FI, FR, GE, DE, GR, HN, HK, HU, IS, IN, IE, IL, IT, JM, JP, KZ, KE, KR, LV, LS, LT, LU, MG, MW, MY, MT, MX, MD, MA, MZ, NA, NL, NZ, NI, NG, MK, NO, PY, PE, PL, PT, RO, RU, SA, RS, SG, SK, SI, ZA, ES, SZ, SE, CH, TW, TN, TR, UA, AE, GB, US, UY, UZ, VE, VN, ZM, ZW]` + - Holiday dates come from `python-holidays`. + - `weekend_days` must be a string like `'SATURDAY|SUNDAY'` +arguments: + - name: start_date + type: date + - name: end_date + type: date + - name: country_code + type: string + - name: weekend_days + type: string +output: + name: nb_workdays + type: int64 +examples: + - description: "" + arguments: + - "date('2023-05-01')" + - "date('2023-05-09')" + - "'FR'" + - "'SATURDAY|SUNDAY'" + output: "5" +code: | + ( + + with dates as ( + select date, + case when date = date(start_date) then 0 + else 1 + end as day_ratio + from bigfunctions.eu.dates + where country = country_code + and date between date(start_date) and date(end_date) + and is_public_holiday is false + and not regexp_contains(upper(format_date('%A', date)), upper(weekend_days)) + ) + + select case when date(start_date) = date(end_date) then 0 + else sum(day_ratio) + end as n_days + from dates + + ) \ No newline at end of file