Module:Sandbox/DixonD/Datetime

local Error = require('Module:Error')
local this = {};

this.calendars = {};

function this.registerCalendar(moduleName, ...)
    moduleName = require(moduleName); --debug only: not lazily
    this.calendars[#(this.calendars)] = moduleName; --indexed by number, module loaded lazily
    for i = 1, arg.n do    
        this.calendars[arg[i]] = moduleName; -- indexed by code, module loaded lazily
    end;
end;

this.registerCalendar("Module:Sandbox/DixonD/Datetime/Gregorian", "Gregorian", "g"); --first position is the default
this.registerCalendar("Module:Sandbox/DixonD/Datetime/Julian",    "Julian",    "j");
-- more calendars may be added later in new modules pluggable here into this factory...

function this.selectCalendar(index)
    if index == nil then
        index = 0; -- use the default calendar (Gregorian as set above)
    end;

    local calendar = this.calendars[index];
    if calendar == nil then
        return index; -- fast path: assume that index is a calendar object (not indexed) already loaded
    end;

    if type(calendar) == "string" then -- calendar is a string (i.e. a module name)
        local moduleName = calendar;
        calendar = require(moduleName); -- effective loading of calendar object
        if calendar == nil then
            return tostring(Error.error{"Calendar module name ''" .. moduleName .. "'' is either missing or cannot be loaded!"});
        end;
        -- replace the moduleName given ad value of any calendar key by the newly loaded calendar object
        for k, v in pairs(this.calendars) do
            if v == moduleName then
                this.calendars[k] = calendar;
            end;
        end;
    end; -- else assume calendar is a valid calendar object

    return calendar;
end;

-- main functions
function this.julianDay(year, month, day, hour, minute, second, calendar)
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.julianDay(year, month, day, hour, minute, second);
end;

function this.dateOfJulianDay(julianDay, calendar)
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.dateOfJulianDay(julianDay);
end;

function this.yearOfJulianDay(julianDay, calendar)
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.yearOfJulianDay(julianDay);
end;
 
function this.monthOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.monthOfJulianDay(julianDay);
end;
 
function this.dayOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.dayOfJulianDay(julianDay);
end;
 
function this.weekdayOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.weekdayOfJulianDay(julianDay);
end;
 
function this.hourOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.hourOfJulianDay(julianDay);
end;
 
function this.minuteOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.minuteOfJulianDay(julianDay);
end;
 
function this.secondOfJulianDay(julianDay, calendar)   
    calendar = this.selectCalendar(calendar);
    if calendar == nil then
        return tostring(Error.error{"Calendar ''" .. calendar .. "'' is either missing or unknown!"});
    end;

    return calendar.secondOfJulianDay(julianDay);
end;

return this;

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.