Module talk:Age
Module:Age
I created Module:Age to fix some problems reported with {{Age in years and months}}, and that template has been replaced with code to invoke this module. I don't know if it would be worth replacing other related templates with a call to a module, but as an experiment I have added code to this module to implement {{Age in days}} and {{Gregorian serial date}}. I have done over 100 tests offline which suggest that the module is performing correctly. The following shows a few results that compare the outputs from the current templates with User:Johnuniq/age days and User:Johnuniq/gsd which invoke the module.
Parameters to template | Result from {{age in days}} |
Result from {{User:Johnuniq/age days}}
|
---|---|---|
|1898|1|1|1900|1|1 |
730 | 730 |
|1900|1|1|1898|1|1 |
−730 | −730 |
|2013|4|1 |
4538 | 4538 |
Parameters to template | Result from {{gsd}} |
Result from {{User:Johnuniq/gsd}}
|
---|---|---|
|year=1|month=1|day=1 |
1 | 1 |
|year=2000|month=12|day=31 |
730485 | 730485 |
(none) |
739497 | 739497 |
I'll mention this in a couple of places to let people know the module is available. Johnuniq (talk) 09:55, 2 April 2013 (UTC)
- Seems to be working well! Will it also work in e.g. {{Birth date and age}}? It Is Me Here t / c 14:55, 11 June 2013 (UTC)
- I'm super busy and can't take on any extra thinking for a while, however the short answer is that the module could fairly easily be extended to handle what I think the birth date and age template is doing, but it would need a bit of work and testing. Is there a known problem with the template? If so, that would focus my attention and make it more likely that people would want to accept a new solution to the very widely used dob template. It's a bit lucky that I noticed this. If you post here and I don't reply in a day or two, please ping my talk. Johnuniq (talk) 03:42, 12 June 2013 (UTC)
- No problems as such that I know of; it's just that I thought that templates using Modules rather than template code loaded faster (esp. on mobile devices)? There's no rush; I just thought that if the bulk of the work has already been done, and that it would be a relatively easy matter to employ this code in other areas, it would seem a shame not to if it improves reader experience. It Is Me Here t / c 10:54, 13 June 2013 (UTC)
- I'm super busy and can't take on any extra thinking for a while, however the short answer is that the module could fairly easily be extended to handle what I think the birth date and age template is doing, but it would need a bit of work and testing. Is there a known problem with the template? If so, that would focus my attention and make it more likely that people would want to accept a new solution to the very widely used dob template. It's a bit lucky that I noticed this. If you post here and I don't reply in a day or two, please ping my talk. Johnuniq (talk) 03:42, 12 June 2013 (UTC)
Recent change
I just reverted the change to the module as it broke the module, adding over a hundred articles to Category:Pages with script errors. One change that broke things was the removal of age_ym but there was possibly others, I did not even try checking all the articles with errors but looked for the recent change that caused it.--JohnBlackburnewordsdeeds 12:24, 24 June 2016 (UTC)
- @JohnBlackburne: Thanks! Unfortunately there was a lot of off-wiki chaos here yesterday and I completely forgot to update {{age in years and months}} for the way the new module works. I did check a few things before I was called away, but the pages hadn't updated so I didn't see any problems. I have restored the new version and updated the template, and will be monitoring the situation. Johnuniq (talk) 01:01, 25 June 2016 (UTC)
Allow julianday month offsets to permit use in JULIANDAY
{{Edit template-protected}} closed by author
{{JULIANDAY}} is the only template listed in the docs that doesn't use this module, and I believe it's because of edge cases involving leap years and too-large day-of-months in general, which the Age module rejects but the JULIANDAY template accepts. "1900-02-29" doesn't make sense in most other contexts, but it has a well-defined Julian day and is expected to work in the current {{JULIANDAY}} template (see testcases).
The edit I'm requesting fixes this compatibility issue by using an offset, and if it is accepted we should be able to finally replace {{JULIANDAY}} with this module so that all implemented template functionalities in the docs are actually used in their corresponding templates.
This edit only affects the dateToJd
function, so it will not affect any non-Julian-day usage of the module. It's fully implemented in the module sandbox and it's been tested with all the {{JULIANDAY}} testcases. Here's the revised function:
local function dateToJd(frame) -- Return Julian date (a number) from a date which may include a time, -- or the current date ('currentdate') or current date and time ('currentdatetime'). -- The word 'Julian' is accepted for the Julian calendar. local Date = getExports(frame) local args = frame:getParent().args local date if args[1] and args[2] then date = Date(args[1], args[2], 1, args[4], args[5], args[6], args[7]) elseif args[1] then date = Date(args[1], 1, 1, args[4], args[5], args[6], args[7]) else date = Date(args[1], args[2], args[3], args[4], args[5], args[6], args[7]) end local offset = 0 if args[3] then offset = tonumber(args[3]) - 1 end if date then return tostring(date.jd + offset) end return message('Need valid year/month/day or "currentdate"') end
I can of course make any other changes if they help. Thanks, Habst (talk) 05:12, 17 June 2018 (UTC)
- Please let's take some time to discuss this before moving to change the module. The only reason I haven't yet updated {{JULIANDAY}} to use the module is that I got distracted by other things. I updated the templates very slowly because the tracking categories can take a long time to catch up and show any problems, and I'm conservative about changing code. It also took quite a lot of work to fix old errors (invalid dates) in articles that the module reported.
Thorough tests show that the module gives correct Julian days for all dates from 9999 BCE to 9999 CE (I wrote a test harness to check the first and last days of the month for all years in that range). The current template uses arithmetic that fails for dates well inside that range. For example,
{{JULIANDAY|-4800|3|1}}
(using the old template) gives -32410 whereas the module gives -32044. A reasonable argument could be made that it is silly to expect a Julian day for such an extreme date, but that's how it is. I will need 24 hours to think about the above. Meanwhile, the major advantage of the module IMHO is that it displays an error if an invalid date is used. Why would anyone want to know the Julian day of 1900-02-29 or 2018-04-31 (both of which are invalid dates)? Johnuniq (talk) 05:49, 17 June 2018 (UTC) - By the way, the module supports
fix=on
which allow invalid dates to be entered. The date is adjusted by overflowing the day/month values. For example, using {{extract}}:{{JULIANDAY|1900|02|28}}
→ 2415079{{extract|1900-02-28|show=juliandate}}
→ 2415079{{extract|1900-02-29|fix=on|show=juliandate}}
→ 2415080
- If
fix=on
were omitted, the last of the above would show an error. If there was a reason for needing it, that parameter could be made available in {{JULIANDAY}}. Johnuniq (talk) 05:57, 17 June 2018 (UTC)- Hi Johnuniq, thanks for your points here and I agree we should discuss it. I have no problem using the current
dateToJd
function in {{JULIANDAY}} if that is acceptable -- the only reason why I modified the function to accept invalid dates were because invalid dates were being used in the test cases (which I didn't write) so I wanted to make them pass. - I agree that the Lua module is more accurate for larger magnitude dates, and I think that's a good reason to begin to use the module in the template (surely nobody would have depended on that incorrect behavior with the template arithmetic). Maybe using
fix=on
is better than my offset change -- if it is, then we should use that instead. Or if nobody cares about knowing the date of 1900-02-29, then maybe we shouldn't use it at all. What do you think? --Habst (talk) 19:57, 17 June 2018 (UTC)- I would far prefer to have the template require valid dates and fix problems found where invalid dates are used. That is much better in the long run than accepting junk. I revert a dozen vandal attacks on articles each week when the bad edit damages a date in a template monitored by this module's error tracking category. Do you know of any reason to accept invalid or partial dates for JULIANDAY other than some test examples? I'll look at some examples of JULIANDAY in articles later. Johnuniq (talk) 23:31, 17 June 2018 (UTC)
- I don't know of any examples; I only did it because of the testcases. If it's not used in practice and not documented, then there would be no harm in making the switch to the module either way. Do you think I should cancel this ER and make one at the {{JULIANDAY}} template to just use the unmodified module then? --Habst (talk) 23:43, 17 June 2018 (UTC)
- I'll update JULIANDAY soon but I need to check my notes on the usage of that template first to gauge how many problems might occur. Johnuniq (talk) 01:21, 18 June 2018 (UTC)
- I updated {{JULIANDAY}} to use Module:Age. It turns out the template was used in only half a dozen articles, but is used in about 20 templates which are used in around 12,000 articles. It is too hard to determine whether those templates can generate an invalid date, but a few cases that I checked only had valid dates. I will monitor Category:Age error to see if errors are reported. Johnuniq (talk) 03:56, 18 June 2018 (UTC)
- OK, thank you! I'm marking this as resolved now. --Habst (talk) 05:20, 18 June 2018 (UTC)
- I had to revert my edit so JULIANDAY is again using the old template code. Using the module there were hundreds of articles in Category:ParserFunction errors due to a handful of templates that I will investigate later: {{update after}} + {{show by date}} + {{PD-Australia}} and possibly others. That will be due to errors reported by the module with invalid dates but I'll check what's going on and do something in a day or two. Johnuniq (talk) 06:09, 18 June 2018 (UTC)
- OK, thank you! I'm marking this as resolved now. --Habst (talk) 05:20, 18 June 2018 (UTC)
- I updated {{JULIANDAY}} to use Module:Age. It turns out the template was used in only half a dozen articles, but is used in about 20 templates which are used in around 12,000 articles. It is too hard to determine whether those templates can generate an invalid date, but a few cases that I checked only had valid dates. I will monitor Category:Age error to see if errors are reported. Johnuniq (talk) 03:56, 18 June 2018 (UTC)
- I'll update JULIANDAY soon but I need to check my notes on the usage of that template first to gauge how many problems might occur. Johnuniq (talk) 01:21, 18 June 2018 (UTC)
- I don't know of any examples; I only did it because of the testcases. If it's not used in practice and not documented, then there would be no harm in making the switch to the module either way. Do you think I should cancel this ER and make one at the {{JULIANDAY}} template to just use the unmodified module then? --Habst (talk) 23:43, 17 June 2018 (UTC)
- I would far prefer to have the template require valid dates and fix problems found where invalid dates are used. That is much better in the long run than accepting junk. I revert a dozen vandal attacks on articles each week when the bad edit damages a date in a template monitored by this module's error tracking category. Do you know of any reason to accept invalid or partial dates for JULIANDAY other than some test examples? I'll look at some examples of JULIANDAY in articles later. Johnuniq (talk) 23:31, 17 June 2018 (UTC)
- Hi Johnuniq, thanks for your points here and I agree we should discuss it. I have no problem using the current
The problem is that templates like {{update after}} can pass expressions in a parameter, such as 18+0
for the day. That works with the old template because everything is wrapped in a #expr and so is evaluated. The module would have to use mw.ext.ParserFunctions.expr
on each parameter if it's not a number. Problem: the module supports single-parameter dates, for example, these are the same (there is no comma for the first case since some templates expect a value without commas when used like this):
{{age in days|2001|5|23|2011|6|1}}
→ 3661{{age in days|May 23, 2001|1 June 2011}}
→ 3,661
I'll need a while to think about whether that can be supported for JULIANDAY while evaluating non-numbers as an expression. Johnuniq (talk) 09:52, 18 June 2018 (UTC)
Hmm, I tried something and will think about it later:
{{JULIANDAY|2012|4|30}}
→ 2456048{{JULIANDAY/sandbox|April 30, 2012}}
→ 2456048{{JULIANDAY/sandbox|2012|4|30}}
→ 2456048{{JULIANDAY/sandbox| 2000 + 12 |12-9+1|3*10+0}}
→ Error: Need valid year|month|day or "currentdate"