views:

38

answers:

2

I have unix timestamps from time zone X which is not known,

the current timestamp(now()) in TZ X is known 1275143019,

how to approach a javascript function so that it can generate the datetime in the users current TZ in the format 2010-05-29 15:32:35 ?

UPDATE

I'm not a unix timestamp expert,if unix timestamp is always the same in different TZ,

then I have to change the question a little,so that the current datetime in TZ X is known(like 2010-05-29 22:32:28),and the other datetime is also in this format,how to convert them to the user's TZ based on the difference between now() ?

UPDATE

Something strange from MySQL:

On server:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 18:34:30 | 
+---------------------+
1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1275143674 | 
+------------------+
1 row in set (0.00 sec)

On local:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2010-05-29 22:41:30 |
+---------------------+
1 row in set (0.00 sec)

mysql> select UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1275144091 |
+------------------+
1 row in set (0.00 sec)

Why the difference between now() (2010-05-29 22:41:30-2010-05-29 18:34:30=6hours) and UNIX_TIMESTAMP() (1275144091 - 1275143674 = 417seconds) is not the same ?

+1  A: 
Pointy
I need to take into account the difference of UTC of server/client time,even though it may be caused by clock or whatever.
Do you know how clocks work?
Pointy
Are you serious that matters?
Well, for example, `22:41:30 - 18:34:30' is **not** a "7 hour" difference. It's a difference of 4 hours and 7 minutes. If your machines were simply in different time zones, then the time difference would be an even number of hours and no extra minutes.
Pointy
Ok, I agree this may be a clock problem, but can be fixed if I can get current timestamp in javascript.
The current value comparable to UNIX_TIMESTAMP in Javascript is `var timestamp = new Date().getTime()` - that's like a "Unix" timestamp multiplied by 1000.
Pointy
Almost there ! The final step is how to convert the datetime to format of `2010-05-29 23:39:02` after `var d = new Date(1275146345*1000);` ?
+1 for the good info!
A: 

The difference between the select now(); commands is because of the timezone difference. The select UNIX_TIMESTAMP(); should be always the same on any two machines.

Why is then the 417 seconds differene? It is simple:

417 seconds ~= 7 minutes

22:41:30 - 18:34:30 = 7 minutes

Synchronize the clocks and the unix timestamps will be the same.

Zsolti
22:41:30 - 18:34:30 = 7 **hours** ,not **minutes**
Also,I've tried several different machines,their results of `UNIX_TIMESTAMP()` are all different,so I can't rely on this UTC timestamp.
If you can't rely on the timestamp, then none of this makes any sense at all. Fix your clocks. There's no API to tell you how wrong your clock is; it makes no sense.
Pointy
But as you have seens in my post,there is difference in UTC timestamp practically, do you want me to just ignore it ?
If the clocks on your servers are wrong, you have to fix them. Otherwise there's no way to know what the real UTC time is. That seems so obvious. All the clocks on my network return the same time, to within a couple milliseconds of each other, because my machines (like any well-managed machine) use NTP to keep synchronized.
Pointy
That's only a matter of start point, say the difference is fixed. The problem exists, no complain, but fix. For your "All the clocks",I'd guess they're in the same TZ,which of course don't need to worry about the TZ problem.
If the difference is fixed (still makes no sense, but whatever) then we're talking about a simple subtraction problem. Just add/subtract the offset as appropriate. However the fact remains that if your servers are returning different time values, then the clocks are wrong.
Pointy