python_ time stamp format conversion encapsulation function and

1, the time stamp format conversion

Import Time 

DEF   timestamp_to_str (timestamp = None, the format = ' % Y-M-% D%% H:% M:% S ' ):
     '' ' this is to convert the time stamp into actual formatted, if not pass stamp, it returns the current time '' ' 
    IF timestamp:
         return   the time.strftime (the format, time.localtime (timestamp)) 

    the else :
         return   the time.strftime (the format, time.localtime ()) 


Print (timestamp_to_str (1554307200, ' % Y-M-% D% ' ))
 Print (timestamp_to_str (the format = ' % Y-M-% D% ' ))
 Print (timestamp_to_str ())

/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/test.py
2019-04-04
2019-06-23
2019-06-23 21:45:49

2, a timestamp format conversion time

Import Time 

DEF str_to_timestamp (STR = None, the format = ' % Y-M-% D%% H:% M:% S ' :)
     # , if not pass formatted formatted time stamp the time of rotation , returns the current timestamp 
    IF STR:
         return int (time.mktime (the time.strptime (STR, the format)))
     the else :
         return int (the time.time ()) 

Print (str_to_timestamp ())
 Print (str_to_timestamp ( ' 2019 12:12:34 -04-04 ' ))
 Print (str_to_timestamp ( ' 2019-07-09 ' , ' % Y-M-% D% ' ))


/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/test.py
1561298040
1554351154
1562601600

 

Guess you like

Origin www.cnblogs.com/xiaokuangnvhai/p/11074573.html