#ISO 8601 basic notation example) "20140526T123456+0900"
jstnow().strftime('%Y%m%dT%H%M%d%z')
#ISO 8601 extended notation example) "2014-05-26T12:34:56+0900"
jstnow().strftime('%Y-%m-%dT%H:%M:%d%z')
#ISO 8601 extended notation example) "2014-05-26T12:34:56.123000+0900"
jstnow().isoformat()
from datetime import *
def jstnow():
# JST timezone(+0900)
class JST(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=9)
def dst(self, dt):
return timedelta(0)
return datetime.now(JST())
Implementation that explicitly calculates the difference from UTC
from datetime import *
def jstnow():
# UTC tzinfo
class UTC(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=0)
def dst(self, dt):
return timedelta(0)
# JST timezone(+0900)
class JST(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=9)
def dst(self, dt):
return timedelta(0)
return datetime.utcnow().replace(tzinfo=UTC()).astimezone(JST())
Recommended Posts