Modul:Date: Perbedaan antara revisi

Loncat ke navigasi Loncat ke pencarian
1.209 bita dihapus ,  1 tahun yang lalu
←Membuat halaman berisi '-- Date functions for use by other modules. -- I18N and time zones are not supported. local MINUS = '−' -- Unicode U+2212 MINUS SIGN local floor = math.floor local Date, DateDiff, diffmt -- forward declarations local uniq = { 'unique identifier' } local function is_date(t) -- The system used to make a date read-only means there is no unique -- metatable that is conveniently accessible to check. return type(t) == 'table' and t._id == uniq end local func...'
dw>Johnuniq
(update from sandbox: implement show=M (minutes) and show=s (seconds); better method to fill a partial date)
(←Membuat halaman berisi '-- Date functions for use by other modules. -- I18N and time zones are not supported. local MINUS = '−' -- Unicode U+2212 MINUS SIGN local floor = math.floor local Date, DateDiff, diffmt -- forward declarations local uniq = { 'unique identifier' } local function is_date(t) -- The system used to make a date read-only means there is no unique -- metatable that is conveniently accessible to check. return type(t) == 'table' and t._id == uniq end local func...')
Baris 524: Baris 524:
local day_info = {
local day_info = {
-- 0=Sun to 6=Sat
-- 0=Sun to 6=Sat
[0] = { 'Sun', 'Sunday' },
[0] = { 'Min', 'Minggu' },
{ 'Mon', 'Monday' },
{ 'Sen', 'Senin' },
{ 'Tue', 'Tuesday' },
{ 'Sel', 'Selasa' },
{ 'Wed', 'Wednesday' },
{ 'Rab', 'Rabu' },
{ 'Thu', 'Thursday' },
{ 'Kam', 'Kamis' },
{ 'Fri', 'Friday' },
{ 'Jum', 'Jumat' },
{ 'Sat', 'Saturday' },
{ 'Sab', 'Sabtu' },
}
}


local month_info = {
local month_info = {
-- 1=Jan to 12=Dec
-- 1=Jan to 12=Dec
{ 'Jan', 'January' },
{ 'Jan', 'Januari' },
{ 'Feb', 'February' },
{ 'Feb', 'Februari' },
{ 'Mar', 'March' },
{ 'Mar', 'Maret' },
{ 'Apr', 'April' },
{ 'Apr', 'April' },
{ 'May', 'May' },
{ 'Mei', 'Mei' },
{ 'Jun', 'June' },
{ 'Jun', 'Juni' },
{ 'Jul', 'July' },
{ 'Jul', 'Juli' },
{ 'Aug', 'August' },
{ 'Agu', 'Agustus' },
{ 'Sep', 'September' },
{ 'Sep', 'September' },
{ 'Oct', 'October' },
{ 'Okt', 'Oktober' },
{ 'Nov', 'November' },
{ 'Nov', 'November' },
{ 'Dec', 'December' },
{ 'Des', 'Desember' },
}
}


Baris 557: Baris 557:
local function day_number(text)
local function day_number(text)
return name_to_number(text, {
return name_to_number(text, {
sun = 0, sunday = 0,
sun = 0, sunday = 0, min = 0, minggu = 0,
mon = 1, monday = 1,
mon = 1, monday = 1, sen = 1, senin = 1,
tue = 2, tuesday = 2,
tue = 2, tuesday = 2, sel = 2, selasa = 2,
wed = 3, wednesday = 3,
wed = 3, wednesday = 3, rab = 3, rabu = 3,
thu = 4, thursday = 4,
thu = 4, thursday = 4, kam = 4, kamis = 4,
fri = 5, friday = 5,
fri = 5, friday = 5, jum = 5, jumat = 5,
sat = 6, saturday = 6,
sat = 6, saturday = 6, sab = 6, sabtu = 6,
})
})
end
end
Baris 569: Baris 569:
local function month_number(text)
local function month_number(text)
return name_to_number(text, {
return name_to_number(text, {
jan = 1, january = 1,
jan = 1, january = 1, januari = 1,
feb = 2, february = 2,
feb = 2, february = 2, februari = 2,
mar = 3, march = 3,
mar = 3, march = 3, maret = 3,
apr = 4, april = 4,
apr = 4, april = 4,
may = 5,
may = 5, mei = 5,
jun = 6, june = 6,
jun = 6, june = 6, juni = 6,
jul = 7, july = 7,
jul = 7, july = 7, juli = 7,
aug = 8, august = 8,
aug = 8, august = 8, agu = 8, agt = 8, agustus = 8,
sep = 9, september = 9, sept = 9,
sep = 9, september = 9, sept = 9,
oct = 10, october = 10,
oct = 10, october = 10, okt = 10, oktober = 10,
nov = 11, november = 11,
nov = 11, november = 11,
dec = 12, december = 12,
dec = 12, december = 12, des = 12, desember = 12,
})
})
end
end
Baris 897: Baris 897:
end
end
return date, options
return date, options
end
local function autofill(date1, date2)
-- Fill any missing month or day in each date using the
-- corresponding component from the other date, if present,
-- or with 1 if both dates are missing the month or day.
-- This gives a good result for calculating the difference
-- between two partial dates when no range is wanted.
-- Return filled date1, date2 (two full dates).
local function filled(a, b)
-- Return date a filled, if necessary, with month and/or day from date b.
-- The filled day is truncated to fit the number of days in the month.
local fillmonth, fillday
if not a.month then
fillmonth = b.month or 1
end
if not a.day then
fillday = b.day or 1
end
if fillmonth or fillday then  -- need to create a new date
a = Date(a, {
month = fillmonth,
day = math.min(fillday or a.day, days_in_month(a.year, fillmonth or a.month, a.calendar))
})
end
return a
end
return filled(date1, date2), filled(date2, date1)
end
end


Baris 1.188: Baris 1.160:
options = {},
options = {},
list = _date_list,
list = _date_list,
subtract = function (self, rhs, options)
return DateDiff(self, rhs, options)
end,
text = _date_text,
text = _date_text,
}
}
Baris 1.401: Baris 1.370:
end
end
local H, M, S = diff.hours, diff.minutes, diff.seconds
local H, M, S = diff.hours, diff.minutes, diff.seconds
if code == 'dh' or code == 'dhm' or code == 'dhms' or code == 'h' or code == 'hm' or code == 'hms' or code == 'M' or code == 's' then
if code == 'dh' or code == 'dhm' or code == 'dhms' or code == 'h' or code == 'hm' or code == 'hms' then
local days = floor(diff.age_days + extra_days)
local days = floor(diff.age_days + extra_days)
local inc_hour
local inc_hour
Baris 1.416: Baris 1.385:
inc_hour = true
inc_hour = true
end
end
end
elseif code == 'M' then
if S >= 30 then
M = M + 1
end
end
else
else
Baris 1.446: Baris 1.411:
elseif code == 'hm' then
elseif code == 'hm' then
return hours, M
return hours, M
elseif code == 'M' or code == 's' then
M = hours * 60 + M
if code == 'M' then
return M
end
return M * 60 + S
end
end
return hours, M, S
return hours, M, S
Baris 1.579: Baris 1.538:
}
}


function DateDiff(date1, date2, options)  -- for forward declaration above
function DateDiff(date1, date2)  -- for forward declaration above
-- Return a table with the difference between two dates (date1 - date2).
-- Return a table with the difference between two dates (date1 - date2).
-- The difference is negative if date1 is older than date2.
-- The difference is negative if date1 is older than date2.
Baris 1.594: Baris 1.553:
if not (is_date(date1) and is_date(date2) and date1.calendar == date2.calendar) then
if not (is_date(date1) and is_date(date2) and date1.calendar == date2.calendar) then
return
return
end
local wantfill
if type(options) == 'table' then
wantfill = options.fill
end
end
local isnegative = false
local isnegative = false
Baris 1.616: Baris 1.571:
-------------A===B------------------------- A=2001-04-01  B=2001-04-30
-------------A===B------------------------- A=2001-04-01  B=2001-04-30
--------C=====================D------------ C=2001-01-01  D=2001-12-31
--------C=====================D------------ C=2001-01-01  D=2001-12-31
if wantfill then
local function zdiff(date1, date2)
date1, date2 = autofill(date1, date2)
local diff = date1 - date2
else
if diff.isnegative then
local function zdiff(date1, date2)
return date1 - date1  -- a valid diff in case we call its methods
local diff = date1 - date2
if diff.isnegative then
return date1 - date1  -- a valid diff in case we call its methods
end
return diff
end
end
local function getdate(date, which)
return diff
return date.partial and date.partial[which] or date
end
end
local function getdate(date, which)
local maxdiff = zdiff(getdate(date1, 'last'), getdate(date2, 'first'))
return date.partial and date.partial[which] or date
local mindiff = zdiff(getdate(date1, 'first'), getdate(date2, 'last'))
end
local years, months
local maxdiff = zdiff(getdate(date1, 'last'), getdate(date2, 'first'))
if maxdiff.years == mindiff.years then
local mindiff = zdiff(getdate(date1, 'first'), getdate(date2, 'last'))
years = maxdiff.years
local years, months
if maxdiff.months == mindiff.months then
if maxdiff.years == mindiff.years then
months = maxdiff.months
years = maxdiff.years
else
if maxdiff.months == mindiff.months then
months = { mindiff.months, maxdiff.months }
months = maxdiff.months
end
else
else
years = { mindiff.years, maxdiff.years }
months = { mindiff.months, maxdiff.months }
end
end
return setmetatable({
else
date1 = date1,
years = { mindiff.years, maxdiff.years }
date2 = date2,
partial = {
years = years,
months = months,
maxdiff = maxdiff,
mindiff = mindiff,
},
isnegative = isnegative,
iszero = iszero,
age = _diff_age,
duration = _diff_duration,
}, diffmt)
end
end
return setmetatable({
date1 = date1,
date2 = date2,
partial = {
years = years,
months = months,
maxdiff = maxdiff,
mindiff = mindiff,
},
isnegative = isnegative,
iszero = iszero,
age = _diff_age,
duration = _diff_duration,
}, diffmt)
end
end
local y1, m1 = date1.year, date1.month
local y1, m1 = date1.year, date1.month

Menu navigasi