美文网首页生活不易 我用python
Python 本地时间和UTC时间转换

Python 本地时间和UTC时间转换

作者: 画星星高手 | 来源:发表于2018-05-13 10:24 被阅读18次

    代码

    import time
    import datetime
    def utc2local(utc_st):
        """UTC时间转本地时间(+8: 00)"""
        now_stamp = time.time()
        local_time = datetime.datetime.fromtimestamp(now_stamp)
        utc_time = datetime.datetime.utcfromtimestamp(now_stamp)
        offset = local_time - utc_time
        local_st = utc_st + offset
        return local_st
    
    
    def local2utc(local_st):
        """本地时间转UTC时间(-8: 00)"""
        time_struct = time.mktime(local_st.timetuple())
        utc_st = datetime.datetime.utcfromtimestamp(time_struct)
        return utc_st
    
    if __name__ == '__main__':
        year = int(time.strftime("%Y"))
        month = int(time.strftime("%m"))
        day = int(time.strftime("%d"))
        hour = int(time.strftime("%H"))
        minute = int(time.strftime("%M"))
        second = int(time.strftime("%S"))
        local_time = datetime.datetime(year, month, day, hour, minute, second)
        utc_time = local2utc(local_time)
    
    image.png

    相关文章

      网友评论

        本文标题:Python 本地时间和UTC时间转换

        本文链接:https://www.haomeiwen.com/subject/hxokdftx.html