时间处理模块time

一、时间概念

1.1 时间戳  

  时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总
  秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。

1.2 时区

  为了时间各国的时间能统一,使用了时区的概念。以格林尼治为0时区,总24个时区。 东+,西-

1.3 夏令时

  为了节约夏天的能源,开发出来的一个调节时间。 然后实际证明没什么软用。 中国以前也用过,但是取消掉了。

二、python代码中的时间

  时间有三种格式:

      时间戳(timestamp)

      时间字符串(Format String)

      结构化的时间(struct_time):(year=2018, month=5, day=1, hour=16, min=37, sec=6)

  python中的时间模块

      time

      datetime

      python-dateutil (pip install)  

2.1 time 模块

  常用方法:

    time.time()   当前的时间戳

    time.sleep()   停止多少时间

2.1.1 时间戳 与 结构化时间 之间相互转换

import time
print(time.localtime()) # 无参数的时候,表示取当前时间
print(time.localtime(0)) # 0时间戳,在中国是 70年8点
print(time.mktime(time.localtime()))

2.1.2 结构化时间 与 格式化字符串时间 相互转换

一、  strftime : tuple -> 字符串时间

  strftime(format[, tuple]) -> string

两个参数:

    format:我们定义的格式

    tuple为可选参数,不写的时候,使用当前的时间。

  format的字符:

%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.
>>> time.strftime('%Y-%m-%d %X', time.localtime())

'2018-06-24 13:14:35'

二 、 字符串时间 到 结构化时间转换

    strptime(string, format) -> struct_time

    把一个时间的字符串,按照格式进行转化

time.strptime('2018-05-06 22:52:40', "%Y-%m-%d %X")

2.1.3 结构化时间和时间戳 到 固定字符串时间的转换

  字符串时间的格式固定

# 结构化时间 到 固定字符串时间
>>> time.asctime()
'Sun Jun 24 13:40:09 2018'
>>> time.asctime(time.localtime(0))
'Thu Jan 1 08:00:00 1970'

# 时间戳 到 固定字符串时间
>>> time.ctime()
'Sun Jun 24 13:40:39 2018'
>>> time.ctime(0)
'Thu Jan 1 08:00:00 1970'

2.2 datetime 模块

datetime包含的模块如何:

  date 日期模块 包含年 月 日
  time 时间模块 包含时 分 秒 微秒
  datetime 日期时间模块 包含了时间全部东西
  timedelta 日期计算模块

2.2.1 date模块

  只包含日期

a = datetime.date(2018, 11, 11) # 生成一个date对象
print(a.year, a.month, a.day)
from datetime import date
date.today()
print(a.today())
a.year
a.mouth
a.day

2.2.2 time模块

  只包含时间

a = datetime.time(12, 23, 24, 12) # 生成一个时间对象,包含了时分秒
print(a)
a.hour
a.minute
a.second
print(a.microsecond)

2.2.3 datetime模块

  包含日期与时间

datetime类

```
a = datetime.datetime.now() # 获取当前时间的datetime类
b = datetime.datetime(2018, 2, 19, 12, 15) # 自己生成一个datetime对象

字符串格式化,使用Y m d等提取datetime对象中相对应的参数

c = '{dt:%Y-%m-%d %H}'.format(dt=datetime.datetime.now())
c = f'{datetime.datetime.now():%Y-%m-%d}'
print(c)时间戳 转 datatime对象python

时间戳 转 datime对象

import time
from datetime import datetime
a = time.time()
print('a', datetime.utcfromtimestamp(0))
print('a', datetime.utcfromtimestamp(a))
datetime 与 字符串时间之间的转换
```

# 格式化字符串表示时间
# strftime: % -> 1999-12-12, 把代码里面的时间对象转成人类认识的字符串,f:format
# strptime: 2000-12-12 -> object,把人类认识的字符串,转成代码里面的对象,p,parse
dt = datetime(2018,12,12,12,12)
print(dt.strftime('%Y-%m-%d %X'))
print(dt.strftime('%A %B %Y'))

f = dt.strptime('2018-12-12 12:12:00', '%Y-%m-%d %X') # 生成一个新的dt对象
fb = dt.strptime('Wednesday December 2018', '%A %B %Y')
print(fb, type(fb))

2.2.4 timedelta 计算模块

# 时间的计算
from datetime import timedelta
from datetime import date
a = timedelta(days=1)
# timedelta(hours=12)
today = date.today()
future = today + a*10
print(future, type(future))

c = future - today
print(c, type(c))

猜你喜欢

转载自www.cnblogs.com/louhui/p/9222784.html
今日推荐