python中的时间表示

datetime模块

简介

在python参考手册8.1节

There are two kindsof date and time objects: “naive”and “aware

An aware object has sufficient knowledge of applicable algorithmic and politicaltime adjustments to locate itself relative to other aware objects

A naive object does not contain enough information to unambiguouslylocate itself relative to other date/time objects

Datatime模块包含如下的子类

Available Types

A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendarindefinitely extended in both directions

A time object represents a (local) time of day, independent of any particular day

A datetime object is a single object containing all the information from a date object and atime object.

Like a date object, datetime assumes thecurrent Gregorian calendar extended in both directions

Like a time object,datetime assumes thereare exactly 3600*24 seconds in every day

3种表示时间方式

1)时间戳

2)格式化的时间字符串

3)元组(struct_time)共九个元素

date类

  1. __str__():返回日期的字符串;

  1. ctime():返回ctime格式的字符串;

  1. __format__(...):返回指定格式的日期字符串;

  1. strftime(...):返回指定格式的日期字符串,与方法3等价

a = datetime.date(2018, 6, 19)
a
-- > datetime.date(2018, 6, 19)

a.__str__()
-- > '2018-06-19'

a.ctime()
-- > 'Tue Jun 19 00:00:00 2018'

a.__format__('%Y/%m/%d')
-- > '2018/06/19’

a.strftime('%Y-%m-%d')
-- > '2018-06-19'

time类

  1. __str__():返回时间的字符串;

  1. isoformat():返回符合ISO标准的时间字符串;

  1. __format__(..):返回指定格式的时间字符串;

  1. strftime(...):返回指定格式的时间字符串,与方法3等价

a = datetime.time(12,20,59,899)
a
datetime.time(12, 20, 59, 899)

a.__str__()
-- > '12:20:59.000899'

a.isoformat()
-- > '12:20:59.000899'

a.strftime('%H:%M:%S')
-- > '12:20:59'

a.__format__('%H:%M:%S')
-- > '12:20:59'

datetime类

可以看做是date类和time类的合体,大部分的方法和属性都继承于这2个类,其数据构成也是由这2个类的所有属性构成

now():返回当前日期时间的datetime对象;

date():返回datetime对象的日期部分;

time():返回datetime对象的时间部分;

utcnow():返回当前日期时间的UTC的datetime对象;

  1. __str__():返回日期的字符串;

  1. ctime():返回ctime格式的字符串;

  1. __format__(...):返回指定格式的日期字符串;

  1. strftime(...):返回指定格式的日期字符串,与方法3等价

timedelta类

· 类构成:计算2个datetime对象的差值;

· 有7个可选参数,默认值为0:datetime.timedelta(weeks=0,days=0, hours=0, minutes=0, second=0, microsecond=0, milliseconas=0);

str(t)

Returns a string in the form [D day[s], ][H]H:MM:SS[.UUUUUU], where D

is negative for negative t. (5)

repr(t)

Returns a string in the form datetime.timedelta(D[, S[, U]]), where D

is negative for negative t. (5)

time模块

在python参考手册16.3节

注意datetime模块中time类与time模块不是一个概念

得到struct_time的函数:time.localtime()

得到timestamp的函数:time.time()

得到format string time的函数:time.asctime()或time.ctime()

time类的转化函数如下,下面都是time类的类成员函数:

方法一:

方法二:

上面的是24字符时间,例如“TueDec 11 18:07:14 2008”,与上面的Format string是不同的,Format string可以自己定义格式;

struct_time转24字符:

import time

time.asctime(time.localtime())

timestamp转24字符:

import time

asctime(localtime(secs))

datetime模块中datetime和time模块中的转化

可以通过字符串相互转化

python中计算运行时间

方法1

import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
print (endtime - starttime).seconds

方法 2

start = time.time()
run_fun()
end = time.time()
print end-start

方法3

start = time.clock()
run_fun()
end = time.clock()
print end-start

方法1和方法2都包含了其他程序使用CPU的时间,是程序开始到程序结束的运行时间。

方法3算只计算了程序运行的CPU时间

方法4

start = time.perf_counter()
run_fun()
end = time.perf_counter()
print end-start

方法5

start = time.process_counter()
run_fun()
end = time.process_counter()
print end-start
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值