0

There are many different ways to specify an interval in SQL. Specifically, in PostgreSQL flavour, see https://www.postgresql.org/docs/9.1/datatype-datetime.html.

Is there a way to validate an arbitrary interval string in Python? Is there a way to convert it into a timedelta? Can it be done without an explicit connection (and a call to the cursor)?

For some simple string like 31 minutes I could write a custom validator which splits on space and validates that we have an integer and valid units. But what about strings like 1 year 2 months 3 days 4 hours 5 minutes 6 seconds, P1Y2M3DT4H5M6S, P0001-02-03T04:05:06? I don't want to write my own validators for them all, so is there a better way?

GratefulGuest
  • 777
  • 7
  • 15
  • 1
    Have you tried https://stackoverflow.com/a/952600/10309040? – Mario Camilleri Oct 06 '20 at 06:49
  • Yes, and according to https://stackoverflow.com/a/8980982/7978198, "SQLAlchemy doesn't build this in as it defers to the DBAPI/database as the best and most efficient source of validation and coercion of values." So it appears doing it via the database is the correct way to do it generally. – GratefulGuest Oct 07 '20 at 01:52

0 Answers0