Python uses the arrow library to handle time data in an elegant manner.

Source: Internet
Author: User

Python uses the arrow library to handle time data in an elegant manner.

Preface

We should all know that we have to deal with time in many times, but the time processing module in the Python standard library is actually not very friendly. Why do I say this? Because I believe that most people query documents almost every time they process time data, such as interchange between time and text formats, and increasing or decreasing time, it is not easy to process in Python.

The most terrible thing is that there are actually two modules in the Python standard library to process time, one is time and the other is datetime, which provides a similar method, but the two are completely different. This is not complete yet. There is also a module named calendar in the standard library, which is also used to process time.

Today, I will not take you to understand the relationship between the three, because now you remember it does not mean you will not forget it in the future. The main character of today is an elegant time processing database-arrow that I cannot give up.

Introduction

Arrow is a lightweight Python library dedicated to processing time and date. It provides a reasonable and intelligent way to create, operate, format, convert time, and date.

Install

pip install arrow

Use

Let's look at the Code directly, and the comments are split lines.

>>> Import arrow # obtain the current time >>> utc = arrow. utcnow () >>> utc <Arrow [2017-05-11T21: 23: 58.970460 + 00: 00] >#adjust the time >>> utc = utc. shift (days = + 1, hours =-1) >>> utc <Arrow [2017-05-12T20: 23: 58.970460 + 00: 00] >#modify time >>> utc. replace (hour = 4, minute = 40) <Arrow [2017-05-12T04: 40: 58.970460 + 00: 00] ># >>> local = utc. to ('us/pacpacific ')> local <Arrow [2017-05-11T13: 23: 58.970460-]> # convert from text to time Object> arrow. get ('1970-05-11T21: 23: 2017 + 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 was born in May 1980 ', 'mmmm yyyy') <Arrow [t00: 00: 00 + 00: 00]> # obtain the timestamp >>> local. timestamp1368303838 # format the output >>> local. format () '2017-05-11 13:23:58-'>>> local. format ('yyyy-MM-DD HH: mm: ss') '2017-05-11 13:23:58 '>>> local. humanize () 'An hour ago '# convert to a standard library Object>. date () datetime. date (2017, 5, 7) >>>. time () datetime. time (4, 38, 15,447 644)

Summary

Didn't I lie to you? If your Python project will require processing time in the future, discard the standard library and arrow will save countless brain cells.

Well, the above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Attach the official arrow document. For more cool usage, visit the official website.

Http://arrow.readthedocs.io/en/latest/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.