简体   繁体   中英

Convert UTC timestamp into EST date and time using SQL

I have downloaded a bunch of tweets from the Twitter API. As I understand, the (JSON) created_at property is given in UTC (indicated by the +0000):

created_at: Fri Feb 18 21:08:38 +0000 2011

In my script, I stored these dates as a unix timestamp by converting them using PHP's strtotime:

strtotime(Fri Feb 18 21:08:38 +0000 2011) = 1298063318

I thus now have a table with tweets, with UTC unix timestamps:

id  nyse_date   nyse_time  twitter_timestamp
-------------------------------------------
1   2011-02-18  16:08:38   1298063318

The tweets I gathered all talk about stocks of the NYSE. The NYSE, (in New York, obviously), is located in EST, which is UTC - 5 hours (or: 18.000 seconds). So a tweet written on UTC Fri Feb 18 21:08:38 2011 is written on NYSE time on Fri Feb 18 16:08:38 2011. Hence, (see the table) a 1298063318 UTC timestamp is stored converted to EST (NYSE) date and time.

I now need to do this for all (millions of) tweets in the table. With what query can I automate this? I get heavily confused when I think about UTC timestamps of for instance

Fri Feb 18 02:08:38 +0000 2011

Here, nyse_date would be 2011-02-17 (not: 18) and nyse_time 21:08:38.

I'm hoping for some SQL goodness, because this is heavily confusing me (probably needlessly but it's my first time working with timestamps and timezones)

After reading in your data, update the twitter_timestamp field, like in

update mydata set twitter_timestamp = twitter_timestamp - 5 * 3600;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM