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

What are the Python time and date libraries?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly talks about "what are the Python time and date library". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the Python time and date libraries"?

1 、 Arrow

Arrow is a lightweight Python library that specializes in time and date processing. It provides a reasonable and intelligent way to create, manipulate, format, and convert time and date, and provides a smart module API that supports many common build scenarios. To put it simply, it can help you use dates and times with easier operation and less code. Its design inspiration mainly comes from moment.js and requests.

Quick start

$pip install arrow > import arrow > utc = arrow.utcnow () > > utc > utc = utc.replace (hours=-1) > > utc > local = utc.to ('US/Pacific') > > local > arrow.get (' 2013-05-11T21T21VRV 58.970460') > > local.timestamp 1368303838 > > local.format () '2013-05-11 13:23:58-077Flt' > > local.format ('YYYY-MM-DD HH:mm:ss ZZ')' 2013 -05-11 13:23:58-07 local.humanize ()'an hour ago' > local.humanize (locale='ko_kr')'1 goat'

2 、 Delorean

Delorean provides a better abstraction than datetime and pytz, making it easier for you to deal with time. It has many useful features for dealing with time zones, standardizing time zones or changing from one time zone to another.

Quick start

From datetime import datetime import pytz est = pytz.timezone ('US/Eastern') d = datetime.now (pytz.utc) d = est.normalize (d.astimezone (est)) return dfrom delorean import Delorean d = Delorean () d = d.shift (' US/Eastern') return d

3 、 Pendulum

Native datetime is sufficient to cope with basic situations, but when faced with more complex use cases, it is often stretched and less intuitive. Pendulum provides a simpler, easier-to-use API based on the standard library, designed to make Python datetime easier to use.

Quick start

> import pendulum > now_in_paris = pendulum.now ('Europe/Paris') > now_in_paris' 2016-07-04T00VRV 58.50211602VOV 0000Seamless timezone switching > now_in_paris.in_timezone ('UTC')' 2016-07-03T2249VRV 58.50211603T2249VRV 58.50211600VLV > > tomorrow = pendulum.now (). Add (days=1) > last_week = pendulum.now (). Subtract (weeks=1) > > if pendulum.now (). Print ('partyopia') Past = pendulum.now (). Subtract (minutes=2) > > past.diff_for_humans () >'2 minutes ago' > delta = past-last_week > delta.hours 23 > delta.in_words (locale='en')'6 days 23 hours 58 minutes' # Proper handling of datetime normalization > pendulum.create (2013, 3, 31, 2, 30, 0, 0) 'Europe/Paris')' 2013-03-31T03-31T03VR 30Proper handling of dst transitions 00' # 2:30 does not exist (Skipped time) # Proper handling of dst transitions > just_before = pendulum.create (2013, 3, 31, 1, 59, 59, 999999, 'Europe/Paris')' 2013-03-31T01Proper handling of dst transitions 59.99999999 01purl 0000' > > just_before.add (microseconds=1) '2013-03-31T03VR 00Rose 0002purl'

4 、 dateutil

Dateutil is an extension of the datetime standard library, which supports general parsing of dates in almost all string formats, flexible date calculation and timely update of internal data.

Quick start

> > from dateutil.relativedelta import * > > from dateutil.easter import * > > from dateutil.rrule import * > > from dateutil.parser import * > > from datetime import * > > now = parse ("Sat Oct 11 17:13:46 UTC 2003") > today = now.date () > year = rrule (YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR) [0] .year > > rdelta = relativedelta (easter (year)) Today) > print ("Today is:% s"% today) Today is: 2003-10-11 > print ("Year with next Aug 13th on a Friday is:% s"% year) Year with next Aug 13th on a Friday is: 2004 > print ("How far is the Easter of that year:% s"% rdelta) How far is the Easter of that year: relativedelta (months=+6) > print ("And the Easter of that year is:% s"% (today+rdelta)) And the Easter of that year is: 2004-04-11

5 、 moment

The Python library for dealing with date / time is also inspired by moment.js and requests, and the design concept is derived from the Times Python module.

Usage

Import moment from datetime import datetime # Create a moment from a string moment.date (12-18-2012) # Create a moment with a specified strftime format moment.date (12-18-2012, "% m-%d-%Y") # Moment uses the awesome dateparser library behind the scenes moment.date ("2012-12-18") # Create a moment with words in it moment.date ("December 18" 2012 ") # Create a moment that would normally be pretty hard to do moment.date (" 2 weeks ago ") # Create a future moment that would otherwise be really difficult moment.date (" 2 weeks from now ") # Create a moment from the current datetime moment.now () # The moment can also be UTC-based moment.utcnow () # Create a moment with the UTC time zone moment.utc (" 2012-12-18 ") # Create a moment from a Unix timestamp moment.unix (1355875153626) # Create a moment from a Unix UTC timestamp moment.unix (1355875153626 Utc=True) # Return a datetime instance moment.date (2012, 12, 18). Date # We can do the same thing with the UTC method moment.utc (2012, 12, 18). Date # Create and format a moment using Moment.js semantics moment.now (). Format ("YYYY-M-D") # Create and format a moment with strftime semantics moment.date (2012, 12,18). Strftime ("% Y-%m-%d") # Update your moment's time zone moment.date (datetime (2012, 12) 18). Locale ("US/Central"). Date # Alter the moment's UTC timezone to a different timezone moment.utcnow (). Timezone ("US/Eastern"). Date # Set and update your moment's time zone. For instance, I'm on the # west coast, but want NYC's current time. Moment.now (). Locale ("US/Pacific"). Timezone ("US/Eastern") # In order to manipulate time zones, a locale must always be set or # you must be using UTC. Moment.utcnow (). Timezone ("US/Eastern"). Date # You can also clone a moment, so the original stays unaltered now = moment.utcnow () .timezone ("US/Pacific") future = now.clone () .add (weeks=2)

6 、 When.py

Provides very user-friendly features to help perform common date and time operations.

Usage

At this point, I believe you have a deeper understanding of "Python time and date library". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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