-2

I have a linx based system where we cannot specify timezone of the time being set. So whoever sets time on this device sets their localtime. Now I have a requirement where I need to calculate the GMT Time based on the localtime that was existing on the device.

I guess I need to be taking the input of timezone the user is located in some variable. For Example timezone = EST-5:00EDT which means the local time is 5 hours behind the GMT Time.

Now do we have any functions in standard C Library which will calculate the GMT Time for me when I pass the offset.

Any help in this regard is highly appreciated. I searched through web and all I could find is only these functions in the below link: http://linux.die.net/man/3/localtime_r. I could not find any function which can calculate the time based on offset.

Is there any other approach to handle this.

Kranthi Kumar
  • 1,184
  • 3
  • 13
  • 26

1 Answers1

1

For any time, get the "local" time as a `time_t' (which will be a value in seconds since the epoch), add the GMT offset and you will have the timestamp in GMT.

At least if you don't care about such things like daylight-savings or possible leap seconds that happened during the last few hours.

Or make it a requirement that the installers or customers set the correct timezone so you could use the standard time functions. This is often the simplest solution, as developer cost in handling time-related bugs and complications is often much more expensive than a one-time setup of some new hardware.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • What you said is right. Unfortunately I need to be taking care of day light saving too which is the main reason why I am looking for standard functions which should be taking care of all possible cases. All that the device provides is just setting the time but not the zone. So the customer just sets the local time based on his watch. It is on my part to calculate the GMT Time. – Kranthi Kumar Aug 01 '16 at 10:37