How to coverter string for timestamp object?

1

How can I transform a string containing a date, for example: 'Thu Jul 27 13:54:22 2017' , into a datetime object, or time?

    
asked by anonymous 27.07.2017 / 19:28

2 answers

0

Arrow

pip install arrow

import datetime
timestamp = time.localtime()
str_time = arrow.get(timestamp).format('DD-MM-YYYY HH:mm:ss ZZ')
print (str_time)

Exit:

'27-07-2017 15:39:20 +00:00'
  

Edition:
  Missing the conversion from String to TS.

ts = arrow.get('27-07-2017 15:39:20', 'DD-MM-YYYY HH:mm:ss')
print (ts)

Exit:

2017-07-27T15:39:20+00:00  

See working in repl.it.

* Remembering that repl.it's localtime is different from ours.

    
27.07.2017 / 20:46
0
def execute01():
    ''' Converte string para objeto time'''
    import time
    str_date = 'Thu Jul 27 13:54:22 2017'
    obj_date = time.strptime(str_date, "%a %b %d %H:%M:%S %Y")

    return time.strftime('%Y/%m/%d', obj_date)

def execute02():
    ''' Converte string para objeto datetime'''
    import datetime
    str_date = 'Thu Jul 27 13:54:22 2017'
    return datetime.datetime.strptime(str_date, "%a %b %d %H:%M:%S %Y").strftime("%Y/%m/%d")

def execute03():
    ''' Converte string para objeto arrow'''
    import arrow
    str_date = 'Thu Jul 27 13:54:22 2017'
    return arrow.get(str_date, 'ddd MMM DD HH:mm:ss YYYY').format('YYYY/MM/DD')

Running on link

    
27.07.2017 / 19:28