0
    from dateutil.parser import parse
    from dateutil.relativedelta import relativedelta
    from dateutil.tz import gettz
    from datetime import datetime, timedelta

    input_time = '2019-02-01 09:50:08+11:00'

    parsed=parse(input_time)
    print parsed.tzinfo

I have a time input string:

       input_time = '2019-02-01 09:50:08+11:00'

I want to convert it into this format: YYYY-MM-DD HH:MM:SS. Basically, adding the offset to the actual time object. For the above example I am looking for below output:

      input_time_converted = '2019-02-01 20:50:08'

Found some useful stuff in dateutil library to parse the date object passed as a string and get the offset details but it gives me this output:

          tzoffset(None, 39600)

But I don't know how to get the actual digits from the above and do the rest of the maths.

I have tried to call it with -as explained in the official dateutil parser documentation-

   print parsed.tzinfo.tzoffset

But didn't work.

9
  • It's not a duplicate that answer applies to python3 this is python2. omg Commented Feb 6, 2019 at 2:29
  • Are your input times really messed up like that where they are UTC times plus the timezone offset they should have, but don't? Commented Feb 6, 2019 at 2:33
  • naaah, all of the inputs will be as same as the example. With UTC time plus/minus the offset. Commented Feb 6, 2019 at 2:35
  • Just so you know, you have the wrong expected date time for UTC. You want to do the opposite of what the offset is, so your result should be '2019-01-31 22:50:08'. If you add the offset, you're effectively doubling it. Unless that's what you want to do. Commented Feb 6, 2019 at 2:38
  • What @Chris said: the input time (if normal) already has the offset applied and is in that local time. Commented Feb 6, 2019 at 2:41

1 Answer 1

1
#!/usr/bin/env python2


def process_messed_up_timestamp(ts):
    """Convert messed up timestamps to something else.

    Input timestamp is in UTC format with the offset that
    should be applied but hasn't been.
    """
    from datetime import datetime, timedelta
    timestamp, plus, offset = ts[:19], ts[19], ts[20:]
    # should validate plus is '+' or '-'
    fmt = '%Y-%m-%d %H:%M:%S'
    base = datetime.strptime(timestamp, fmt)
    hours, minutes = [int(n) for n in offset.split(':')]
    delta = timedelta(hours=hours, minutes=minutes)
    multiplier = -1 if plus == '-' else 1
    return (base + multiplier * delta).strftime(fmt)


input_time = '2019-02-01 09:50:08+11:00'
input_time_converted = '2019-02-01 20:50:08'
assert process_messed_up_timestamp(input_time) == input_time_converted
Sign up to request clarification or add additional context in comments.

4 Comments

I had to modify timestamp, offset = ts[:19], ts[19:] to timestamp, offset = ts[:19], ts[20:] to make it work. The plus sign was messing it. I ll still mark it as solution, its your decision to improve it if you want. Thanks!
@takobaba Yeah, i messed that up. Use the plus or minus with the timedelta.
@takobaba fixed
Beauty! Now it is a full answer in case someone else needs it. THanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.