Converting an undefined GMT/UTC date to a user's local time

Hi there! I want to get the timezone of a person, not to display it, but to convert a GMT/UTC undefined date to the person’s local time. I do not have any way to access this way a date, only with a UNIX time. Is there any way to do this?

If you do not know what I exactly mean by this, let’s say I have a date that isn’t pre-defined. That “date” doesn’t actually show a human-readable date, it shows it in UNIX time and it’s in GMT/UTC timezone. However, how would I convert that time to the user’s local time?

1 Like

os.date lets you grab information based on both the users local timezone and UTC.

You could do os.date("*t") for local time, os.date("!*t") for UTC, and then get the difference between them. (Passing the tables they return as the argument to os.time returns a raw number of seconds, which allows for easier subtraction.)

From there, you know how far their local time is offset from UTC, so you can apply that same offset to your date.

3 Likes

This worked, except it returned it one hour behind. Does it have anything to do with daylight savings time? If so, how would you fix it?

That’s odd, but yeah, it’s probably something weird with daylight savings time. You could try manually checking if the os.date table has isdst as true or something? I’m not really sure why it’d act that way, though.

2 Likes

Alright, that worked. This is what I did (I believe it always works.)

if localTime.isdst then
    localTime.hour = localTime.hour + 1
end
1 Like