Python - datetime of a specific timezone

前端 未结 2 762
自闭症患者
自闭症患者 2021-02-05 21:42

I am having the hardest time trying to get the current time in EDT timezone.

print datetime.time(datetime.now()).strftime(\"%H%M%S\")

datetime.

2条回答
  •  清酒与你
    2021-02-05 22:24

    I am not very conversent about the EDT time zone but this example should serve your purpose.

    import datetime
    

    datetime.datetime.now must be passed the time zone info which should be of type datetime.tzinfo. Here is a class that implements that with some of the required functions. I am providing no day light saving details here as this is an example.

    class EST(datetime.tzinfo):
        def utcoffset(self, dt):
          return datetime.timedelta(hours=-5)
    
        def dst(self, dt):
            return datetime.timedelta(0)
    

    Now you could use this to get the info with time zone correctness:

    print datetime.datetime.now(EST()) 
    

    Output:

    2010-11-01 13:44:20.231259-05:00 
    

提交回复
热议问题