-2

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'
Georgy
  • 12,464
  • 7
  • 65
  • 73
Gavko
  • 43
  • 1
  • 6
  • Idea is to find all date matches with for example regex with alternatives `(\d+ \w+, \d+)|(\w+ \d+, \d+)` and then go over each match one by one, parse it and replace it in place since you know your match boundaries – Gramotei Jul 17 '19 at 07:31

1 Answers1

0

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
Gavko
  • 43
  • 1
  • 6