This is the 25th day of my participation in the August Challenge

Python has a lot of time and date processing library, such as time, datetime, while provides a very full on the date, time and time zone conversion processing function, but way too much, not easy to remember, and often require various transformation operations, is very complicated, such as time and the timestamp conversion, time format string conversion, etc. Look at the tutorial documentation almost every time you use it. Is there a more user-friendly date-time library? Let’s take a look at arrow’s date and time library.

Arrow is a lightweight Python library that deals exclusively with times and dates. Arrow provides a reasonable, user-friendly way to create, manipulate, format, and convert dates, times, and timestamps, making it easy to create time zone aware date and time instances. You can install it using PIP Install arrow.

Use of arrow module

Get arrow object

Arrow can be very flexible to convert multiple formats of time data into Arrow objects, as follows:

import arrow


print(repr(arrow.Arrow(2021.8.23.8)))
print(repr(arrow.get(2021.8.23.8.40)))
print(repr(arrow.get('the 2021-08-23 GMT')))
print(repr(arrow.get('2021.08.23')))
print(repr(arrow.get('23/2012/08'.'DD/YYYY/MM')))
Copy the code

The result is as follows:

Any of the above methods can convert character data into arrow objects, which is very flexible. In addition, you can convert the timestamp to an Arrow object.

print(repr(arrow.get(1629683393.6558669)))
Copy the code

Get the current time

utc_time = arrow.utcnow()
local_time = arrow.now()
print(utc_time)
print(local_time)
Copy the code

The utcnow() and now() functions get utc time and local time, respectively. We can also specify the time zone when calling now(), for example, arrow.now(‘US/Pacific’).

Conversion of time form

When using a datetime, we often need to convert operations, such as converting a time string to a specified format, converting to a timestamp, etc.

Convert to a time string

now = arrow.now()

print(now)
print(now.format())
print(now.format("YYYY-MM-DD hh:mm:ss"))
print(now.format("YYYY-MM-DD"))
Copy the code

The result is as follows:

The datetime module’s ‘%Y-%M-%D % H :% M :%s’ format is more user-friendly and easy to remember. To convert an arrow object to a timestamp you can use t.timstamp to convert the arrow object to a timestamp.

now.timestamp
Copy the code

To get the data

After converting to Arrow, we can easily obtain all the time data we want by year, month, day, hour, minute, second, week, etc., such as:

now = arrow.now()

print(now.year)
print(now.month)
print(now.day)
print(now.hour)
print(now.minute)
print(now.second)
print(now.week)
Copy the code

Modify the time

Sometimes when we get a time, we need to modify the time, such as changing the time zone and time, etc. We can use the following methods to modify the time.

now = arrow.now()

print(now.format("YYYY-MM-DD hh:mm:ss"))  # 2021-08-23 10:11:04
now_utc = now.to("utc")
print(now_utc.format("YYYY-MM-DD hh:mm:ss"))  # 2021-08-23 02:11:04
now1 = now.replace(day=31, hour=12)
print(now1.format("YYYY-MM-DD hh:mm:ss"))  # 2021-08-31 12:11:04
now2 = now.shift(months=-2)
print(now2.format("YYYY-MM-DD hh:mm:ss"))  # 2021-06-23 10:11:04 
Copy the code

We can switch time zones using to(), change time using replace(), and move time backwards and forwards using Shift ().

As with Python’s built-in datetime library, the arrow object also supports time comparisons and computation of time differences. In addition, there are a number of unexpected actions that you can see in the documentation: arrow: Better Dates & Times for Python — Arrow 🏹 1.1.1 documentation)

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!