Although Python provides a number of built-in modules for operation date time, but sometimes does not meet our needs, so the following article mainly introduces you about how Python uses arrow library to gracefully handle time data related information, the need for friends can reference, Let's take a look below.
Objective
We should all know that in many cases we have to deal with time, but the module dealing with time in the Python standard library is not designed to be very friendly, why would I say so? Because I believe that most people work with time data almost each time, again to look up documents, such as time and text format, time changes and other seemingly very basic operations, in Python is not easy to deal with.
Most of all, there are two module processing times in the Python standard library, one called time, the other called DateTime, which provides a similar approach but two is not the same thing. It's not over yet, and the standard library has a module called Calendar, which is also used to handle time.
Today I do not take you to understand the three of their relationship, because now you remember that does not mean that you will never forget. Today's protagonist is an elegant to I can't give up the time processing library-arrow.
Brief introduction
Arrow is a lightweight Python library dedicated to time and date, providing a logical and intelligent way to create, manipulate, format, convert time and date.
Installation
Pip Install arrow
Use
We look directly at the code, and the comment splits the line.
>>> Import arrow# get current time >>> UTC = Arrow.utcnow () >>> Utc<arrow [2017-05-11t21 : 23:58.970460+00:00]># Adjust time >>> UTC = Utc.shift (days=+1, Hours=-1) >>> Utc<arrow [2017-05-12t20 : 23:58.970460+00:00]># Modified Time >>> utc.replace (hour=4, minute=40) <arrow [2017-05-12t04:40:58.970460+ 00:00]># Conversion time zone >>> local = utc.to (' us/pacific ') >>> Local<arrow [2017-05-11t13 : 23:58.970460-07:00]># convert from Text to time object >>> arrow.get (' 2017-05-11t21:23:58.970460+00:00 ') <arrow [ 2017-05-11t21:23:58.970460+00:00]>>>> arrow.get (1367900664) <arrow [2017-05-07T04:24:24+00:00]> >>> Arrow.get (' June is born in May 1980 ', ' MMMM YYYY ') <arrow [1980-05-01t00:00:00+00:00]># get timestamp >> > local.timestamp1368303838# formatted Output >>> local.format () ' 2017-05-11 13:23:58-07:00 ' >>> Local.format (' yyyy-mm-dd HH:mm:ss ') ' 2017-05-11 13:23:58 ' >>> local.humanize () ' an hour ago ' # converted to standard library objects >> > A.date() Datetime.date (5, 7) >>> A.time () datetime.time (4, 38, 15, 447644)
Summarize
I didn't lie to you, did I? If your Python project needs to process time later, throw away the standard library and arrow will save you countless brain cells.