I am using MySQL 5.7 and have a table that has the following columns that are being used to store some data that is source from an Apache common log formatted access log; details extracted from a MySQL schema export:
`timestamp` timestamp NULL DEFAULT NULL,
`offset` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL
`date` date GENERATED ALWAYS AS ((`timestamp` + interval replace(replace(replace(`offset`,'-0','-'),'+0','+'),'00','') hour)) VIRTUAL
`time` time GENERATED ALWAYS AS ((`timestamp` + interval replace(replace(replace(`offset`,'-0','-'),'+0','+'),'00','') hour)) VIRTUAL
`hour` int(2) GENERATED ALWAYS AS (hour((`timestamp` + interval replace(replace(replace(`offset`,'-0','-'),'+0','+'),'00','') hour))) VIRTUAL
As you can see I am storing timestamp
(0000-00-00 00:00:00
) and offset
(+00:00
) and then using generated columns to calculate, date
(0000-00-00
), time
(00:00:00
) and hour
(0
) values. And this is working well: I get to store the timestamp
as UTC and then—by storing the offset
value—I can dynamically get other info in non-UTC form.
But I don’t think the chained/nested REPLACE
items are too hot. The goal is to be able to take an ISO 8601 timezone offset value like -0400
or +1000
as part of an interval [some value] hour
calculation.
So is there a better way to approach this? I might be able to adjust the way that the timezone offset is stored initially so it is—essentially—exactly what I need for column calculations, but that seems messy and non-intuitive so I would rather use that +/-[for digit]
format if possible.