Handling of time in Python3 (continuous update ing...)

The gods are silent-personal CSDN blog post directory

This article introduces various libraries and usage scenarios for processing time in Python3

Last update time: 2023.6.2
Earliest update time: 2023.6.2

1. datetime library

Official documentation: datetime — Basic date and time types — Python 3.11.3 documentation

  1. datetime.datetime.now()
  2. datetime.datetime.today()
  3. datetime.datetime.strptime(字符串,"%Y-%m-%d")(The second input parameter is the time format)
    Code example to calculate the number of days and months between two dates:
import datetime
def days(str1,str2):
    date1=datetime.datetime.strptime(str1,"%Y-%m-%d")
    date2=datetime.datetime.strptime(str2,"%Y-%m-%d")
    num=(date1-date2).days
    return num
 
def months(str1,str2):
    year1=datetime.datetime.strptime(str1,"%Y-%m-%d").year
    year2=datetime.datetime.strptime(str2,"%Y-%m-%d").year
    month1=datetime.datetime.strptime(str1,"%Y-%m-%d").month
    month2=datetime.datetime.strptime(str2,"%Y-%m-%d").month
    num=(year1-year2)*12+(month1-month2)
    return num

print(days('2023-5-18','2023-5-10'))
print(days('2023-5-18','2021-5-10'))

output:

8
738

2. time library

  1. time.time(): returns the current time (float value)
  2. time.localtime(secs)
  3. time.sleep(秒数): sleep specified time

3. JioNLP Library: (Chinese) Extracting Time Information from Text

Extracted by regular expression.

Installation method:pip install jionlp

Time Semantic Analysis Documentation · dongrixinyu/JioNLP Wiki · GitHub
online demo: http://www.jionlp.com/jionlp_online/extract_time

Sample code:

import time
import jionlp as jio
time_text_list = ['2021年前两个季度', '从2018年12月九号到十五号', '2019年感恩节', '每周六上午9点到11点', '30~90日']
for time_text in time_text_list:
    print(jio.parse_time(time_text, time_base=time.time()))

output:

# jionlp - 微信公众号: JioNLP  Github: `https://github.com/dongrixinyu/JioNLP`.
# jiojio - `http://www.jionlp.com/jionlp_online/cws_pos` is available for online trial.
# jiojio - Successfully load C funcs for CWS and POS acceleration.
{'type': 'time_span', 'definition': 'accurate', 'time': ['2021-01-01 00:00:00', '2021-06-30 23:59:59']}
{'type': 'time_span', 'definition': 'accurate', 'time': ['2018-12-09 00:00:00', '2018-12-15 23:59:59']}
{'type': 'time_point', 'definition': 'accurate', 'time': ['2019-11-28 00:00:00', '2019-11-28 23:59:59']}
{'type': 'time_period', 'definition': 'accurate', 'time': {'delta': {'day': 7}, 'point': {'time': ['2023-06-03 09:00:00', '2023-06-03 11:00:00'], 'string': '周六上午9点到11点'}}}
{'type': 'time_delta', 'definition': 'blur', 'time': [{'day': 30.0}, {'day': 90.0}]}

You can read the documentation, time_span roughly refers to the time period (the beginning and the end are specific times), time_point is the time point, time_period is the time period, and time_delta is the length of time.

4. datefinder library: (English) extract time information from text

pypi website: datefinder PyPI
pip installation method:pip install datefinder

Sample code:

dt=list(datefinder.find_dates("ASPLOS会议的官网是https://www.asplos-conference.org/,2023年论文提交截止时间是2022年10月20日。"))

Returns a list of datetime objects.

5. pytz library

One thing to say, I actually don't understand what this library is doing, probably a unified time zone or something?
Anyway, there are some other packages that will be required to be installed by the way during installation.
pytz PyPI

Guess you like

Origin blog.csdn.net/PolarisRisingWar/article/details/130995979