I’m creating a custom timestamp, where the first time of a player in the game starts at second 0 and accumulates over the seconds.
So I want to turn this into a real date.
I tried this:
local ts = 0 -- seconds
print(DateTime.fromUnixTimestamp(ts):FormatLocalTime("DD/MM/YY HH:mm:ss", "pt-br"))
but it will print 31/12/69 21:00:00 when the correct should be 01/01/70 00:00.
This is a problem with my time zone.
How to fix this?
Thanks. Still, on the same topic, could you tell me how to correctly add YEARS to the timestamp?
Since the default result for Unix Timestamp is for the year 1970, I should add 51 years to get to 2021.
local ts = 60*60*24*365*51
print(DateTime.fromUnixTimestamp(ts):FormatUniversalTime("DD/MM/YY HH:mm", "pt-br"))
But will print 19/12/20 00:00, which certainly has to do with leap years.
local years = 52 --change to number of years
local daysInYears = 0
for i = 1, years do
if i % 4 == 0 then
daysInYears += 366
else
daysInYears += 365
end
end
local ts = 60*60*24*daysInYears
print(ts)
print(DateTime.fromUnixTimestamp(ts):FormatUniversalTime("DD/MM/YY HH:mm", "pt-br"))
This will check for leap years (adding around 12 days) although you’ll need to add the 11 months which have elapsed thus far in this year.
Here’s a more precise solution: if I want UnixTimeStamp as an offset starting on 01/01/2021, I just create this offset and then add my custom timestamp:
local MyTimeStamp = 0
local Offset = {
year = 2021,
month = 1,
day = 1,
hour = 0
}
local TimeStamp = os.time(Offset)
MyTimeStamp += TimeStamp