[PYTHON] Get JST current time in ISO 8601 notation

#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

Get JST current time in ISO 8601 notation
python get current time
Get the current date and time in Python, considering the time difference
Get date and time in specified format
Conversion of time data in 25 o'clock notation
Get and convert the current time in the system local timezone with python
Get time series data from k-db.com in Python
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
Get drawing information every set time in Tkinter
Get standard output in real time with Python subprocess
Get your current location and user agent in Python
Get date in Python
Class notation in Python
[Go language] How to get terminal input in real time
Get YouTube Live chat field in real time with API