### Install Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/index.md Install the Arrow library using pip or pipenv. ```console $ pip install -U arrow ``` -------------------------------- ### Install Arrow using pip Source: https://github.com/arrow-py/arrow/blob/master/docs/getting-started.md Use this command to install the latest version of the Arrow library. ```console pip install -U arrow ``` -------------------------------- ### Install Arrow using pip Source: https://github.com/arrow-py/arrow/blob/master/README.rst Use this command to install or upgrade the Arrow library. Ensure you are using a compatible Python version (3.8+). ```console $ pip install -U arrow ``` -------------------------------- ### Floor to Week with Custom Start Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Rounds a date down to the beginning of the week, with a customizable start day (e.g., 7 for Sunday). Requires the arrow library. ```python >>> arrow.utcnow().floor('week', week_start=7) ``` -------------------------------- ### Get Arrow object from ISO 8601 string Source: https://github.com/arrow-py/arrow/blob/master/README.rst Import the Arrow library and use the `get` method to create an Arrow object from a string representing a specific date and time with timezone information. ```python >>> import arrow >>> arrow.get('2013-05-11T21:23:58.970460+07:00') ``` -------------------------------- ### Basic Arrow Usage Source: https://github.com/arrow-py/arrow/blob/master/docs/index.md Demonstrates common operations like creating Arrow objects from strings, getting current UTC time, shifting time, converting timezones, and formatting output. ```python >>> import arrow >>> arrow.get('2013-05-11T21:23:58.970460+07:00') ``` ```python >>> utc = arrow.utcnow() >>> utc ``` ```python >>> utc = utc.shift(hours=-1) >>> utc ``` ```python >>> local = utc.to('US/Pacific') >>> local ``` ```python >>> local.timestamp() 1368303838.970460 ``` ```python >>> local.format() '2013-05-11 13:23:58 -07:00' ``` ```python >>> local.format('YYYY-MM-DD HH:mm:ss ZZ') '2013-05-11 13:23:58 -07:00' ``` ```python >>> local.humanize() 'an hour ago' ``` ```python >>> local.humanize(locale='ko-kr') '한시간 전' ``` -------------------------------- ### Adjust week start day with span method in Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md The `span` method can be used to get the start and end of a week. By default, the week starts on Monday (ISO weekday 1). The `week_start` argument allows customization of the first day of the week. ```python >>> arw >>> arw.isoweekday() 1 # Monday >>> arw.span("week") (, ) >>> arw.span("week", week_start=4) (, ) ``` -------------------------------- ### Create and Manipulate Arrow Objects Source: https://github.com/arrow-py/arrow/blob/master/docs/getting-started.md Demonstrates creating Arrow objects from strings, getting the current UTC time, shifting time, converting timezones, and accessing timestamp values. ```python import arrow arrow.get('2013-05-11T21:23:58.970460+07:00') ``` ```python utc = arrow.utcnow() utc ``` ```python utc = utc.shift(hours=-1) utc ``` ```python local = utc.to('US/Pacific') local ``` ```python local.timestamp() ``` -------------------------------- ### Ceil to Week with Custom Start Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Rounds a date up to the end of the week, with a customizable start day (e.g., 7 for Sunday). Requires the arrow library. ```python >>> arrow.utcnow().ceil('week', week_start=7) ``` -------------------------------- ### Get Time Span of Unit Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Retrieves the start and end of a specified time unit (e.g., 'hour') relative to the current UTC time. Requires the arrow library. ```python >>> arrow.utcnow().span('hour') (, ) ``` -------------------------------- ### Timestamp Parsing without Format String (Deprecated) Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md These examples will no longer work in v0.15.0 due to changes in `arrow.get()` behavior. Timestamps require a format string. ```python >>> arrow.get("1565358758") ``` ```python >>> arrow.get("1565358758.123413") ``` -------------------------------- ### Format Arrow object with custom string Source: https://github.com/arrow-py/arrow/blob/master/README.rst Provide a format string to the `format()` method to customize the output. This example uses 'YYYY-MM-DD HH:mm:ss ZZ' for a specific date and time representation. ```python >>> local.format('YYYY-MM-DD HH:mm:ss ZZ') '2013-05-11 13:23:58 -07:00' ``` -------------------------------- ### Parse String with Timezone Information Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Shows how to pass a timezone string to the `get()` function. This functionality was fixed in v0.14.4. ```python arrow.get("2019072807", "YYYYMMDDHH", tzinfo="UTC") ``` -------------------------------- ### Humanize Arrow object with Korean locale Source: https://github.com/arrow-py/arrow/blob/master/README.rst Specify a locale string to the `humanize()` method to get the human-readable time difference in a different language. This example uses the Korean locale. ```python >>> local.humanize(locale='ko-kr') '한시간 전' ``` -------------------------------- ### Range Iteration Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Iterates over a range of dates, yielding Arrow objects at specified intervals between a start and end datetime. Requires `datetime` and `arrow` imports. ```python >>> start = datetime(2013, 5, 5, 12, 30) >>> end = datetime(2013, 5, 5, 17, 15) >>> for r in arrow.Arrow.range('hour', start, end): ... print(repr(r)) ... ``` -------------------------------- ### Get Current Time Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Use `arrow.utcnow()` for the current time in UTC or `arrow.now()` for the current local time. Specify a timezone string for local time in a specific timezone. ```python >>> arrow.utcnow() ``` ```python >>> arrow.now() ``` ```python >>> arrow.now('US/Pacific') ``` -------------------------------- ### Get Datetime and Timestamp Representations Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Access the underlying `datetime` object using the `.datetime` property or the Unix timestamp with `.timestamp`. The `.naive` property returns a timezone-naive datetime. ```python >>> a = arrow.utcnow() >>> a.datetime datetime.datetime(2013, 5, 7, 4, 38, 15, 447644, tzinfo=tzutc()) ``` ```python >>> a.naive datetime.datetime(2013, 5, 7, 4, 38, 15, 447644) ``` ```python >>> a.tzinfo tzutc() ``` -------------------------------- ### Get current UTC time Source: https://github.com/arrow-py/arrow/blob/master/README.rst Use `arrow.utcnow()` to obtain the current date and time in Coordinated Universal Time (UTC). This is the default timezone for Arrow objects. ```python >>> utc = arrow.utcnow() >>> utc ``` -------------------------------- ### Span Range Iteration Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Iterates over a range of time spans, yielding tuples of start and end Arrow objects for each unit. Requires `datetime` and `arrow` imports. ```python >>> start = datetime(2013, 5, 5, 12, 30) >>> end = datetime(2013, 5, 5, 17, 15) >>> for r in arrow.Arrow.span_range('hour', start, end): ... print(r) ... (, ) (, ) (, ) (, ) (, ) ``` -------------------------------- ### Humanize Arrow object relative to now Source: https://github.com/arrow-py/arrow/blob/master/README.rst The `humanize()` method provides a human-readable representation of the time difference between the Arrow object and the current time. This example shows 'an hour ago'. ```python >>> local.humanize() 'an hour ago' ``` -------------------------------- ### Shift Arrow object by hours Source: https://github.com/arrow-py/arrow/blob/master/README.rst The `shift` method allows you to modify an Arrow object by adding or subtracting time units. This example subtracts one hour from the current UTC time. ```python >>> utc = utc.shift(hours=-1) >>> utc ``` -------------------------------- ### Get Unix timestamp from Arrow object Source: https://github.com/arrow-py/arrow/blob/master/README.rst The `timestamp()` method returns the Unix epoch timestamp (seconds since January 1, 1970, 00:00:00 UTC) for the given Arrow object. ```python >>> local.timestamp() 1368303838.970460 ``` -------------------------------- ### Use exact argument with span_range in Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md The `exact=True` argument in `span_range` ensures that the generated time spans precisely adhere to the start and end times provided, preventing them from extending beyond these boundaries. ```python >>> start = Arrow(2021, 2, 5, 12, 30) >>> end = Arrow(2021, 2, 5, 17, 15) >>> for r in arrow.Arrow.span_range('hour', start, end, exact=True): ... print(r) ... (, ) (, ) (, ) (, ) (, ) ``` -------------------------------- ### Humanize with Quarter Granularity Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates using the `humanize` method with the `granularity` parameter set to 'quarter' to express time differences in quarters. This is useful for approximating longer time spans. Note that passing an empty list to `granularity` will raise a `ValueError`. ```python >>> import arrow >>> now = arrow.now() >>> four_month_shift = now.shift(months=4) >>> now.humanize(four_month_shift, granularity="quarter") 'a quarter ago' >>> four_month_shift.humanize(now, granularity="quarter") 'in a quarter' >>> thirteen_month_shift = now.shift(months=13) >>> thirteen_month_shift.humanize(now, granularity="quarter") 'in 4 quarters' >>> now.humanize(thirteen_month_shift, granularity="quarter") '4 quarters ago' ``` -------------------------------- ### Format Dates with Built-in Standards Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Demonstrates formatting an Arrow object using various predefined standards. Ensure an Arrow object is initialized before use. ```python >>> arw = arrow.utcnow() >>> arw.format(arrow.FORMAT_ATOM) '2020-05-27 10:30:35+00:00' ``` ```python >>> arw.format(arrow.FORMAT_COOKIE) 'Wednesday, 27-May-2020 10:30:35 UTC' ``` ```python >>> arw.format(arrow.FORMAT_RSS) 'Wed, 27 May 2020 10:30:35 +0000' ``` ```python >>> arw.format(arrow.FORMAT_RFC822) 'Wed, 27 May 20 10:30:35 +0000' ``` ```python >>> arw.format(arrow.FORMAT_RFC850) 'Wednesday, 27-May-20 10:30:35 UTC' ``` ```python >>> arw.format(arrow.FORMAT_RFC1036) 'Wed, 27 May 20 10:30:35 +0000' ``` ```python >>> arw.format(arrow.FORMAT_RFC1123) 'Wed, 27 May 2020 10:30:35 +0000' ``` ```python >>> arw.format(arrow.FORMAT_RFC2822) 'Wed, 27 May 2020 10:30:35 +0000' ``` ```python >>> arw.format(arrow.FORMAT_RFC3339) '2020-05-27 10:30:35+00:00' ``` ```python >>> arw.format(arrow.FORMAT_W3C) '2020-05-27 10:30:35+00:00' ``` -------------------------------- ### Format Arrow Objects Source: https://github.com/arrow-py/arrow/blob/master/docs/getting-started.md Shows how to format Arrow objects into different string representations, including default and custom formats. ```python local.format() ``` ```python local.format('YYYY-MM-DD HH:mm:ss ZZ') ``` -------------------------------- ### Humanize Week Granularity with Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Shows how to use the `humanize` method with week granularity. This requires the `granularity` parameter to be set to 'week'. ```python arrow.utcnow().shift(weeks=-1).humanize(granularity="week") ``` -------------------------------- ### Format Datetime with Built-in Format Strings Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Shows how to format an Arrow object using predefined, built-in format strings. This simplifies common formatting tasks without needing to specify custom format tokens. ```python >>> arw = arrow.utcnow() >>> arw.format(arrow.FORMAT_COOKIE) 'Wednesday, 27-May-2020 10:30:35 UTC' ``` -------------------------------- ### Parse ISO 8601 Basic Format with Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Illustrates parsing dates and times using the ISO 8601 basic format (YYYYMMDDTHHmmssZ). ```python arrow.get("20190908T143000Z") ``` -------------------------------- ### Parse ISO 8601 Basic Format with Arrow Source: https://github.com/arrow-py/arrow/blob/master/CHANGELOG.rst Illustrates parsing dates and times using the ISO 8601 basic format (YYYYMMDDTHHmmssZ). ```python arrow.get("20230101T123059Z", "YYYYMMDDTHHmmssZ") ``` -------------------------------- ### Handle DST Shifts with Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates how Arrow handles imaginary datetimes during Daylight Saving Time shifts. Ensure your timezone is correctly set. ```python >>> just_before = arrow.get(2013, 3, 31, 1, 55, tzinfo="Europe/Paris") >>> just_before.shift(minutes=+10) ``` -------------------------------- ### Create Arrow Object from ISO Calendar Tuple Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Use arrow.get() with an ISO calendar tuple (year, week number, weekday) to create an Arrow object. ```python >>> arrow.get((2013, 18, 7)) ``` -------------------------------- ### Parse and Format ISO 8601 Week Dates Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Use the 'W' token to parse and format ISO 8601 week dates. Ensure the correct format string is provided. ```python >>> arrow.get("2013-W29-6", "W") ``` ```python >>> utc=arrow.utcnow() >>> utc >>> utc.format("W") '2020-W04-4' ``` -------------------------------- ### Parse Ordinal Date Tokens with Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates the usage of new ordinal date tokens (DDD, DDDD) for parsing dates. Ensure the correct format string is provided. ```python arrow.get("1998-045") ``` ```python arrow.get("1998-45", "YYYY-DDD") ``` ```python arrow.get("1998-045", "YYYY-DDDD") ``` -------------------------------- ### Create Arrow from Timestamps Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Instantiate Arrow objects from integer or float Unix timestamps. The precision of the timestamp determines the precision of the resulting Arrow object. ```python >>> arrow.get(1367900664) ``` ```python >>> arrow.get(1367900664.152325) ``` -------------------------------- ### Timestamp Parsing with Format String (Supported) Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates the correct way to parse timestamps with the 'X' format string in v0.15.0. This includes handling float timestamps. ```python >>> arrow.get("1565358758", "X") ``` ```python >>> arrow.get("1565358758.123413", "X") ``` ```python >>> arrow.get(1565358758) ``` ```python >>> arrow.get(1565358758.123413) ``` -------------------------------- ### Range Generation Across DST in Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Illustrates generating a range of hours between two datetimes, showing how Arrow converts to UTC and accounts for DST changes. This is useful for analyzing time series data that spans DST transitions. ```python >>> before = arrow.get("2018-03-10 23:00:00", "YYYY-MM-DD HH:mm:ss", tzinfo="US/Pacific") >>> after = arrow.get("2018-03-11 04:00:00", "YYYY-MM-DD HH:mm:ss", tzinfo="US/Pacific") >>> result=[(t, t.to("utc")) for t in arrow.Arrow.range("hour", before, after)] >>> for r in result: ... print(r) ... (, ) (, ) (, ) (, ) (, ) ``` -------------------------------- ### Create Arrow Object from Timestamp with Timezone Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Use arrow.get() with a timestamp and tzinfo to create an Arrow object with a specific timezone. ```python >>> arrow.get(1367900664, tzinfo=tz.gettz('US/Pacific')) ``` -------------------------------- ### Instantiate Arrow Directly Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Arrow objects can be created directly using year, month, day, and other `datetime` arguments, similar to the `datetime` constructor. ```python >>> arrow.get(2013, 5, 5) ``` ```python >>> arrow.Arrow(2013, 5, 5) ``` -------------------------------- ### Parse String to Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Parse date and time information from strings. Provide a format string for non-standard formats, or rely on ISO 8601 parsing for compliant strings. ```python >>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss') ``` ```python >>> arrow.get('June was born in May 1980', 'MMMM YYYY') ``` ```python >>> arrow.get('2013-09-30T15:34:00.000-07:00') ``` -------------------------------- ### Humanize Arrow Objects Source: https://github.com/arrow-py/arrow/blob/master/docs/getting-started.md Illustrates the humanize function for relative time formatting, including locale-specific output. ```python local.humanize() ``` ```python local.humanize(locale='ko-kr') ``` -------------------------------- ### Format Arrow Objects Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Format an Arrow object into a string representation using supported tokens. Refer to the 'Supported Tokens' section for a complete list. ```python >>> arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ') '2013-05-07 05:23:16 -00:00' ``` -------------------------------- ### Access Date and Time Properties Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Retrieve individual components of the date and time, such as year, month, and day, using attribute access. Standard `datetime` methods like `.date()` and `.time()` are also available. ```python >>> a = arrow.utcnow() >>> a.year 2013 ``` ```python >>> a.date() datetime.date(2013, 5, 7) ``` ```python >>> a.time() datetime.time(4, 38, 15, 447644) ``` -------------------------------- ### Custom Arrow Factory Usage Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Creates and uses a factory for a custom Arrow-derived type to instantiate objects with the custom methods. Requires the `arrow` library and a custom class definition. ```python >>> factory = arrow.ArrowFactory(CustomArrow) >>> custom = factory.utcnow() >>> custom >>> >>> custom.days_till_xmas() >>> 211 ``` -------------------------------- ### Humanize with Multiple Granularity Levels Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Combine multiple levels of granularity (e.g., hours and minutes) when using the humanize method for relative time differences. ```python >>> later140 = arrow.utcnow().shift(seconds=+8400) >>> later140.humanize(granularity="minute") 'in 139 minutes' >>> later140.humanize(granularity=["hour", "minute"]) 'in 2 hours and 19 minutes' ``` -------------------------------- ### Handling Ambiguous Datetimes with Fold Attribute Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates how to handle ambiguous datetimes during DST shifts using the `fold` attribute. This is crucial for accurate time representation when clocks spring forward or fall back. ```python >>> before = Arrow(2017, 10, 29, 2, 0, tzinfo='Europe/Stockholm') >>> before.fold 0 >>> before.ambiguous True >>> after = Arrow(2017, 10, 29, 2, 0, tzinfo='Europe/Stockholm', fold=1) >>> after = before.replace(fold=1) ``` -------------------------------- ### Floor to Unit Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Rounds a date down to the beginning of the specified time unit. Requires the arrow library. ```python >>> arrow.utcnow().floor('hour') ``` -------------------------------- ### Parse Float Timestamps with 'X' Token Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md The 'X' token now correctly matches float timestamps provided as strings. Ensure the format string 'X' is used. ```python arrow.get("1565358758.123415", "X") ``` -------------------------------- ### Normalize Timestamps with Microsecond Precision Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Demonstrates how `arrow.get` normalizes millisecond and microsecond timestamps. The `.timestamp` property returns seconds since the epoch, truncating sub-second precision. ```python >>> ts = 1591161115194556 >>> arw = arrow.get(ts) >>> arw.timestamp 1591161115 ``` -------------------------------- ### Format Microseconds with 'x' Token Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Format datetime objects to display microseconds using the 'x' token. The 'X' token formats seconds. ```python >>> dt = arrow.utcnow() >>> dt.format("x") '1585669870688329' >>> dt.format("X") '1585669870' ``` -------------------------------- ### Create Arrow Objects from Timestamp with Timezone Source: https://github.com/arrow-py/arrow/blob/master/CHANGELOG.rst When creating Arrow objects from a timestamp, you can now specify the timezone using the tzinfo parameter. This ensures correct timezone handling. ```python >>> arrow.get(1367900664, tzinfo=tz.gettz('US/Pacific')) ``` -------------------------------- ### Format Arrow object to default string Source: https://github.com/arrow-py/arrow/blob/master/README.rst The `format()` method, when called without arguments, formats the Arrow object into a human-readable string including the date, time, and timezone offset. ```python >>> local.format() '2013-05-11 13:23:58 -07:00' ``` -------------------------------- ### Create custom locales by subclassing in Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Custom locales can be created by subclassing existing locale classes, such as `EnglishLocale`. This allows for defining custom names and behaviors for new locales. ```python >>> from arrow.locales import EnglishLocale >>> class Klingon(EnglishLocale): ... names = ["tlh"] ... >>> from arrow import locales >>> locales.get_locale("tlh") <__main__.Klingon object at 0x7f7cd1effd30> ``` -------------------------------- ### Replace Date/Time Components Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Create a new Arrow object with modified date or time components using the `.replace()` method, similar to Python's `datetime` objects. ```python >>> arw = arrow.utcnow() >>> arw >>> arw.replace(hour=4, minute=40) ``` -------------------------------- ### Dehumanize with Locale Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Converts a human-readable string into a future date using a specified locale. Ensure the arrow library is imported and the locale is supported. ```python >>> arw = arrow.utcnow() >>> arw >>> later = arw.dehumanize("एक माह बाद", locale="hi") >>> later ``` -------------------------------- ### Parse Dates with Regular Expressions for Whitespace Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Employ regular expressions within format strings to match variable amounts of whitespace between date tokens. This is beneficial for parsing log files or other data where spacing is inconsistent. ```python >>> fmt = r"ddd[\s+]MMM[\s+]DD[\s+]HH:mm:ss[\s+]YYYY" >>> arrow.get("Mon Sep 08 16:41:45 2014", fmt) ``` ```python >>> arrow.get("Mon \tSep 08 16:41:45 2014", fmt) ``` ```python >>> arrow.get("Mon Sep 08 16:41:45 2014", fmt) ``` -------------------------------- ### Humanize Time Differences Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Display time differences in a human-readable format relative to 'now' or another specified Arrow/datetime object. Options include `only_distance` and `granularity` for fine-tuning the output. ```python >>> past = arrow.utcnow().shift(hours=-1) >>> past.humanize() 'an hour ago' ``` ```python >>> present = arrow.utcnow() >>> future = present.shift(hours=2) >>> future.humanize(present) 'in 2 hours' ``` ```python >>> present = arrow.utcnow() >>> future = present.shift(hours=2) >>> future.humanize(present, only_distance=True) '2 hours' ``` ```python >>> present = arrow.utcnow() >>> future = present.shift(minutes=66) >>> future.humanize(present, granularity="minute") 'in 66 minutes' ``` ```python >>> future.humanize(present, granularity=["hour", "minute"]) 'in an hour and 6 minutes' ``` ```python >>> present.humanize(future, granularity=["hour", "minute"]) 'an hour and 6 minutes ago' ``` ```python >>> future.humanize(present, only_distance=True, granularity=["hour", "minute"]) 'an hour and 6 minutes' ``` ```python >>> future = arrow.utcnow().shift(hours=1) >>> future.humanize(a, locale='ru') 'через 2 час(а,ов)' ``` -------------------------------- ### Ceil to Unit Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Rounds a date up to the end of the specified time unit. Requires the arrow library. ```python >>> arrow.utcnow().ceil('hour') ``` -------------------------------- ### Handle Large Sub-Second Tokens Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Arrow now accurately handles rounding for large sub-second tokens that exceed standard precision, preventing incorrect date objects. ```python arrow.get("2015-01-12T01:13:15.9999995") ``` -------------------------------- ### Normalize Whitespace During Parsing Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Utilizes the `normalize_whitespace` flag in `arrow.get` to parse strings with inconsistent spacing. This is particularly useful for log files or data with irregular formatting. ```python >>> arrow.get("Jun 1 2005 1:33PM", "MMM D YYYY H:mmA", normalize_whitespace=True) ``` ```python >>> arrow.get("2013-036 04:05:06Z", normalize_whitespace=True) ``` -------------------------------- ### Create Arrow from Datetime Objects Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Create Arrow objects from Python `datetime` objects. You can provide a timezone string or `dateutil.tz` object to make the Arrow object timezone-aware. ```python >>> from datetime import datetime >>> arrow.get(datetime.utcnow()) ``` ```python >>> arrow.get(datetime(2013, 5, 5), 'US/Pacific') ``` ```python >>> from dateutil import tz >>> arrow.get(datetime(2013, 5, 5), tz.gettz('US/Pacific')) ``` ```python >>> arrow.get(datetime.now(tz.gettz('US/Pacific'))) ``` -------------------------------- ### Handle Ambiguous Times Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md When dealing with daylight saving time transitions, Arrow objects have an `ambiguous` property. Use `.replace(fold=1)` to select the later occurrence of an ambiguous time. ```python >>> paris_transition = arrow.Arrow(2019, 10, 27, 2, tzinfo="Europe/Paris", fold=0) >>> paris_transition >>> paris_transition.ambiguous True >>> paris_transition.replace(fold=1) ``` -------------------------------- ### Format String with Escaped Characters Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md Escape characters within a format string using square brackets `[]` to prevent them from being interpreted as tokens. ```python >>> arw = arrow.now() >>> fmt = "YYYY-MM-DD h [h] m" >>> arw.format(fmt) '2019-11-02 3 h 32' ``` -------------------------------- ### Parse Dates with Leading/Trailing Punctuation Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Handle date strings that are enclosed by a single punctuation character. Note that multiple punctuation marks following the date will raise an exception. ```python >>> arrow.get("Cool date: 2019-10-31T09:12:45.123456+04:30.", "YYYY-MM-DDTHH:mm:ss.SZZ") ``` ```python >>> arrow.get("Tomorrow (2019-10-31) is Halloween!", "YYYY-MM-DD") ``` ```python >>> arrow.get("Halloween is on 2019.10.31.", "YYYY.MM.DD") ``` -------------------------------- ### Convert Timezones Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Convert an Arrow object to a different timezone using the `.to()` method. You can specify the target timezone by name or by using a `dateutil.tz` object. 'local' is a shorthand for the system's local timezone. ```python >>> utc = arrow.utcnow() >>> utc >>> utc.to('US/Pacific') ``` ```python >>> utc.to(tz.gettz('US/Pacific')) ``` ```python >>> utc.to('local') ``` ```python >>> utc.to('local').to('utc') ``` -------------------------------- ### Dehumanize to Future Time Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Converts a human-readable string into a future date relative to the current UTC time. Ensure the arrow library is imported. ```python >>> arw = arrow.utcnow() >>> arw >>> later = arw.dehumanize("in a month") >>> later ``` -------------------------------- ### Custom Arrow Type Definition Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Defines a custom Arrow-derived class with additional methods, such as calculating days until Christmas. Requires the `arrow` library. ```python >>> class CustomArrow(arrow.Arrow): ... ... def days_till_xmas(self): ... ... xmas = arrow.Arrow(self.year, 12, 25) ... ... if self > xmas: ... xmas = xmas.shift(years=1) ... ... return (xmas - self).days ``` -------------------------------- ### Dehumanize to Past Time Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Converts a human-readable string into a past date relative to the current UTC time. Ensure the arrow library is imported. ```python >>> arw = arrow.utcnow() >>> arw >>> earlier = arw.dehumanize("2 days ago") >>> earlier ``` -------------------------------- ### Shift Date/Time Components Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Advance or rewind an Arrow object by a specified duration using the `.shift()` method. This returns a new Arrow object with the adjusted time. ```python >>> arw = arrow.utcnow() >>> arw.shift(weeks=+3) ``` -------------------------------- ### Use dehumanize for relative time shifts in Arrow Source: https://github.com/arrow-py/arrow/blob/master/docs/releases.md The `dehumanize` method converts human-readable time expressions into relative time shifts. It supports various formats and can utilize specific locales for parsing. ```python >>> arw >>> arw.dehumanize("8 hours ago") >>> arw.dehumanize("in 4 days") >>> arw.dehumanize("in an hour 34 minutes 10 seconds") >>> arw.dehumanize("hace 2 años", locale="es") ``` -------------------------------- ### Parse Dates with Custom Phrases Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Use custom phrases within format strings to parse dates that include literal text. This is useful for locales where date components are separated by words or specific phrases. ```python >>> fmt = "YYYY-MM-DD h [h] m" >>> arw = arrow.get("2018-03-09 8 h 40", fmt) >>> arw.format(fmt) '2018-03-09 8 h 40' ``` ```python >>> fmt = "YYYY-MM-DD h [hello] m" >>> arw = arrow.get("2018-03-09 8 hello 40", fmt) >>> arw.format(fmt) '2018-03-09 8 hello 40' ``` ```python >>> fmt = "YYYY-MM-DD h [hello world] m" >>> arw = arrow.get("2018-03-09 8 hello world 40", fmt) >>> arw.format(fmt) '2018-03-09 8 hello world 40' ``` -------------------------------- ### Convert Arrow object to a different timezone Source: https://github.com/arrow-py/arrow/blob/master/README.rst Use the `to` method to convert an Arrow object from its current timezone to another specified timezone, such as 'US/Pacific'. ```python >>> local = utc.to('US/Pacific') >>> local ``` -------------------------------- ### Replace Timezone Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Change the timezone of an Arrow object without altering the underlying time values using `.replace(tzinfo=...)`. This is useful for representing the same moment in different timezones. ```python >>> arw = arrow.utcnow() >>> arw.replace(tzinfo='US/Pacific') ``` -------------------------------- ### Normalize Whitespace During Parsing Source: https://github.com/arrow-py/arrow/blob/master/docs/guide.md Automatically normalize redundant whitespace characters (spaces, tabs, newlines) when parsing date strings by setting `normalize_whitespace=True` in `arrow.get`. ```python >>> arrow.get('\t \n 2013-05-05T12:30:45.123456 \t \n', normalize_whitespace=True) ``` ```python >>> arrow.get('2013-05-05 T \n 12:30:45\t123456', 'YYYY-MM-DD T HH:mm:ss S', normalize_whitespace=True) ```