Módulo:Date/ejemplos
Este módulo no tiene página de documentación[crear]
-- Ejemplos de uso del [[Módulo:Date]]. Ver página de discusión para los resultados.
local Date = require('Módulo:Date')._Date
local show -- para mostrar resultados, definida abajo
-- A date can be constructed using various input formats.
local function make_a_date()
show('Crear una fecha')
local same_dates = {
Date(2016, 3, 9),
Date('2016-03-09'),
Date('2016-3-9'),
Date('9 Marzo 2016'),
Date('09 mar 2016'),
Date('MAR 09 2016'),
Date('Marzo 9, 2016'),
Date('Marzo 9, 2016 CE'),
Date('Marzo 9, 2016 A.D.'),
Date('3:45 pm 9 de Marzo de 2016'),
Date('3:45 p.m. 9 de Marzo de 2016'),
Date(2016, 3, 9, 15, 45),
Date('9 Marzo 2016 15:45'),
}
for _, date in ipairs(same_dates) do
show(nil, date:text() .. ' o ' .. date:text('mdy'))
end
local more_dates = {
Date('4 Octubre 1582', 'Julian'),
Date('juliandate', 2299160),
Date('15 Octubre 1582'),
}
for _, date in ipairs(more_dates) do
show(nil, date.dayname .. ' ' .. date:text() .. ' en el calendario ' ..
date.calendar .. ' es el día ' .. date.jd .. ' en el Juliano')
end
end
-- A date can be displayed using various output formats.
local function show_a_date()
show('Mostrar una fecha')
local dates = {
Date(2016, 3, 9),
Date('9 Marzo 2016 BC'),
}
local format_option = {
{ 'ymd' },
{ 'mdy' },
{ 'dmy' },
{ 'dmy', 'era=B.C.E.' },
{ '%A %B %-d, %Y %{era}' },
{ '%A %B %-d, %Y %{era}', 'era=A.D.' },
{ 'un %{dayname} en %{monthname} de %Y %{era}' },
}
for _, t in ipairs(format_option) do
local format = t[1]
local option = t[2]
for _, date in ipairs(dates) do
show(nil, date:text(format, option))
end
end
end
-- When an input date is parsed, its format is stored.
local function keep_format()
show('Mantener el formato de entrada de la fecha')
local somedate = Date('Marzo 9, 2016')
local dates = {
Date(2016, 3, 9),
Date('2016-3-9'),
Date('9 Mar 2016'),
Date('Marzo 9, 2016'),
Date(somedate, {day = 1}), -- somedate with day changed
somedate + 23, -- 23 days after somedate
somedate - '3 months', -- 3 months before somedate
Date('3:45 p.m. Marzo 9, 2016'),
}
show(nil, 'El formato base era ' .. somedate.format)
for _, date in ipairs(dates) do
show(nil, date:text() .. ' o con el formato base ' .. date:text(date.format))
end
end
-- Using Date to get the current date, or current date and time.
local function current_date()
local now_date = Date('currentdate')
local now_datetime = Date('currentdatetime')
show('Muesrta de uso de currentdate para ver cuando se modificó la página',
now_date:text(), -- 7 March 2016 (for example)
now_date:text('mdy'), -- March 7, 2016
now_date:text('ymd'), -- 2016-03-07
now_date:text('%A %-d %B %-Y'), -- Monday 7 March 2016
now_datetime:text(), -- 21:32:45 7 March 2016
now_datetime:text('hms'), -- 21:32:45
now_datetime:text('%c') -- 9:32 pm 7 March 2016
)
end
-- Using current to provide default values.
local function current_as_default(year, month, day, hour, minute, second)
local current = require('Módulo:Date')._current
year = year or current.year
month = month or current.month
day = day or current.day
hour = hour or current.hour
minute = minute or current.minute
second = second or current.second
show('Uso de la fecha y hora actuales por defecto',
year, month, day, hour, minute, second)
-- Alternatively, a date can be constructed with specified items overridden.
show(nil, Date('currentdatetime', {
year = year,
month = month,
day = day,
hour = hour,
minute = minute,
second = second }):text('%c'))
end
-- Make a date from the day number in a year.
local function date_from_day_of_year()
-- Example: day 123 in 2015 and in 2016.
show('Crear una fecha partiendo del número de día del año')
local offset = 123 - 1 -- 1 January has day-of-year = 1
for _, year in ipairs({ 2015, 2016 }) do
local date = Date(year, 1, 1) + offset
show(nil, 'El día ' .. date.dayofyear .. ' del año ' .. year .. ' es el ' .. date:text())
end
end
-- Number of days in a month for Gregorian (default) and Julian calendars.
local function days_in_month(year, month, calendar_name)
local title = 'Cantidad de días del mes'
if calendar_name then
title = title .. ' (calendario ' .. calendar_name .. ')'
end
local monthdays = require('Módulo:Date')._days_in_month
show(title, monthdays(year, month, calendar_name))
-- Alternative method, using Date.
local date = Date(year, month, 1, calendar_name)
show(nil, date.monthname .. ' de ' .. date.year .. ' tuvo ' .. date.monthdays .. ' días')
show(nil, date:text('%{monthname} de %{year} tuvo %{monthdays} días')) -- same
end
-- Julian day number and date arithmetic.
local function julian_date(year, month, count, calendar_name)
if calendar_name then
show('Fecha en el Juliano ( desde el ' .. calendar_name .. ')')
else
show('Fecha en el Juliano (desde Gregoriano)') -- Gregorian calendar by default
local date = Date('24 de noviembre de 4714 BCE')
show(nil, 'El número de día Juliano fué ' .. date.jd .. ' en el ' .. date:text('dmy'))
end
local first_of_month = Date(year, month, 1, calendar_name)
for _ = 1, count do
first_of_month = first_of_month + '1m' -- next month
local date = first_of_month - '1d' -- last day of month
show(nil,
'El último día del mes (' .. date:text() .. ' en el calendario ' .. date.calendar ..
') fué el día número ' .. date.jd .. ' del calendario Juliano'
)
end
end
-- Number of days a date was in the past, or will be in the future.
local function how_long(date_text)
local now_date = Date('currentdate')
local then_date = Date(date_text)
if not then_date then
show('Cuánto ha pasado desde', 'Fechá no válida: ' .. (date_text or ''))
return
end
local fmt = '%A %-d de %B de %-Y %{era} (día del año = %{dayofyear}, día en total = %{gsd}) '
local info = then_date:text(fmt)
if then_date == now_date then
fmt = 'es ahora (%d día%s)'
elseif then_date > now_date then
fmt = 'será en %d día%s'
else
fmt = 'fué hace %d día%s'
end
local diff = then_date - now_date
local days = diff.age_days
local s = days == 1 and '' or 's'
show('Cuánto ha pasado desde', info .. string.format(fmt, days, s))
local y, m, d = diff:age('ymd') -- age in years, months, days
show(nil, string.format('(%d años + %d meses + %d días)', y, m, d))
end
-- First and third Fridays in each month of the given year.
local function fridays(year)
show('Primero y tercer viernes en cada més del año ' .. year)
for month = 1, 12 do
local dates = Date(year, month, 1):list('Viernes >=')
show(nil, dates[1] .. ', ' .. dates[3])
end
end
-- Next Friday after or before a particular date.
local function next_friday()
show('Siguiente y anterior viernes a una fecha en particular')
local dates = {
Date('1 Ene 2016'),
Date('2 Ene 2016'),
Date('3 Mar 1980'),
}
for _, date in ipairs(dates) do
-- 1 = number of Fridays that are wanted in the list
local after = date:list('1 Viernes')[1]
local before = date:list('1 Viernes <')[1]
local format = '%A %-d %B %-Y'
show(nil,
'Para el ' .. date:text(format) ..
', el próximo es el ' .. after:text(format) ..
', y el último fué el ' .. before:text(format)
)
end
end
-- Results are held in the lines table.
local lines
function show(title, ...) -- for forward declaration above
if title then
if lines[1] then
table.insert(lines, '')
end
table.insert(lines, "'''" .. title .. "'''")
end
for _, text in ipairs({...}) do
table.insert(lines, ':' .. tostring(text))
end
end
local function main()
lines = {}
make_a_date()
show_a_date()
keep_format()
current_date()
current_as_default(nil, nil, nil, 14, 30, 0) -- 2:30 pm today
date_from_day_of_year()
days_in_month(1900, 2)
days_in_month(1900, 2, 'Julian')
julian_date(1899, 11, 4)
julian_date(-120, 11, 4, 'Julian')
how_long('29 de feb de 2100') -- an invalid date
how_long('currentdate')
how_long('29 de feb de 2400')
how_long('29 de feb de 2401 BCE')
fridays(2016)
next_friday()
return table.concat(lines, '\n')
end
return { main = main }