r0xette
r0xette

Reputation: 908

Time conversion so I can insert data in mysql

Excuse my lack of knowledge playing with time in python3 but I am stuck for 30 minutes already in this problem so any help will be highly appreciated.

I have salesforce time coming in my script as 2016-11-15T23:49:48.000Z. How can I convert it into mysql datetime format without storing it in a variable.

Desired output format: 2016-11-16 12:11:21.525885

Upvotes: 0

Views: 41

Answers (1)

noahbkim
noahbkim

Reputation: 538

You can use the time module's strptime referenced here.

I'm making some assumptions about the input data, but here's a format that will probably work:

import time
import datetime
def translate_timestamp(timestamp):
    t = time.strptime(x, "%Y-%m-%dT%H:%M:%S.%fZ")
    s = datetime.datetime.fromtimestamp(time.mktime(t))
    return s.strftime("%Y-%m-%d %H:%M:%S.%f")

An example might be:

>>> translate_timestamp("2016-11-15T23:49:48.000Z")
'2016-11-15 23:49:48.000000'

Upvotes: 1

Related Questions