Home >Backend Development >Python Tutorial >How to obtain and convert time in Python time module

How to obtain and convert time in Python time module

WBOY
WBOYforward
2023-05-13 12:19:061917browse

Python time module time acquisition and conversion

Python's Time library can perform time-related processing, such as accessing the current date and time, outputting time in different formats, and waiting for a specified time.

1. Get time

1.1. Timestamp

import time
timestamp = time.time()
# 1682737552.5009851

Starting from 00:00:00 on January 1, 1970, Greenwich Mean Time (GMT) The total number of seconds to date

1.2. Structured time

import time
struct_time = time.localtime()
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=6, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)

1.3. Formatted time

import time
format_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
# 2023-04-29 11:07:30 AM

%Y Year with century as a decimal number.(year)
%m Month as a decimal number [01,12].(month)
%d Day of the month as a decimal number [01,31].(日)
% H Hour (24-hour clock) as a decimal number [00,23].(hour-24 hour)
%M Minute as a decimal number [00,59].(minute)
%S Second as a decimal number [00,61].(seconds)
%z Time zone offset from UTC.(The time difference between international standard time and local time (East Eighth District, Beijing time))
%a Locale’s abbreviated weekday name.(Day of the week-abbreviation)
%A Locale’s full weekday name.(Day of the week-full name)
%b Locale’s abbreviated month name.(Month name-abbreviation)
%B Locale&rsquo ;s full month name.(month name-full name)
%c Locale’s appropriate date and time representation.(linux time format)
%I Hour (12-hour clock) as a decimal number [01 ,12].(hour-12 o'clock)
%p Locale’s equivalent of either AM or PM.(morning or afternoon)
%X hour:minute:second, and %H:%M:%S The effect is the same
%x hours/minutes/seconds

2. Time conversion

2.1. Timestamp6d267e5fab17ea8bc578f9e7e5e1570b Structured time

import time
timestamp = time.time()
# 1682738174.6577663
# 时间戳 > 结构化时间
struct_time = time.localtime(timestamp)  # 默认time.time()
# time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=16, tm_sec=14, tm_wday=6, tm_yday=120, tm_isdst=0)
# 结构化时间 > 时间戳
timestamp = time.mktime(struct_time)
# 1682738174.0

2.2 . structured time6d267e5fab17ea8bc578f9e7e5e1570b formatted time

import time
struct_time = time.localtime()
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
# 结构化时间 > 格式化时间
format_time = time.strftime('%Y-%m-%d %H:%M:%S', struct_time)
#2023-04-29 11:21:43
# 格式化时间 > 结构化时间
struct_time = time.strptime(format_time, '%Y-%m-%d %H:%M:%S')
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=-1)

The above is the detailed content of How to obtain and convert time in Python time module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete