LoginSignup
7
6

More than 5 years have passed since last update.

ISO8601表記でJST現在時刻を取得

Last updated at Posted at 2014-05-26
# ISO 8601基本表記  例) "20140526T123456+0900"
jstnow().strftime('%Y%m%dT%H%M%d%z')
# ISO 8601拡張表記  例) "2014-05-26T12:34:56+0900"
jstnow().strftime('%Y-%m-%dT%H:%M:%d%z')
# ISO 8601拡張表記  例) "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())

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())
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6