This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Left4Cake's profile picture

Date API (Get the date of your minecraft world)

Started by Left4Cake, 18 April 2013 - 02:20 AM
Left4Cake #1
Posted 18 April 2013 - 04:20 AM
I made a simple API that converts os.day() into a days of month, months, and years. (Starts on Day 1, Month 1, Year 0)




pastebin get u5V5bEc4 dateapi

Spoiler


function getDate()

local worldDays = os.day()
local month = 1
local day = worldDays
local year = 1
local leap = 28

-- get Year
i = 1
daysInYear = 365
while day > daysInYear do
if 1 < 4 then
daysInYear = 365
i = i + 1
leap = 28
else
daysInYear = 366
i = 1
leap = 29
end
year = year + 1
day = day - daysInYear
end

-- get Month
-- January
if day > 31 then
day = day - 31
month = month + 1
end
-- February
if day > leap then
day = day - leap
month = month + 1
end
-- March
if day > 31 then
day = day - 31
month = month + 1
end
-- April
if day > 30 then
day = day - 30
month = month + 1
end
-- May
if day > 31 then
day = day - 31
month = month + 1
end
-- June
if day > 30 then
day = day - 30
month = month + 1
end
-- July
if day > 31 then
day = day - 31
month = month + 1
end
-- August
if day > 31 then
day = day - 31
month = month + 1
end
-- September
if day > 30 then
day = day - 30
month = month + 1
end
-- October
if day > 31 then
day = day - 31
month = month + 1
end
-- November
if day > 30 then
day = day - 30
month = month + 1
end
-- December
if day > 31 then
day = day - 31
month = 1
year = year + 1
end

return day,month,year
end

This api only has one functions
day,month,year = dateapi.getDate()

Example Program as seen in screen shot above.
Spoiler

os.loadAPI("dateapi")
day,month,year = dateapi.getDate()
print(day .. "/" .. month .. "/" .. year)
Edited on 27 November 2013 - 11:32 AM
Zudo #2
Posted 18 April 2013 - 06:24 AM
Nice! Now I need an API for the real date and time :P/>
robotica34 #3
Posted 18 September 2013 - 01:48 PM
Nice! I like this. This could be useful for monitors when used with term.setTextScale()!
Lupus590 #4
Posted 15 December 2013 - 06:18 PM
nice idea and good(ish) code
for readability could you split the code into functions, to prevent the (new) functions being called outside the api, make the api local

local function calcLeap(year)
	--leap year calculation
end

attempting to call dateapi.calcLeap while using local will return attempt to call nil error

if possible get the day month bit in some functions as well, if done correctly your code will be smaller (in bytes) and easier to read
(i actually tried to write some of this for you but my brain didn't feel up to it, need to sleep, its 11:27pm)


suggestion for improving leap year calculation

if year is divisible by 400 then
	is_leap_year
else
	if year is divisible by 100 then
		not_leap_year
	else
		if year is divisible by 4 then
			is_leap_year
		else
			not_leap_year

source: http://en.wikipedia...._year#Algorithm

also you code can only count up to one year i.e. your program will work (properly) between 1/1/0 and 31/1/1
any higher dates will come out of the system with x/1/1 where x larger then 31
to fix this put the entire claculating bit of code in a do while loop wth an exit condition of (day <= 31)

i hope that you see this as constructive criticism

edit: <spelling/grammer corrections>
Edited on 16 December 2013 - 04:22 AM