Created
May 29, 2011 14:44
-
-
Save sgk/997821 to your computer and use it in GitHub Desktop.
Timezone conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# by sgk | |
import datetime | |
class TZ(datetime.tzinfo): | |
def __init__(self, name, offset): | |
self.name_ = name | |
self.offset_ = offset | |
def utcoffset(self, dt): | |
return datetime.timedelta(hours=self.offset_) | |
def tzname(self, dt): | |
return self.name | |
def dst(self, dt): | |
return datetime.timedelta(0) | |
UTC = TZ('UTC', 0) | |
JST = TZ('JST', 9) | |
def jst_from_utc(dt): | |
return dt.replace(tzinfo=UTC).astimezone(JST) | |
def utc_from_jst(dt): | |
return dt.replace(tzinfo=JST).astimezone(UTC) | |
def as_jst(dt): | |
return dt.astimezone(JST) | |
def as_utc(dt): | |
return dt.astimezone(UTC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment