Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Convert ISO-8601 Duration to Interval for DATE_ADD

Tags:

mysql

iso8601

I have two fields stored in a database, one of which contains a datetime and the other which contains a string representing a duration in ISO-8601 format (e.g. "P1MT2H"). I want to be able to add the duration to the datetime column in SQL and it seems like I should be able to do this using DATE_ADD but I don't know if there's a good way to convert it to an interval. I would prefer to not to have to define my own sql function that parses out the interval if possible.

like image 366
sgcharlie Avatar asked Feb 06 '17 23:02

sgcharlie


1 Answers

I would still love to hear some nice answers to this question but, for the time being, I've created a function to do this. I added a third optional parameter to run the calculation in a specific timezone. It's not a universal solution but I hope it puts someone else on the right track.

CREATE FUNCTION ADD_ISO_DURATION(StartDate DATETIME, Duration VARCHAR(45), Timezone VARCHAR(45))
RETURNS DATETIME
BEGIN
  DECLARE TimeStr VARCHAR(45) DEFAULT '';
  DECLARE Pos INTEGER;

  IF StartDate IS NULL OR Duration IS NULL OR LENGTH(Duration) = 0 THEN
    RETURN StartDate;
  END IF;

  IF Timezone IS NOT NULL THEN SET StartDate = CONVERT_TZ(StartDate, 'UTC', Timezone); END IF;

  IF Duration REGEXP '^P[0-9]+W$' THEN
    SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(Duration, 2, LENGTH(Duration) - 2) WEEK);
    RETURN IF(Timezone IS NULL, StartDate, CONVERT_TZ(StartDate, Timezone, 'UTC'));
  END IF;

  IF Duration NOT REGEXP '^P([0-9]+Y)?([0-9]+M)?([0-9]+D)?(T([0-9]+H)?([0-9]+M)?([0-9]+S)?)?$' THEN
    RETURN NULL;
  END IF;

  SET Pos = LOCATE('T', Duration);
  IF Pos <> 0 THEN
    SET TimeStr = SUBSTR(Duration, Pos + 1);
    SET Duration = SUBSTR(Duration, 2, Pos);
  ELSE
    SET Duration = SUBSTR(Duration, 2);
  END IF ;

  SET Pos = LOCATE('Y', Duration);
  IF Pos <> 0 THEN
    SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(Duration, 1, Pos - 1) YEAR);
    SET Duration = SUBSTR(Duration, Pos + 1);
  END IF;

  SET Pos = LOCATE('M', Duration);
  IF Pos <> 0 THEN
    SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(Duration, 1, Pos - 1) MONTH);
    SET Duration = SUBSTR(Duration, Pos + 1);
  END IF;

  SET Pos = LOCATE('D', Duration);
  IF Pos <> 0 THEN
    SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(Duration, 1, Pos - 1) DAY);
  END IF;

  IF LENGTH(TimeStr) <> 0 THEN
    SET Pos = LOCATE('H', TimeStr);
    IF Pos <> 0 THEN
      SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(TimeStr, 1, Pos - 1) HOUR);
      SET TimeStr = SUBSTR(TimeStr, Pos + 1);
    END IF;

    SET Pos = LOCATE('M', TimeStr);
    IF Pos <> 0 THEN
      SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(TimeStr, 1, Pos - 1) MINUTE);
      SET TimeStr = SUBSTR(TimeStr, Pos + 1);
    END IF;

    SET Pos = LOCATE('S', TimeStr);
    IF Pos <> 0 THEN
      SET StartDate = DATE_ADD(StartDate, INTERVAL SUBSTR(TimeStr, 1, Pos - 1) SECOND);
      SET TimeStr = SUBSTR(TimeStr, Pos + 1);
    END IF;
  END IF ;

  RETURN IF(Timezone IS NULL, StartDate, CONVERT_TZ(StartDate, Timezone, 'UTC'));
END $$
like image 99
sgcharlie Avatar answered Nov 15 '22 01:11

sgcharlie