Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

By using HTTP request how to get server's timezone not in UTC format

My server's machine timezone is in HST format. When I try to get the timezone by using HTTP request in JavaScript, it is giving me in UTC format. Is there any way to get the server's timezone.

like image 521
Maharjun M Avatar asked Feb 17 '17 11:02

Maharjun M


1 Answers

A few things:

  • Never rely on the server's time zone to be set to anything in particular. It can easily be changed, or you may simply want to move your server somewhere else. Either should not affect your data.
  • The HTTP header will only give you the time in UTC/GMT. This part of the HTTP specification, in RFC7231 section 7.1.1.1 and 7.1.1.2.
  • The client knows nothing about the server's time zone, unless you specifically go out of your way to send it yourself. Due to the previous two points, this should not be required anyway, or should be used in very rare circumstances.
  • The server knows nothing about the client time zone either. If you want to display the value of the server's clock in the client's local time, you have two options:
  1. Send the UTC time to the client, and use JavaScript to convert from UTC to Local time. The JavaScript Date object is sufficient for this, but you may also find libraries like Moment.js and others useful.

  2. Determine the client's local time zone by some other means, either by asking the user, or by guessing. See this answer for more detail. Once you have the time zone ID (such as America/Los_Angeles, etc.) use this on the server-side. This approach is only useful if you have a lot of date/time manipulation to do on the server-side. If you are simply converting to local time for display, prefer option 1.

like image 139
Matt Johnson-Pint Avatar answered Sep 30 '22 01:09

Matt Johnson-Pint