易截截图软件、单文件、免安装、纯绿色、仅160KB

python time 字符串转UTC

今天遇到一个一个问题,是将字符串类型的时间转化为UTC时间。例如time_str = "2009/11/09 12:23:23" 转化为UTC int型。
找了一些资料,发现time模块就能完成这个转换。
import time
time_str = "2009/11/09 12:23:23"
time_s = time.strptime(time_str,"%Y/%m/%d %H:%M:%S")
utc_f = time.mktime(time_s)
utc_i = int(time.mktime(time_s))
print time_s
(2009, 11, 9, 12, 23, 23, 0, 313, -1)
print utc_f
1257740603.0
print utc_t
1257740603

在linux中输入“date  +%s” 就能输出当前utc时间。
附录:
http://www.opengroup.org/onlinepubs/009695399/functions/strptime.html
Directive:
%a - abbreviated weekday name
%A - full weekday name
%b - abbreviated month name
%B - full month name
%c - preferred date and time representation
%C - century number (the year divided by 100, range 00 to 99)
%d - day of the month (01 to 31)
%D - same as %m/%d/%y
%e - day of the month (1 to 31)
%g - like %G, but without the century
%G - 4-digit year corresponding to the ISO week number (see %V).
%h - same as %b
%H - hour, using a 24-hour clock (00 to 23)
%I - hour, using a 12-hour clock (01 to 12)
%j - day of the year (001 to 366)
%m - month (01 to 12)
%M - minute
%n - newline character
%p - either am or pm according to the given time value
%r - time in a.m. and p.m. notation
%R - time in 24 hour notation
%S - second
%t - tab character
%T - current time, equal to %H:%M:%S
%u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1
%U - week number of the current year, starting with the first Sunday as the first day of the first week
%V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week
%W - week number of the current year, starting with the first Monday as the first day of the first week
%w - day of the week as a decimal, Sunday=0
%x - preferred date representation without the time
%X -


相关文档:

python如何连接MySQL数据库

 
#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
#=========================================================================
#
# NAME: Python MySQL test
#
# AUTHOR: benyur
# DATE  : 2004-12-28
#
# COMMENT: 这是一个python连接mysql的例子
#
#================ ......

Python读写文件


Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt' ......

被疏忽的一段Python语法

    python提供了有序(sequence)类型(字符串,元组,列表都是有序类型),并且提供了特殊的语法来方便对这些类型进行操作,最常用的有切片操作。同一有序类型的对象之间支持”+”操作符,用来连成一个新的有序对象,有序对象也可以与一个整数进行相乘,得到一个新的有序对象。在调试的时候,我经常使用这样 ......

Python开发Activex组件

Python强的功能就在于它无所不能。
使用win32com模块开发window ActiveX的示例:(如果你还没有装win32com模块的话,请到http://python.net/crew/skippy/win32/Downloads.html下载)。
# SimpleCOMServer.py
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号