## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsimportjson# noqa: F401importtime# noqa: F401importuuid# noqa: F401fromdatetimeimportdatetime,timedeltafromrandomimportrandom# noqa: F401fromtypingimportTYPE_CHECKING,Anyimportdateutil# noqa: F401frombabelimportLocalefrombabel.datesimportLC_TIME,format_datetimeimportairflow.utils.yamlasyaml# noqa: F401fromairflow.utils.deprecation_toolsimportadd_deprecated_classesifTYPE_CHECKING:frompendulumimportDateTime__deprecated_classes={"hive":{"closest_ds_partition":"airflow.providers.apache.hive.macros.hive.closest_ds_partition","max_partition":"airflow.providers.apache.hive.macros.hive.max_partition",},}add_deprecated_classes(__deprecated_classes,__name__)
[docs]defds_add(ds:str,days:int)->str:""" Add or subtract days from a YYYY-MM-DD. :param ds: anchor date in ``YYYY-MM-DD`` format to add to :param days: number of days to add to the ds, you can use negative values >>> ds_add("2015-01-01", 5) '2015-01-06' >>> ds_add("2015-01-06", -5) '2015-01-01' """ifnotdays:returnstr(ds)dt=datetime.strptime(str(ds),"%Y-%m-%d")+timedelta(days=days)returndt.strftime("%Y-%m-%d")
[docs]defds_format(ds:str,input_format:str,output_format:str)->str:""" Output datetime string in a given format. :param ds: Input string which contains a date. :param input_format: Input string format (e.g., '%Y-%m-%d'). :param output_format: Output string format (e.g., '%Y-%m-%d'). >>> ds_format("2015-01-01", "%Y-%m-%d", "%m-%d-%y") '01-01-15' >>> ds_format("1/5/2015", "%m/%d/%Y", "%Y-%m-%d") '2015-01-05' >>> ds_format("12/07/2024", "%d/%m/%Y", "%A %d %B %Y", "en_US") 'Friday 12 July 2024' """returndatetime.strptime(str(ds),input_format).strftime(output_format)
[docs]defds_format_locale(ds:str,input_format:str,output_format:str,locale:Locale|str|None=None)->str:""" Output localized datetime string in a given Babel format. :param ds: Input string which contains a date. :param input_format: Input string format (e.g., '%Y-%m-%d'). :param output_format: Output string Babel format (e.g., `yyyy-MM-dd`). :param locale: Locale used to format the output string (e.g., 'en_US'). If locale not specified, default LC_TIME will be used and if that's also not available, 'en_US' will be used. >>> ds_format("2015-01-01", "%Y-%m-%d", "MM-dd-yy") '01-01-15' >>> ds_format("1/5/2015", "%m/%d/%Y", "yyyy-MM-dd") '2015-01-05' >>> ds_format("12/07/2024", "%d/%m/%Y", "EEEE dd MMMM yyyy", "en_US") 'Friday 12 July 2024' .. versionadded:: 2.10.0 """returnformat_datetime(datetime.strptime(str(ds),input_format),format=output_format,locale=localeorLC_TIMEorLocale("en_US"),)
[docs]defdatetime_diff_for_humans(dt:Any,since:DateTime|None=None)->str:""" Return a human-readable/approximate difference between datetimes. When only one datetime is provided, the comparison will be based on now. :param dt: The datetime to display the diff for :param since: When to display the date from. If ``None`` then the diff is between ``dt`` and now. """importpendulumreturnpendulum.instance(dt).diff_for_humans(since)