import time python_Python中的time模块与datetime模块用法总结

1

2

3

4

5

6

7

8

import time

# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=24,

tm_hour=14, tm_min=17, tm_sec=26,

tm_wday=4, tm_yday=114, tm_isdst=0)

# 2015

print time.localtime()

print time.localtime().tm_year

函数

time.time(): 返回一个时间戳

time.asctime([t]): 转换gmtime()和localtime()返回的元组或struct_time为string.

time.clock(): 在第一次调用的时候, 返回程序运行的时间. 第二次之后返回与之前的间隔.

time.ctime([secs]): 将时间戳转换为时间字符串, 如没有提供则返回当前的时间字符串,并与asctime(localtime())一样.

time.gmtime([secs]): 将时间戳转化为, UTC 时区的struct_time.

time.localtime([secs]): 类似gmtime()但会把他转换成本地时区.

time.mktime(t): struct_time 转化为时间戳.

time.sleep(secs): 线程推迟指定时间, 以秒为单位.

time.strftime(format[,t]): 根据参数转换一个sturc_time或元组为字符串.

time.strptime(string[, format]): 与strftime相反,返回一个struct_time.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

import time

# Fri Apr 24 06:39:34 2015

print time.asctime(time.gmtime())

# 0.0

# None

# 1.01136392961 因计算机而异

print time.clock()

print time.sleep(1)

print time.clock()

# Fri Apr 24 14:42:07 2015

print time.ctime()

# 2015-04-24

print time.strftime('%Y-%m-%d', time.localtime())

# 1429857836.0

print time.mktime(time.localtime())

time模块中常用的格式化字符串

%y 两位数的年份 00 ~ 99.

%Y 四位数的年份 0000 ~ 9999

%m 月份 01 ~ 12.

%d day 01 ~ 31.

%H 时 00 ~ 23.

%I 时 01 ~ 12.

%M 分 00 ~ 59.

%S 秒 00 ~ 61.

datetime.MINYEAR 和 datetime.MAXYEAR 模块常量表示datetime接受的范围

class datetime.date: 一个理想化的日期, 提供year, month, day属性

class datetime.time: 一个理想化的时间, 提供hour, minute, second, microsecond, tzinfo.

class datetime.datetime: 日期和时间的组合.提供year, month, day, hour, minute, second, microsecond, tzinfo.

class datetime.timedelta: 表达两个date,time和datetime持续时间内的微妙差异.

class datetime.tzinfo: 时间对象的抽象基类.

1

2

3

4

5

6

7

8

9

from datetimeimport timedelta, datetime

a= datetime.now()

b= timedelta(days=7)

# 7 days, 0:00:00

# 2015-04-14 16:02:39.189000

print b

print a- b

下面说具体说一下类和类的方法

date类

一个date对象代表理想化的日期.

1

2

3

4

5

6

class datetime.date(year, month, day)

# All arguments are required. Arguments may be ints or longs.

# 所有参数都是必须的. 参数可能是 int 或 long.

MINYEAR <= year <= MAXYEAR

1<= month <= 12

1<= day <= number of daysin the given monthand year.(随着月份和年份)

如果参数脱离给的范围会抛出, valueError.

1.类方法 >`date.today()`:返回当前的本地日期, 这等价于 `date.fromtimestamp(time.time())`.

Return the current local date. This is equvalent to `date.fromtimestamp(time.time())`.

1

2

3

4

from datetimeimport date

# print 2015-04-21

print date.today()

2.date.fromtimestamp(timestamp):根据提供的时间戳返回local date. 时间戳常用于对时间类型的存储.

1

2

3

4

5

6

7

import time

from datetimeimport date

# 1429587111.21

# 2015-04-21

print time.time()

print date.fromtimestamp(time.time())

3.类方法date.fromordinal(ordinal):根据提供的Gregorian日历返回date.(不做描述)

类属性

date.min: 返回 date(MINYEAR, 1, 1).

date.max: 返回 date(MAXYEAR, 12, 31).

date.year: 返回 年, MINYEAR和MAXYEAR之间

date.month: 返回 月, 1到12月之间

date.day: 返回 1到 n 之间.

1

2

3

d= date(2014,4,21)

# 2014 4 21

print d.year, d.month, d.day

实例方法

date.replace(year, month, day):返回一个相同值的data对象, 除了这些参数给关键字指定新的值.

date.timetuple(): 返回一个time.struct_time对象.

date.toordinal(): 返回一个Gregoian Calendar对象.

date.weekday(): 返回day of the week. 星期一为0,星期日为6.

date.isoweekday(): 返回day of the week. 星期一为1,星期日为7.

date.isocalendar(): 返回一个三元组, (ISO year, ISO week number, ISO weekday).

date.isoformat(): 返回 一个'YYYY-MM-DD'的字符串格式.

date.ctime(): 返回一个字符串日期, d.ctime() 等同于 time.ctime(time.mktime(d.timetuple())).

date.strftime(format): 返回一个字符串日期, 格式自定义.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

d= date(2015,4,21)

# 2015-04-21

# 2015-04-21

# 2015-04-22

print d

print d.replace()

print d.replace(day=22)

# time.struct_time(tm_year=2015, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=111, tm_isdst=-1)

print d.timetuple()

# print 1

# print 2

print d.weekday()

print d.isoweekday()

# print 2015-04-21

print d.isoformat()

# print 21/04/2015

print d.strftime('%d/%m/%y')

datetime 类

datetime 对象是一个单一的对象, 包含所有date和time对象的信息.

1

2

3

4

5

6

7

8

9

10

11

12

13

class datetime.datetime(year, month, day[, hour

[, minute

[, second

[, microsecond

[, tzinfo]]]]])

# The year, month and day arguments are required.

MINYEAR <= year <= MAXYEAR

1 <= month <= 12

1 <= day <= n

0 <= hour <24

0 <= minute <60

0 <= second <60

0 <= microsecond <10**6

类方法

datetime.today(): 返回当前本地datetime.随着 tzinfo None. 这个等同于datetime.fromtimestamp(time.time()).

datetime.now([tz]): 返回当前本地日期和时间, 如果可选参数tz为None或没有详细说明,这个方法会像today().

datetime.utcnow(): 返回当前的UTC日期和时间, 如果tzinfo None ,那么与now()类似.

datetime.fromtimestamp(timestamp[, tz]): 根据时间戳返回本地的日期和时间.tz指定时区.

datetime.utcfromtimestamp(timestamp): 根据时间戳返回 UTC datetime.

datetime.fromordinal(ordinal): 根据Gregorian ordinal 返回datetime.

datetime.combine(date, time): 根据date和time返回一个新的datetime.

datetime.strptime(date_string, format): 根据date_string和format返回一个datetime.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

from datetimeimport datetime

# 2015-04-21 14:07:39.262000

print datetime.today()

# 2015-04-21 14:08:20.362000

print datetime.now()

# 1429596607.06

# 2015-04-21 14:10:07.061000

t= time.time()

print t

print datetime.fromtimestamp(t)

from datetimeimport datetime, date, time

a= date(2015,4,21)

b= time(14,13,34)

# 2015-04-21 14:13:34

print datetime.combine(a, b)

实例方法

datetime.date(): 返回相同年月日的date对象.

datetime.time(): 返回相同时分秒微秒的time对象.

datetime.replace(kw): kw in [year, month, day, hour, minute, second, microsecond, tzinfo], 与date类似.

其他方法可查看官方文档…

1

2

3

4

5

6

7

from datetimeimport datetime, date, time

td= date(2015,4,21)

n= time(14,28,30)

# 2099-04-21 14:30:42.103000

print datetime.now(0.replace(year=2099)

类属性

datetime.min: datetime(MINYEAR, 1, 1).

datetime.max: datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999).

实例属性(read-only)

datetime.year: 1 至 9999

datetime.month: 1 至 12

datetime.day: 1 至 n

datetime.hour: In range(24). 0 至 23

datetime.minute: In range(60).

datetime.second: In range(60).

datetime.microsecond: In range(1000000).

time类

time 代表本地(一天内)时间.

1

2

3

4

5

6

7

8

9

10

11

class datetime.time([hour

[, minute

[, second

[, microsecond

[, tzinfo]]]]])

# All arguments are optional.

# 所有参数都是可选的.

0 <= hour <24

0 <= minute <60

0 <= second <60

0 <= microsesond <10**6

time类就是对时间的一些操作,其功能类似与datetime.其实date和time就是对datetime中日期和时间的操作.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言都有相应的实现方式,可以应用于各种不同的场景。C语言的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值