Arrow

repository·master·Indexed 27 days ago

https://github.com/arrow-py/arrow

A Python library providing a human-friendly approach to creating, manipulating, formatting, and converting dates, times, and timestamps. It serves as a drop-in replacement for the standard datetime module, featuring the Arrow class for date/time manipulation, the get() function for flexible parsing, and utilities for humanizing relative time strings, timezone conversion via to(), and time range generation.

Tokens
8.6K
Snippets
14
Records
69
Agent score
92%

What's inside arrow

  1. Escape regular expressions in format strings

    master

    You can escape regular expressions by enclosing them in square brackets []. This is useful for parsing strings with unpredictable whitespace or separators (e.g., log files).

    >>> fmt = r"ddd[\s+]MMM[\s+]DD[\s+]HH:mm:ss[\s+]YYYY"
    >>> arrow.get("Mon Sep 08 16:41:45 2014", fmt)
    <Arrow [2014-09-08T16:41:45+00:00]>
    
    >>> arrow.get("Mon \tSep 08   16:41:45     2014", fmt)
    <Arrow [2014-09-08T16:41:45+00:00]>
  2. Escape tokens and phrases in format strings

    master

    To include literal text, tokens, or phrases in your format string without them being interpreted as date components, enclose them in square brackets [].

    >>> fmt = "YYYY-MM-DD h [h] m"
    >>> arw = arrow.get("2018-03-09 8 h 40", fmt)
    <Arrow [2018-03-09T08:40:00+00:00]>
    >>> arw.format(fmt)
    '2018-03-09 8 h 40'
    
    >>> fmt = "YYYY-MM-DD h [hello] m"
    >>> arw = arrow.get("2018-03-09 8 hello 40", fmt)
    <Arrow [2018-03-09T08:40:00+00:00]>
    >>> arw.format(fmt)
    '2018-03-09 8 hello 40'
  3. Quick Start with Arrow

    master
    Arrow provides a human-friendly API for creating, manipulating, and formatting dates and times. Key capabilities include parsing ISO 8601 strings, getting the current UTC time, shifting time offsets, converting timezones, and humanizing date strings.
  4. Normalize redundant whitespace during parsing

    master

    When using arrow.get(), you can pass the normalize_whitespace=True flag to automatically handle redundant spaces, tabs, or newlines in the input string.

    >>> arrow.get('\t \n  2013-05-05T12:30:45.123456 \t \n', normalize_whitespace=True)
    <Arrow [2013-05-05T12:30:45.123456+00:00]>
    
    >>> arrow.get('2013-05-05  T \n   12:30:45\t123456', 'YYYY-MM-DD T HH:mm:ss S', normalize_whitespace=True)
    <Arrow [2013-05-05T12:30:45+00:00]>
  5. Create Arrow objects

    master

    You can create Arrow objects using several methods:

    • Current time: Use arrow.utcnow() for UTC or arrow.now() for local time. You can specify a timezone with arrow.now('US/Pacific').
    • Timestamps: Use arrow.get() with an int or float timestamp.
    • Datetime objects: Pass a datetime object to arrow.get(). You can also provide a timezone string or a tzinfo object.
    • Strings: Use arrow.get() with a format string for custom patterns, or pass ISO 8601 compliant strings directly. arrow.get() can also search for dates within a larger string.
    • Direct instantiation: Use arrow.get(year, month, day) or arrow.Arrow(year, month, day) with arguments similar to the standard datetime constructor.
  6. Parse dates with surrounding punctuation

    master

    Arrow can parse date strings that are surrounded by punctuation characters such as , . ; : ? ! " ' [ ] { } ( ) < >`. However, be careful: if multiple punctuation marks follow the date, it may raise an exception.

    >>> arrow.get("Cool date: 2019-10-31T09:12:45.123456+04:30.", "YYYY-MM-DDTHH:mm:ss.SZZ")
    <Arrow [2019-10-31T09:12:45.123456+04:30]>
    
    >>> arrow.get("Tomorrow (2019-10-31) is Halloween!", "YYYY-MM-DD")
    <Arrow [2019-10-31T00:00:00+00:00]>
  7. Replace and shift Arrow objects

    master

    Arrow objects are immutable; these methods return a new Arrow object:

    • .replace(): Create a new object with altered attributes (e.g., hour, minute) or a different timezone via tzinfo.
    • .shift(): Create a new object by moving time forward or backward using units like weeks, days, hours, etc. (e.g., arw.shift(weeks=+3)).
    • Ambiguous times: Use .replace(fold=1) to move between the earlier and later moments of an ambiguous time during a timezone transition.
  8. Convert timezones

    master
    Use the .to() method to convert an Arrow object from its current timezone to another. You can use timezone names (e.g., 'US/Pacific'), tzinfo objects, or the shorthand 'local' to convert to the system's local timezone.
  9. Use built-in formatting standards

    master

    Arrow provides several pre-defined constants for common formatting standards. You can use these with the .format() method to quickly output dates in standard formats like RFC or W3C.

    >>> arw = arrow.utcnow()
    >>> arw.format(arrow.FORMAT_ATOM)
    '2020-05-27 10:30:35+00:00'
    >>> arw.format(arrow.FORMAT_COOKIE)
    'Wednesday, 27-May-2020 10:30:35 UTC'
    >>> arw.format(arrow.FORMAT_RSS)
    'Wed, 27 May 2020 10:30:35 +0000'
    >>> arw.format(arrow.FORMAT_RFC822)
    'Wed, 27 May 20 10:30:35 +0000'
    >>> arw.format(arrow.FORMAT_RFC850)
    'Wednesday, 27-May-20 10:30:35 UTC'
    >>> arw.format(arrow.FORMAT_RFC1036)
    'Wed, 27 May 20 10:30:35 +0000'
    >>> arw.format(arrow.FORMAT_RFC1123)
    'Wed, 27 May 2020 10:30:35 +0000'
    >>> arw.format(arrow.FORMAT_RFC2822)
    'Wed, 27 May 20 10:30:35 +0000'
    >>> arw.format(arrow.FORMAT_RFC3339)
    '2020-05-27 10:30:35+00:00'
    >>> arw.format(arrow.FORMAT_W3C)
    '2020-05-27 10:30:35+00:00'