__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. import json # noqa: F401
  20. import time # noqa: F401
  21. import uuid # noqa: F401
  22. from datetime import datetime, timedelta
  23. from random import random # noqa: F401
  24. from typing import TYPE_CHECKING, Any
  25. import dateutil # noqa: F401
  26. from babel import Locale
  27. from babel.dates import LC_TIME, format_datetime
  28. import airflow.utils.yaml as yaml # noqa: F401
  29. from airflow.utils.deprecation_tools import add_deprecated_classes
  30. if TYPE_CHECKING:
  31. from pendulum import DateTime
  32. __deprecated_classes = {
  33. "hive": {
  34. "closest_ds_partition": "airflow.providers.apache.hive.macros.hive.closest_ds_partition",
  35. "max_partition": "airflow.providers.apache.hive.macros.hive.max_partition",
  36. },
  37. }
  38. add_deprecated_classes(__deprecated_classes, __name__)
  39. def ds_add(ds: str, days: int) -> str:
  40. """
  41. Add or subtract days from a YYYY-MM-DD.
  42. :param ds: anchor date in ``YYYY-MM-DD`` format to add to
  43. :param days: number of days to add to the ds, you can use negative values
  44. >>> ds_add("2015-01-01", 5)
  45. '2015-01-06'
  46. >>> ds_add("2015-01-06", -5)
  47. '2015-01-01'
  48. """
  49. if not days:
  50. return str(ds)
  51. dt = datetime.strptime(str(ds), "%Y-%m-%d") + timedelta(days=days)
  52. return dt.strftime("%Y-%m-%d")
  53. def ds_format(ds: str, input_format: str, output_format: str) -> str:
  54. """
  55. Output datetime string in a given format.
  56. :param ds: Input string which contains a date.
  57. :param input_format: Input string format (e.g., '%Y-%m-%d').
  58. :param output_format: Output string format (e.g., '%Y-%m-%d').
  59. >>> ds_format("2015-01-01", "%Y-%m-%d", "%m-%d-%y")
  60. '01-01-15'
  61. >>> ds_format("1/5/2015", "%m/%d/%Y", "%Y-%m-%d")
  62. '2015-01-05'
  63. >>> ds_format("12/07/2024", "%d/%m/%Y", "%A %d %B %Y", "en_US")
  64. 'Friday 12 July 2024'
  65. """
  66. return datetime.strptime(str(ds), input_format).strftime(output_format)
  67. def ds_format_locale(
  68. ds: str, input_format: str, output_format: str, locale: Locale | str | None = None
  69. ) -> str:
  70. """
  71. Output localized datetime string in a given Babel format.
  72. :param ds: Input string which contains a date.
  73. :param input_format: Input string format (e.g., '%Y-%m-%d').
  74. :param output_format: Output string Babel format (e.g., `yyyy-MM-dd`).
  75. :param locale: Locale used to format the output string (e.g., 'en_US').
  76. If locale not specified, default LC_TIME will be used and if that's also not available,
  77. 'en_US' will be used.
  78. >>> ds_format("2015-01-01", "%Y-%m-%d", "MM-dd-yy")
  79. '01-01-15'
  80. >>> ds_format("1/5/2015", "%m/%d/%Y", "yyyy-MM-dd")
  81. '2015-01-05'
  82. >>> ds_format("12/07/2024", "%d/%m/%Y", "EEEE dd MMMM yyyy", "en_US")
  83. 'Friday 12 July 2024'
  84. .. versionadded:: 2.10.0
  85. """
  86. return format_datetime(
  87. datetime.strptime(str(ds), input_format),
  88. format=output_format,
  89. locale=locale or LC_TIME or Locale("en_US"),
  90. )
  91. def datetime_diff_for_humans(dt: Any, since: DateTime | None = None) -> str:
  92. """
  93. Return a human-readable/approximate difference between datetimes.
  94. When only one datetime is provided, the comparison will be based on now.
  95. :param dt: The datetime to display the diff for
  96. :param since: When to display the date from. If ``None`` then the diff is
  97. between ``dt`` and now.
  98. """
  99. import pendulum
  100. return pendulum.instance(dt).diff_for_humans(since)