dbTalk Databases Forums  

How to read TIMESTAMP field in UTC?

comp.databases.mysql comp.databases.mysql


Discuss How to read TIMESTAMP field in UTC? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Nagle
 
Posts: n/a

Default How to read TIMESTAMP field in UTC? - 11-28-2010 , 01:45 PM






TIMESTAMP fields are stored in UTC, but a SELECT returns
them adjusted to the server's time zone. How can I get the
timestamp in UTC, without conversion?

UTC_TIMESTAMP() returns the current timestamp only;
it doesn't take an argument. So that's no help.

CONVERT_TZ(timeval,'SYSTEM','+0:00')

turns out to work. A more correct form is

CONVERT_TZ(timeval, @@time_zone, '+0:00')

which should compensate for any server-side time zone.

This is clunky, though. Is there some way to just
retrieve the timestamp in UTC?

John Nagle

Reply With Quote
  #2  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: How to read TIMESTAMP field in UTC? - 11-29-2010 , 04:36 AM






On Nov 28, 7:45*pm, John Nagle <na... (AT) animats (DOT) com> wrote:
Quote:
* * *TIMESTAMP fields are stored in UTC, but a SELECT returns
them adjusted to the server's time zone. *How can I get the
timestamp in UTC, without conversion?

* * *UTC_TIMESTAMP() returns the current timestamp only;
it doesn't take an argument. *So that's no help.

* * *CONVERT_TZ(timeval,'SYSTEM','+0:00')

turns out to work. *A more correct form is

* * *CONVERT_TZ(timeval, @@time_zone, '+0:00')

which should compensate for any server-side time zone.

* * *This is clunky, though. *Is there some way to just
retrieve the timestamp in UTC?

* * * * * * * * * * * * * * * * John Nagle
If you want to store a UTC time stamp, just create a datetime field
and put the value in it!

Reply With Quote
  #3  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: How to read TIMESTAMP field in UTC? - 11-29-2010 , 05:00 AM



John Nagle <nagle (AT) animats (DOT) com> wrote:
Quote:
TIMESTAMP fields are stored in UTC, but a SELECT returns
them adjusted to the server's time zone.
s/server/current connection/

Quote:
How can I get the timestamp in UTC, without conversion?
By asking to get it in UTC? I.e. like so:

Quote:
CONVERT_TZ(timeval, @@time_zone, '+0:00')

This is clunky, though. Is there some way to just
retrieve the timestamp in UTC?
What is the problem with the above? Maybe you rather want to set
the time zone for that connection to UTC?

mysqldump does similar in order to preserve timestamp values:

-- near the start of the dump file
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
-- now there is data, including timestamps in UTC
-- near the end of the dump file
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

The time_zone variable exists both locally (for the current connection)
and globally (initializing the per-connection value):

http://dev.mysql.com/doc/refman/5.1/...svar_time_zone


XL

Reply With Quote
  #4  
Old   
John Nagle
 
Posts: n/a

Default Re: How to read TIMESTAMP field in UTC? - 11-30-2010 , 12:38 AM



On 11/29/2010 3:00 AM, Axel Schwenke wrote:
Quote:
John Nagle<nagle (AT) animats (DOT) com> wrote:
TIMESTAMP fields are stored in UTC, but a SELECT returns
them adjusted to the server's time zone.

s/server/current connection/

How can I get the timestamp in UTC, without conversion?

By asking to get it in UTC? I.e. like so:

CONVERT_TZ(timeval, @@time_zone, '+0:00')

This is clunky, though. Is there some way to just
retrieve the timestamp in UTC?

What is the problem with the above? Maybe you rather want to set
the time zone for that connection to UTC?
For one hour each year in areas that use daylight
savings time, local time is ambiguous.

Quote:
-- near the start of the dump file
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
-- now there is data, including timestamps in UTC
-- near the end of the dump file
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
That's probably better.

It seems weird that there isn't some straightforward
way to retrieve TIMESTAMP values in UTC, regardless of
time zone settings.

John Nagle

Reply With Quote
  #5  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: How to read TIMESTAMP field in UTC? - 11-30-2010 , 01:34 AM



Hi John,

John Nagle <nagle (AT) animats (DOT) com> wrote:
Quote:
On 11/29/2010 3:00 AM, Axel Schwenke wrote:

CONVERT_TZ(timeval, @@time_zone, '+0:00')

This is clunky, though. Is there some way to just
retrieve the timestamp in UTC?

What is the problem with the above?

For one hour each year in areas that use daylight
savings time, local time is ambiguous.
The same ambiguity is there when the timestamp is stored, because
then it is converted from local time to UTC.

If this is a concern to you, then you must either set the time zone
to UTC. Or use UTC_TIMESTAMP() and store it in a DATETIME column.

Quote:
It seems weird that there isn't some straightforward
way to retrieve TIMESTAMP values in UTC, regardless of
time zone settings.
Setting the per-connection time_zone is not straightforward enough?


XL

Reply With Quote
  #6  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: How to read TIMESTAMP field in UTC? - 11-30-2010 , 02:03 AM



Axel Schwenke <axel.schwenke (AT) gmx (DOT) de> wrote:
Quote:
John Nagle <nagle (AT) animats (DOT) com> wrote:

It seems weird that there isn't some straightforward
way to retrieve TIMESTAMP values in UTC, regardless of
time zone settings.
There is this feature request:

http://bugs.mysql.com/bug.php?id=25010

and while playing with those functions I found and reported

http://bugs.mysql.com/bug.php?id=58583


XL

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.