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.

takobaba
  • 306
  • 1
  • 4
  • 15
  • It's not a duplicate that answer applies to python3 this is python2. omg – takobaba Feb 06 '19 at 02: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? – Harvey Feb 06 '19 at 02:33
  • naaah, all of the inputs will be as same as the example. With UTC time plus/minus the offset. – takobaba Feb 06 '19 at 02: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. – Chris Clayton Feb 06 '19 at 02:38
  • What @Chris said: the input time (if normal) already has the offset applied and is in that local time. – Harvey Feb 06 '19 at 02:41
  • I m not sure about that Chris. My understanding is that the time is shown as UTC +11 so I add 11 to convert it to the format I m looking for, non-UTC, local timezone. – takobaba Feb 06 '19 at 02:41
  • UTC time is +00:00. The defacto standard for passing timestamps: https://en.wikipedia.org/wiki/ISO_8601 – Harvey Feb 06 '19 at 02:44
  • Ah, so your input is UTC, and you're converting to local time? My mistake – Chris Clayton Feb 06 '19 at 02:44
  • I am also confused now, thanks a lot for the inputs and answers. Let me go back a step and validate the input data. – takobaba Feb 06 '19 at 02:46

1 Answers1

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
Harvey
  • 5,703
  • 1
  • 32
  • 41
  • 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 Feb 06 '19 at 05:48
  • 1
    @takobaba Yeah, i messed that up. Use the plus or minus with the timedelta. – Harvey Feb 06 '19 at 12:12
  • 1
    @takobaba fixed – Harvey Feb 06 '19 at 16:45
  • Beauty! Now it is a full answer in case someone else needs it. THanks! – takobaba Feb 06 '19 at 23:34