zoukankan      html  css  js  c++  java
  • Unix时间戳转换

    import time

     
    def timestamp_datetime(value):
        format = '%Y-%m-%d %H:%M:%S'
        # value为传入的值为时间戳(整形),如:1332888820
        value = time.localtime(value)
        ## 经过localtime转换后变成
        ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
        # 最后再经过strftime函数转换为正常日期格式。
        dt = time.strftime(format, value)
        return dt
     
    def datetime_timestamp(dt):
         #dt为字符串
         #中间过程,一般都需要将字符串转化为时间数组
         time.strptime(dt, '%Y-%m-%d %H:%M:%S')
         ## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
         #将"2012-03-28 06:53:40"转化为时间戳
         s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
         return int(s)
     
    if __name__ == '__main__':
        d = datetime_timestamp('2012-03-28 06:53:40')
        print d
        s = timestamp_datetime(1332888820)
        print s
  • 相关阅读:
    hdoj1251 统计难题 字典树
    nyoj322 sort 归并排序,树状数组
    优先队列 如何使用
    字典树(讲解+模版)
    hdoj1069 Monkey and Banana
    ny10 skilng
    hdoj1075 What Are You Talking About
    hdoj1171 Big Event in HDU
    ny613 免费馅饼
    Spring Boot2.0之Admin-UI分布式微服务监控中心
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4308156.html
Copyright © 2011-2022 走看看