Troubles with OS.Date() Specifier

Hello, and thank you for taking the time to read this. I am having some difficulty figuring out the specifier for a timezone abbreviation in os.date.

I’ve attempted using the specifier "*%Z" however, it doesn’t seem to give me the abbreviation, nor does the os API reference specify clearly to my understanding.

I’m sure it’s an easy fix. Thanks!

The os.date specifier isn’t the exact same as lua, it can be either "*t" or "!*t", these can both be put into os.date()

local Date = os.date(os.time(), "*t")

or

local Date = os.date(os.time(), "!*t")

The only difference between these two is that the top one will return the local time (the time of the player’s computer), the second one will return the UTC Time (Universal Coordinated Time)

Unless you are doing something completely different them I’m not sure

Thanks for replying! I was actually asking for the specifier on the specifier for a timezone abbreviation, I appreciate your time

Ohh! Make sense, no problem! Hope you find something.

Hi!

I see what you mean. You wanted to use * as an operator, because it’s written on Roblox Dev Hub. * in this case is not meant to be used as an string patter class modifier, but only marks further explanation, which is:

*This value can vary depending on the current locale.

Notice the * character? The return value of %Z might be an acronym or not, depending on the zone.

What to do now?

You’ll have to abbreviate manually. There are different ways to do this and I tested a couple, but found the following as reasonably short and one of the most performant according to some short tests I did.

local timeZone = os.date("%Z"); -- Default time stamp is os.time()

timeZone = timeZone:gsub("%l+%s*", "");
print(timeZone);

-- %l --> lower case class
-- %s --> space or white space character class
-- + --> matches 1 or more of the preceeding character class
-- * --> matches 0 or more of the proceeding character class

See how string.gsub() works by following this link: string | Roblox Creator Documentation.

First, we remove all lower case characters and all white spaces.

The following text is of a more informative nature (if you are interested in micro-optimization).

The worst alternative is gradual string concatenation. Splitting string into a table, looping through that table and searching for upper cases while gradually concatenating strings performed significantly worse. Execution time was an order of magnitude lower, while memory usage was the worst too, because of the way lua combines strings (gradual concatenation hashes new strings in memory).

Looping through all characters, inserting them in a new table, and concatenating that table performed pretty well too, but the code was longer.

More about string patterns:

Different display?

If you wanted to have abbreviations written differently, for example GTM instead of UTC, or have GMT +1 written instead of CEST, it would open up some further ways to find the time zone.

Thank you! I appreciate it. <3