How To Make A Specific Timezone Clock
Hello there!
In this post, you’ll be learning how to make a simple clock that is based on a specific timezone regardless of a client’s timezone.
For this tutorial, we will be trying to get the timezone of Philippines (but you can use any other location that you would like).
Coding The Clock
To make this clock, we can use os.date(string: formatString, int time) which returns a dictionary that has all the information we are going to need based on a specified time.
Normally, it returns time based on the current local timezone. But we can tell it to give us UTC time ("!*t") instead of local time ("*t").
local date = os.date("!*t", os.time())
“But wait, doesn’t this only return UTC time?”
Yes it does! Which is why we need to add an offset based on a timezone you want. To do this, you’re going to want to search up the name of the country, state, and etc. and find out it’s UTC time.
Something like this should pop up.
As you can see for the example, Philippines is 8 hours ahead of UTC. And each hour has 3600 seconds. And since os.time() returns the amount of seconds since the Unix epoch under UTC time, we just multiply 3600 by 8 and add the offset to it!
local date = os.date("!*t", os.time() + (3600 * 8))
Nice! Now all that’s left to do is to use this and make it readable.
Since the returned hour is military time, we are going to use a simple formula to convert it to standard time. If the given hour is bigger than 12, subtract 12 from that number and voila, you have your standard hour. Then from that, we can just input whether we use AM or PM.
hour - An integer between 1 and 24 describing the current hour of the day.
We are also going to have to format the returned minutes because it returns the exact integer of the minute (i.e. if we just went with just adding the minute and the integer was a single digit, the result would be 10:5 AM instead of 10:05 AM) So what we do is we check how much digits is the returned minutes. If it is less than 2, add 0 in front of the minute, otherwise, do no changes.
min - An integer between 0 and 59 describing the current minute of the hour.
local function MilitaryToStandard(hour)
return if hour > 12 then hour - 12 else hour, if hour > 12 then true else false
end
local function ConvertTo2Digit(digit)
return if #tostring(digit) < 2 then 0 .. digit else digit
end
local date = os.date("!*t", os.time() + (3600 * 8))
local min = ConvertTo2Digit(date.min)
local hour, isPM = MilitaryToStandard(date.hour)
Then last but not the least, just concatenate all of this information into one string. Your code should look like this now!
local function MilitaryToStandard(hour)
return if hour > 12 then hour - 12 else hour, if hour > 12 then true else false
end
local function ConvertTo2Digit(digit)
return if #tostring(digit) < 2 then 0 .. digit else digit
end
local date = os.date("!*t", os.time() + (3600 * 8))
local min = ConvertTo2Digit(date.min)
local hour, isPM = MilitaryToStandard(date.hour)
local result = hour .. ":" .. min .. if isPM then " PM" else " AM"
Conclusion
And there you have it! Congratulations, you just made your own clock!
If you are facing any problems, please ask me about it!