Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Arrow time Library

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Arrow time library". In daily operation, I believe many people have doubts about how to use Arrow time library. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Arrow time library"! Next, please follow the editor to study!

DateParser and Dateutil

These are two friendly third-party libraries for dealing with various time formats, which can convert time written in various strange ways into standard time formats, such as:

#-*-coding: utf-8-*-# @ Time: 2020-12-05 16:46 import time import dateparser from dateutil import parser print (dateparser.parse ("5:01:08 on 2020-11-7")) print (parser.parse ("2020"))

Meet a demand

Recently, when we do some automated file export, some platforms have a limit on the number of exports. for example, if the selected time is half a year and then the cumulative amount of data reaches 50w, the export will fail.

For this situation, you need to do some time segmentation, for example, it can be divided into monthly or even weekly export, that is, rangeTime.

When you search Google for Python time or Datetime sharding times, there are no results, and then you will remember that there is a better library of processing time, which is Arrow.

Many blogs also pass through the explanation of Arrow, posting official examples, but not applied to actual projects, so more usage of Arrow is not mentioned.

Arrow

Like DateParser, the underlying Arrow is encapsulated in datetime and can interact with datetime in specific places.

Some usage shows:

#-*-coding: utf-8-*-# @ Time: 2020-12-05 16:46 I = arrow.now () # i.replace (day=1) replacement time is 1 # i.shift (months=-1) offset Push forward # i.format ('YYYY-MM-DD') to your desired time format print (i.shift (days=-30). Format (' YYYY-MM-DD')) print (i.shift (months=-3). Format ('YYYY-MM-DD')) # in conjunction with Chain rule print (i.replace (day=1). Shift (months=-1). Format ('YYYY-MM-DD')) print (i.replace (day=1). Shift (months=-1). Format (' YYYY-MM-DD HH:mm:ss'))

Back to solving the requirement just now, the time interval attribute of Arrow can be segmented according to different time granularity over a period of time to meet our needs.

#-*-coding: utf-8-*-# @ Time: 2020-12-05 16:46 import arrow # starts at the current time, pushes forward 120 days end = datetime.datetime.now () start = end + datetime.timedelta (days=-120) # to get the time range within 120 Divide def timeYmdRange (start=start,end=end) by month: tRange = [] for r in arrow.Arrow.span_range ('months', start,end): qTime = [i.format (' YYYY-MM-DD') for i in r] tRange.append (qTime) return tRange # to get the time range within 120 Def timeWeekRange (start=start,end=end): tRange = [] for r in arrow.Arrow.span_range ('weeks', start,end): qTime = [i.format (' YYYY-MM-DD') for i in r] tRange.append (qTime) return tRange # Custom time format by month The result is 202011 def timeMonthRange (start=start,end=end): tRange = [] for rin arrow.Arrow.range ('months', start,end): qTime = r.format (' YYYYMM') tRange.append (qTime) return tRange for rin timeHmsRange (start,end): print (', '.join (r)) print (timeWeekRange ()) this ends the study of "how to use the Arrow time library", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report