标签:
在Django的学习过程中的时间处理过程中遇到了strftime函数,于是结合《python cookbook》和python docs 对time模块中常用的一些操作和函数做了一点总结和归纳。
代表了从特定时间点,也被称作纪元(epoch:[英] [?i:p?k] [美] [??p?k, ?i?pɑk] )开始所经历的秒数,是一个看起来不够直观的浮点数,这个时间根据不同的平台有所不同,一般为1970年1月1日午夜。例:
time.gmtime将任何时间戳(参数secs所代表的秒数)转化为一个元组,该元组为GMT(格林威治标准时),亦即UTC(世界标准时间)。也可以传递一个时间戳(从纪元开始经历的秒数)给time.localtime,会根据当前时区进行时间转化。
将struct_time转换为一个由24字符组成的字符串(24-character string),格式如:
将一个元组(tuple)或者struct_time类,即参数t,转换为指定格式的字符串
跟time.strftime相反的操作,解析给定的字符串并返回一个struct_time类。例:
可以实现在python程序中的延时,单位为秒,并支持浮点数非整数秒的延时。例:
附一:struct_time
Index | Attribute | Values |
---|---|---|
0 | tm_year | (for example, 1993) |
1 | tm_mon | range [1, 12] |
2 | tm_mday | range [1, 31] |
3 | tm_hour | range [0, 23] |
4 | tm_min | range [0, 59] |
5 | tm_sec | range [0, 61]; see (1) in strftime()description |
6 | tm_wday | range [0, 6], Monday is 0 |
7 | tm_yday | range [1, 366] |
8 | tm_isdst | 0, 1 or -1; see below |
附二:directives can be embedded in the format string
Directive | Meaning | Notes |
---|---|---|
%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. | |
%d | Day of the month as a decimal number [01,31]. | |
%H | Hour (24-hour clock) as a decimal number [00,23]. | |
%I | Hour (12-hour clock) as a decimal number [01,12]. | |
%j | Day of the year as a decimal number [001,366]. | |
%m | Month as a decimal number [01,12]. | |
%M | Minute as a decimal number [00,59]. | |
%p | Locale’s equivalent of either AM or PM. | (1) |
%S | Second as a decimal number [00,61]. | (2) |
%U | Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w | Weekday as a decimal number [0(Sunday),6]. | |
%W | Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x | Locale’s appropriate date representation. | |
%X | Locale’s appropriate time representation. | |
%y | Year without century as a decimal number [00,99]. | |
%Y | Year with century as a decimal number. | |
%Z | Time zone name (no characters if no time zone exists). | |
%% | A literal ‘%‘ character. |
参考:1. python cookbook
2. docs:http://docs.python.org/library/time.html#time.struct_time
标签:
原文地址:http://www.cnblogs.com/shouce/p/5437441.html