Python模块:时间表示转换器
wibrst
|
1#
wibrst 发表于 2007-04-29 17:35
Python模块:时间表示转换器
今天作加班时间计算,将这个模块整理,升级了一下:
[color="#008080"]# encoding:gb2312 [color="#008080"]# [color="#008080"]# revision 2007.04.29 : [color="#008080"]# 正则升级为带组名, [color="#008080"]# 升级局部变量命名规则, [color="#008080"]# 升级itv2time返回值表达式 [color="#008080"]# revision 2007.03.31 : [color="#008080"]# 启动版本, [color="#008080"]# 基本功能实现 [color="#008080"]# you can use for : [color="#008080"]# 计算与日期无关的时分秒级别的时间差 [color="#008080"]# 如计算加班时间,电影字幕时间修正 [color="#008080"]# timeItv.py [color="#0000ff"]import [color="#800000"]re [color="#008080"]# 将计时器"时:分:秒"字符串转换为秒数间隔 [color="#0000ff"]def [color="#000000"]time2itv(_sTime): [color="#000000"]_sP[color="#008080"]=[color="#ff00ff"]"^(?P[0-9]+):(?P[0-5][0-9]):(?P[0-5][0-9])$" [color="#000000"]_p[color="#008080"]=[color="#800000"]re[color="#008080"].[color="#808000"]compile[color="#000000"](_sP) [color="#000000"]_mTime[color="#008080"]=[color="#000000"]_p[color="#008080"].[color="#808000"]match[color="#000000"](_sTime) [color="#0000ff"]if [color="#000000"]_mTime: [color="#000000"]t[color="#008080"]=[color="#808000"]map[color="#000000"]([color="#808000"]int[color="#000000"],_mTime[color="#008080"].[color="#808000"]group[color="#000000"]([color="#ff00ff"]'h'[color="#000000"],[color="#ff00ff"]'m'[color="#000000"],[color="#ff00ff"]'s'[color="#000000"])) [color="#0000ff"]return [color="#000000"]3600[color="#008080"]*[color="#000000"]t[0][color="#008080"]+[color="#000000"]60[color="#008080"]*[color="#000000"]t[1][color="#008080"]+[color="#000000"]t[2] [color="#0000ff"]else[color="#000000"]: [color="#0000ff"]return [color="#ff00ff"]"[InModuleError]:time2itv(_sTime) invalid argument value" [color="#008080"]# 将秒数间隔转换为计时器"时:分:秒"字符串 [color="#0000ff"]def [color="#000000"]itv2time(_iItv): [color="#0000ff"]if [color="#808000"]type[color="#000000"](_iItv)[color="#008080"]==[color="#808000"]type[color="#000000"](1): [color="#000000"]h[color="#008080"]=[color="#000000"]_iItv[color="#008080"]/[color="#000000"]3600 [color="#000000"]sUp_h[color="#008080"]=[color="#000000"]_iItv[color="#008080"]-[color="#000000"]3600[color="#008080"]*[color="#000000"]h [color="#000000"]m[color="#008080"]=[color="#000000"]sUp_h[color="#008080"]/[color="#000000"]60 [color="#000000"]sUp_m[color="#008080"]=[color="#000000"]sUp_h[color="#008080"]-[color="#000000"]60[color="#008080"]*[color="#000000"]m [color="#000000"]s[color="#008080"]=[color="#000000"]sUp_m [color="#0000ff"]return [color="#ff00ff"]'%02i:%02i:%02i'[color="#008080"]%[color="#000000"](h,m,s) [color="#0000ff"]else[color="#000000"]: [color="#0000ff"]return [color="#ff00ff"]"[InModuleError]:itv2time(_iItv) invalid argument type" [color="#0000ff"]if [color="#008080"]__name__==[color="#ff00ff"]"__main__"[color="#000000"]: [color="#008080"]# 用法示例仅供测试 [color="#000000"]sTime[color="#008080"]=[color="#ff00ff"]"1223:34:15" [color="#000000"]itv[color="#008080"]=[color="#000000"]time2itv(sTime) [color="#0000ff"]print [color="#000000"]itv [color="#008080"]# 4404855 [color="#0000ff"]print [color="#000000"]itv2time(itv) [color="#008080"]# 1223:34:15 [color="#008080"]# !不合约定的参数 [color="#0000ff"]print [color="#000000"]time2itv([color="#ff00ff"]"12:34:95"[color="#000000"]) [color="#0000ff"]print [color="#000000"]time2itv([color="#ff00ff"]"sfa123"[color="#000000"]) [color="#008080"]# print time2itv(itv) [color="#0000ff"]print [color="#000000"]itv2time([color="#ff00ff"]"451223"[color="#000000"]) [color="#0000ff"]print [color="#000000"]itv2time(sTime) [color="#0000ff"]print [color="#000000"]itv2time(time2itv([color="#ff00ff"]'19:12:00'[color="#000000"]) [color="#008080"]- [color="#000000"]time2itv([color="#ff00ff"]'09:03:00'[color="#000000"]) ) |