您当前的位置: 首页 >  Python

Python编程:time时间模块-时间戳、元组形式、字符串形式相互转换

彭世瑜 发布时间:2018-01-07 19:08:27 ,浏览量:2

时间的三种形式:

时间戳(秒),元组形式,字符串形式 这里写图片描述

时间戳 timestamp

1970年1月1日计时,unix诞生于1970年,“UNIX元年” 格林威治时间1970年01月01日00时00分00秒起至现在的 总秒数

import time

x = time.time()  # 以时间戳形式,返回当前时间
print(x)  # 1515306968.886664

print(time.clock())  # 0.0
time1 = time.sleep(2)  # 延迟2秒
print(time.clock())    #上次调用clock之后的间隔
# 2.00014532747535
元组形式 struct_time

UTC coordinated universal time 世界标准时间, 格林威治时间Greenwich Mean Time(GMT),中国UTC-8 DST daylight saving time 夏令时

import time

print(time.gmtime())  # 本初子午线时间,格林威治时间
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=9, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""

y = time.localtime()  # 本地时间
print(y)
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=18, tm_sec=51,
tm_wday=6, tm_yday=7, tm_isdst=0)
"""
字符串形式
import time

z = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 格式化时间
print(z)  # 2018-01-07 16:07:47
转换
print(time.asctime(time.localtime()))  # 将元组转为字符串
# Sun Jan  7 17:23:55 2018

print(time.ctime(time.time()))   # 将时间戳转为字符串
# Sun Jan  7 17:24:24 2018

print(time.mktime(time.localtime()))  # 将元组转为时间戳
# 1515317184.0

print(time.strptime(z, "%Y-%m-%d %H:%M:%S"))  # 将字符串转为元组
"""
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=7,
tm_hour=17, tm_min=28, tm_sec=26,
tm_wday=6, tm_yday=7, tm_isdst=-1)
"""

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
"""
 2018-04-20 11:11:52.007203
 2018-04-20
"""
时间的加减
import datetime

print(datetime.datetime.now())  # 2018-01-07 17:53:23.579155

print(datetime.datetime.now()+datetime.timedelta(3)) # 默认为天
# 2018-01-10 17:53:23.579155

print(datetime.datetime.now()+datetime.timedelta(hours=3))
# 2018-01-07 20:53:23.579155
help(time)
The tuple items are:
      year (including century, e.g. 1998)
      month (1-12)
      day (1-31)
      hours (0-23)
      minutes (0-59)
      seconds (0-59)
      weekday (0-6, Monday is 0)
      Julian day (day in the year, 1-366)
      DST (Daylight Savings Time) flag (-1, 0 or 1)
   
    
Functions:
    
    time() -- return current time in seconds since the Epoch as a float
    clock() -- return CPU time since process start as a float
    sleep() -- delay for a number of seconds given as a float
    gmtime() -- convert seconds since Epoch to UTC tuple
    localtime() -- convert seconds since Epoch to local time tuple
    asctime() -- convert time tuple to string
    ctime() -- convert time in seconds to string
    mktime() -- convert local time tuple to seconds since Epoch
    strftime() -- convert time tuple to string according to format specification
    strptime() -- parse string to time tuple according to format specification
    tzset() -- change the local timezone


        
Commonly used format codes:
        
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
        
        Other codes may be available on your platform.  See documentation for
        the C library strftime function.

关注
打赏
1688896170
查看更多评论

彭世瑜

暂无认证

  • 2浏览

    0关注

    2727博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.3295s