Python timedelta 对象 - strfdelta 和 deltafstr 函数用于转换 timedelta 字符串
有没有类似于strftime()
在datetime
对象上工作的方式,在Python中实现strfdelta()
和deltafstr()
这两个函数?
关于这个问题已经有类似的讨论...
...但没有一种一致的方法可以在这两种格式之间来回转换。
我想要能够从timedelta
转换为string
,然后再转换回timedelta
。
这个功能的预期用途是用于Hadoop的mapper/reducer过程(mapper脚本的中间delta时间输出,用于输入到reducer脚本中)。
1 个回答
2
在寻找可以相互转换的函数时,我没有找到合适的,于是自己写了以下两个函数,并把它们放在一个脚本里。这些函数可以在Python v2.6.6中使用,因为这个版本不支持一些更新的功能,比如timedelta.total_seconds()
:
#!/usr/bin/python
import re
import sys
import datetime
# String from Date/Time Delta:
# Takes a datetime.timedelta object, and converts the internal values
# to a dd:HH:mm:ss:ffffff string, prefixed with "-" if the delta is
# negative
def strfdelta(tdelta):
# Handle Negative time deltas
negativeSymbol = ""
if tdelta < datetime.timedelta(0):
negativeSymbol = "-"
# Convert days to seconds, as individual components could
# possibly both be negative
tdSeconds = (tdelta.seconds) + (tdelta.days * 86400)
# Capture +/- state of seconds for later user with milliseonds calculation
secsNegMultiplier = 1
if tdSeconds < 0:
secsNegMultiplier = -1
# Extract minutes from seconds
tdMinutes, tdSeconds = divmod(abs(tdSeconds), 60)
# Extract hours from minutes
tdHours, tdMinutes = divmod(tdMinutes, 60)
# Extract days from hours
tdDays, tdHours = divmod(tdHours, 24)
# Convert seconds to microseconds, as individual components
# could possibly both be negative
tdMicroseconds = (tdelta.microseconds) + (tdSeconds * 1000000 * secsNegMultiplier)
# Get seconds and microsecond components
tdSeconds, tdMicroseconds = divmod( abs(tdMicroseconds), 1000000)
return "{negSymbol}{days}:{hours:02d}:{minutes:02d}:{seconds:02d}:{microseconds:06d}".format(
negSymbol=negativeSymbol,
days=tdDays,
hours=tdHours,
minutes=tdMinutes,
seconds=tdSeconds,
microseconds=tdMicroseconds)
# Date/Time delta from string
# Example: -1:23:32:59:020030 (negative sign optional)
def deltafstr(stringDelta):
# Regular expression to capture status change events, with groups for date/time,
# instrument ID and state
regex = re.compile("^(-?)(\d{1,6}):([01]?\d|2[0-3]):([0-5][0-9]):([0-5][0-9]):(\d{6})$",re.UNICODE)
matchObj = regex.search(stringDelta)
# If this line doesn't match, return None
if(matchObj is None):
return None;
# Debug - Capture date-time from regular expression
# for g in range(0, 7):
# print "Grp {grp}: ".format(grp=g) + str(matchObj.group(g))
# Get Seconds multiplier (-ve sign at start)
secsNegMultiplier = 1
if matchObj.group(1):
secsNegMultiplier = -1
# Get time components
tdDays = int(matchObj.group(2)) * secsNegMultiplier
tdHours = int(matchObj.group(3)) * secsNegMultiplier
tdMinutes = int(matchObj.group(4)) * secsNegMultiplier
tdSeconds = int(matchObj.group(5)) * secsNegMultiplier
tdMicroseconds = int(matchObj.group(6)) * secsNegMultiplier
# Prepare return timedelta
retTimedelta = datetime.timedelta(
days=tdDays,
hours=tdHours,
minutes=tdMinutes,
seconds=tdSeconds,
microseconds=tdMicroseconds)
return retTimedelta;
这里有一些代码,用来测试在这两种格式之间的转换。timedelta
对象的构造函数参数可以更改,以测试不同的情况:
# Testing (change the constructor for timedelta to test other cases)
firstDelta = datetime.timedelta(seconds=-1,microseconds=999999, days=-1)
print "--------"
print firstDelta
firstDeltaStr = strfdelta(firstDelta)
print "--------"
print firstDeltaStr;
secondDelta = deltafstr(firstDeltaStr)
print "--------"
print secondDelta
secondDeltaStr = strfdelta(secondDelta)
print "--------"
print secondDelta
print "--------"