python [Datetime utils] Python的日期时间和时间戳工具#py3 #datetime#utils

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [Datetime utils] Python的日期时间和时间戳工具#py3 #datetime#utils相关的知识,希望对你有一定的参考价值。

def dt2ts(dt: Optional[datetime]) -> Optional[int]:    
    return int(time.mktime(dt.timetuple())) if dt is not None else None
  
def dt2ts_ms(dt: Optional[datetime]) -> Optional[int]:
    return dt2ts(dt) * 1000 if dt is not None else None

def ts2dt(ts: Optional[int]) -> Optional[datetime]:    
    return datetime.utcfromtimestamp(int(ts)) if ts is not None else None

def ts_ms2dt(ts: Optional[int]) -> Optional[datetime]:    
    return ts2dt(int(ts/1000) if ts is not None else None

以上是关于python [Datetime utils] Python的日期时间和时间戳工具#py3 #datetime#utils的主要内容,如果未能解决你的问题,请参考以下文章