Question

I have a string in RFC2822 format and I would like to insert the appropiate timestamp in UTC time.

Until now I've come up with

DATE_FORMAT(?, "%a, %d %b %Y %T -0700")

but the problem is the TZ offset -0700, there seems to be no format specifier for it.

How to properly insert such a timestamp into the database?

I would like to avoid processing on the application side.

Update

SELECT CONVERT_TZ(STR_TO_DATE(SUBSTRING('Mon, 15 Aug 2005 15:52:01 +0200',1,25), '%a, %d %b %Y %T'),CONCAT(SUBSTRING('Mon, 15 Aug 2005 15:52:01 +0200',27,3), ':', SUBSTRING('Mon, 15 Aug 2005 15:52:01 +0200',30,2)),'+00:00') AS conv;

How to do it "cleanly" by using a variable instead of repeating the string Mon, 15 Aug 2005 15:52:01 +0200?

Was it helpful?

Solution

SET @a:='Mon, 15 Aug 2005 15:52:01 +0200';
SELECT
  CONVERT_TZ(
    STR_TO_DATE(SUBSTRING(@a,1,25), '%a, %d %b %Y %T'),
    CONCAT(SUBSTRING(@a,27,3), ':', SUBSTRING(@a,30,2)),
    '+00:00'
  )
AS conv;

That's what I call an answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top