How do I print a datetime in the local timezone?

后端 未结 6 1971
夕颜
夕颜 2020-12-03 06:38

Let\'s say I have a variable t that\'s set to this:

datetime.datetime(2009, 7, 10, 18, 44, 59, 193982, tzinfo=)

If I say s

6条回答
  •  没有蜡笔的小新
    2020-12-03 07:23

    Think your should look around: datetime.astimezone()

    http://docs.python.org/library/datetime.html#datetime.datetime.astimezone

    Also see pytz module - it's quite easy to use -- as example:

    eastern = timezone('US/Eastern')
    

    http://pytz.sourceforge.net/

    Example:

    from datetime import datetime
    import pytz
    from tzlocal import get_localzone # $ pip install tzlocal
    
    utc_dt = datetime(2009, 7, 10, 18, 44, 59, 193982, tzinfo=pytz.utc)
    print(utc_dt.astimezone(get_localzone())) # print local time
    # -> 2009-07-10 14:44:59.193982-04:00
    

提交回复
热议问题