Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Date Script, Somehow getting a different month?

Asked by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

I made this function earlier this month, and with the turning of the month, I have been experimenting with the function to see how it would handle, for some reason I am getting September of 2016 and I have no clue as to how it is getting that. I know this script is inefficient, but I saw no other way of doing it since some previously made scripts have outputted "0-11-14" or sometime in May. I am not huge on hard math, but I thought this way would be easy enough for me. Any help would be appreciated, thank you.

function GetDate()
local begin = 1412035200
local dayssince = math.floor(((math.floor(os.time()) - begin)+86400) / 86400) --The +86400 is for testing purposes, feel free to remove it.
print(dayssince)
local datetoday = ""
local monthsandyears = {["October 2014"] = 31; ["November 2014"] = 30; ["December 2014"] = 31;
["January 2015"] = 31; ["Febuary 2015"] = 28; ["March 2015"] = 31; ["April 2015"] = 30; ["May 2015"] = 31; ["June 2015"] = 30; ["July 2015"] = 31; ["August 2015"] = 31; ["Spetember 2015"] = 30; ["October 2015"] = 31; ["November 2015"] = 30; ["December 2015"] = 31;
["January 2016"] = 31; ["Febuary 2016"] = 29; ["March 2016"] = 31; ["April 2016"] = 30; ["May 2016"] = 31; ["June 2016"] = 30; ["July 2016"] = 31; ["August 2016"] = 31; ["Spetember 2016"] = 30; ["October 2016"] = 31; ["November 2016"] = 30; ["December 2016"] = 31;
["January 2017"] = 31; ["Febuary 2017"] = 28; ["March 2017"] = 31; ["April 2017"] = 30; ["May 2017"] = 31; ["June 2017"] = 30; ["July 2017"] = 31; ["August 2017"] = 31; ["Spetember 2017"] = 30; ["October 2017"] = 31; ["November 2017"] = 30; ["December 2017"] = 31;
["January 2018"] = 31; ["Febuary 2018"] = 28; ["March 2018"] = 31; ["April 2018"] = 30; ["May 2018"] = 31; ["June 2018"] = 30; ["July 2018"] = 31; ["August 2018"] = 31; ["Spetember 2018"] = 30; ["October 2018"] = 31; ["November 2018"] = 30; ["December 2018"] = 31;
["January 2019"] = 31; ["Febuary 2019"] = 28; ["March 2019"] = 31; ["April 2019"] = 30; ["May 2019"] = 31; ["June 2019"] = 30; ["July 2019"] = 31; ["August 2019"] = 31; ["Spetember 2019"] = 30; ["October 2019"] = 31; ["November 2019"] = 30; ["December 2019"] = 31;
["January 2020"] = 31; ["Febuary 2020"] = 29; ["March 2020"] = 31; ["April 2020"] = 30; ["May 2020"] = 31; ["June 2020"] = 30; ["July 2020"] = 31; ["August 2020"] = 31; ["Spetember 2020"] = 30; ["October 2020"] = 31; ["November 2020"] = 30; ["December 2020"] = 31;
}
local month = ""
for i,v in pairs(monthsandyears) do
    if v < dayssince then
        dayssince = dayssince - v
    else
        month = i
    end
end
if dayssince == 0 then dayssince = 1 end
datetoday = dayssince .. " " .. month
return datetoday
end

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

In general, dealing with time is annoying complicated.


pairs doesn't go over the indexes of a list in any particular order. That means this will more or less pick at random one of the months. (Probably it's doing something like alphabetical, which is not the chronological order)


Listing all of the lengths of each month isn't wise, because it isn't necessary.

Here's a rewrite that is more elegant (and will work for as far into the future as you would like, more or less -- forgiving leap seconds, time zones, and any other tiny adjustments)

function isLeapYear(year)
    if year % 400 == 0 then
        return true;
    end
    if year % 100 == 0 then
        return false;
    end
    return year % 4 == 0;
end

function GetDate()
    local begin = 946702800; -- Seconds since Jan 1, 1970 (GMT)
    -- until Jan 1, 2000 (EST)
    local now = os.time(); -- Seconds since Jan 1, 1970 (GMT)

    local seconds = now - begin;
    local days = seconds / 60 / 60 / 24;

    local t = days;

    local monthNames = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };

    local year = 2000;
    while true do
        local lens = {
            31,
            isLeapYear(year) and 29 or 28,
            31, 30, 31, 30, 31, 31, 30, 31, 30, 31
        };
        for month = 1, 12 do
            t = t - lens[month]
            if t <= 0 then
                return monthNames[month] .. " " .. year, math.floor(lens[month] - t);
            end
        end
        year = year + 1;
    end
end

Standard Lua could use os.date and os.time together, but ROBLOX does not appear to provide os.date.

0
Alright, thank you for fixing up the script, but I think I might experiment with os.time, I just looked up a Lua article and I think it might help me ( http://www.lua.org/pil/22.1.html ) M39a9am3R 3210 — 9y
0
EUREKA! I GOT A WORKING SCRIPT WOOHOO!!! IT IS NOT MESSING UP ON THE DATE! Contact me if you want a copy for others whom may be concerned by this topic. I really think Roblox should add os.date() to their Lua book though. M39a9am3R 3210 — 9y
Ad

Answer this question