Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL ISO 8601 Date with Timezone offset instead of Z value

I need to SELECT a ISO 8601 Date.

DATE_FORMAT(date, '%Y-%m-%dT%TZ') 

This produces something like

2013-11-13T15:47:530Z

But I need it with the offset instead of Z value:

2013-11-13T15:47:53+02:00

How can I do this with plain MySQL ?

like image 981
Daniel W. Avatar asked Oct 20 '25 03:10

Daniel W.


2 Answers

You need to store the timezone as an extra column in DB. I do not know any DB that stores datetime with timezone/offset.

Or store the date as string in ISO 8601 format with offset..

Edit: I stand somewhat corrected, with some newer databases it is possible!

  • Postgresql Date/Time Types, which indeed may include timezone.
    • Datatype: "timestamp [ (p) ] with time zone" only for input and output, it is stored in UTC.
    • Datatype: "time with time zone", only time, stores timezone offset. Since version 8.
  • ORACLE Datetime Datatypes and Time Zone Support.
    • Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset. Since version 9i.
    • Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset or timezone name. Since version 10g.
    • See also Oracle TIMESTAMP WITH TIMEZONE named zone vs offset.
  • MicroSoft SQL Server 2008
    • Datatype: "datetimeoffset", stores timezone offset.
  • Sybase
    • Datatype: "TIMESTAMP WITH TIME ZONE", stores timezone offset.
    • Support for TIMESTAMP WITH TIME ZONE is optional SQL language feature F411 of the SQL/2008 standard.
  • Seems to actually be somewhat standard SQL99.

  • Not for Mysql. Version 5.6.

  • Nor for SQLite. Version 3.

I do find consolation in the fact that the correction came from myself ;-)

like image 138
Trygve Sørdal Avatar answered Oct 21 '25 19:10

Trygve Sørdal


his snippet should work for iso 8601 without milis, zulu time is also not supported:

select IF(
LENGTH(value) < 23,
STR_TO_DATE(value,'%Y-%m-%dT%TZ'),
CASE SUBSTRING(value from 20 for 1)
WHEN '+' THEN
DATE_ADD(
STR_TO_DATE(SUBSTRING(value from 1 for 19),'%Y-%m-%dT%TZ'),
INTERVAL SUBSTRING(value from 21 for 2) HOUR)
WHEN '-' THEN
DATE_SUB(
STR_TO_DATE(SUBSTRING(value from 1 for 19),'%Y-%m-%dT%TZ'),
INTERVAL SUBSTRING(value from 21 for 2) HOUR
)
END
)
from THE_NAMESPACE.THE_TABLE.THE_COLUMN as value;
like image 40
Bernd Hopp Avatar answered Oct 21 '25 19:10

Bernd Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!