Search code examples
pythonregexdatetimesubstitutionpython-dateutil

Substitution local date or datetime for ISO 8601 in string


I try to substitute local date or datetime (+02:00) for ISO 8601 "YYYY-MM-DDTHH:MI:SSZ" (UTC) in string in python 3.x

String example:

x = "This is first example with dates 2019-07-01 21:30:20 and 2019-07-02 21:30:20"

My code, which works fine but not perfect:

def date_to_iso(m):
    date_string = (dateutil.parser.parse(m.group(0))).astimezone(pytz.UTC).strftime("%Y-%m-%d" + "T" + "%H:%M:%S" + "Z")
    return iso_8601

y = re.sub(r"\d{4}(?:-\d{2}){2}" + r" \d{2}(?::\d{2}){2}", date_to_iso, x)

And result is good for first example:

Out[269]: 'This is first example with dates 2019-07-01T19:30:20Z and 2019-07-02T19:30:20Z'

My question is how to modify it for possibility to have dates in different formats. For example:

x = "This is second example with dates 2019-07-01 and 2019-07-02 21:30:20, but there are dates 2019-07-10 07:00 and 2019-07-10 09 too"

and it should return something like this:

Out[269]: 'This is second example with dates 2019-06-30T22:00:00Z and 2019-07-02T19:30:20Z, but there are dates 2019-07-10T05:00:00Z or 2019-07-10T07:00:00Z'

Solution

  • I found it out. This is code, which is good for me:

    import dateutil.parser
    import pytz
    import re
    
    x = "This is second example with dates 2019-07-01 and 2019-07-02 21:30:20, but there are dates 2019-07-10 07:00 and 2019-07-10 09 too"
    
    def datetime_to_iso(m):
        datetime_iso = (dateutil.parser.parse(m.group(0))).astimezone(pytz.UTC).strftime("%Y-%m-%d" + "T" + "%H:%M:%S" + "Z")
        return datetime_iso
    
    x = re.sub(r"\d{4}(?:-\d{1,2}){2}" + r"( \d{1,2}(?::\d{1,2}(?::\d{1,2})?)?)?", datetime_to_iso, x)
    
    
    print(x)
    This is second example with dates 2019-06-30T22:00:00Z and 2019-07-02T19:30:20Z, but there are dates 2019-07-10T05:00:00Z and 2019-07-10T07:00:00Z too