날짜가 있습니다 (UTC로 가정하고 있지만 현지 시간 일 수도 있음) 이 날짜를 초로 변환하고 싶습니다 ... 그리고 뒤로 (초 -> 날짜)시간을 초로 변환하고 뒤로
그래서, 유래에서 몇 가지 조사 후 나는 그 두 기능을 함께했다 :
def UTC_to_sec(utc_time):
"""
convert time in UTC to seconds.
input: s string
output: float
"""
return time.mktime(datetime.datetime.strptime(utc_time, "%Y-%m-%d %H:%M:%S").timetuple())
def sec_to_UTC(s):
"""
convert time in to seconds to UTC.
input: time in seconds (float)
output: string
"""
return datetime.datetime.utcfromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S')
# test with mydate="2010-09-15 08:31:00"
print "time ", mydate, " (UTC) --> (",UTC_to_sec(maydate), ") [s] --> ",sec_to_UTC(UTC_to_sec(mydate)), " (UTC)"
결과는 다음과 같습니다
시간 2010-09-15 08 : 31 : 00 (UTC) -> (1284532260.0) [s] -> 2010-09-15 : 31 : 00 (UTC)
mydate
은 현지 시간이 아니고 UTC가 아닌가요? 다음 함수를 수정할 때 sec_to_UTC()
I은 정확한 결과를 얻을 :
datetime.datetime.utcfromtimestamp(s-time.altzone).strftime('%Y-%m-%d %H:%M:%S')
time.altzone
제 질문은 : 앞서 말한 날짜 - 초 - 날짜 변환이 시간대에 달려있는 이유는 무엇입니까?
오 사실! 그래서 그것은 time.altzone의 값을 추가하여 mktime의 반환 값을 수정해야한다는 것을 의미합니다. 맞습니까? – Tengis