Jump to content

Module talk:Date

Page contents not supported in other languages.
Add topic
From Wikipedia, the free encyclopedia
(Redirected from Module talk:Date/doc)

Template-protected edit request on 15 May 2022

[edit]
Tostkeoydouxyod (talk) 17:21, 15 May 2022 (UTC)Reply

Date active: 2011-always

 Not done not a scrubinto change. — xaosflux Talk 17:25, 15 May 2022 (UTC)Reply

Date math

[edit]

@Johnuniq: (or anyone else) is there an easy way to basic date math? I.e.

local today = Date('currentdate')
local last_year = today - 1.year

I know I can grab the year, subtract one and form a new date by mashing strings together but before I go that route I want to make sure I’m not missing an easier solution… Thanks! Zackmann (Talk to me/What I been doing) 20:32, 30 September 2025 (UTC)Reply

@Zackmann08: It looks like you want to do this in a module. Before looking at that, you might like to know about {{extract}}. Examples:
  • {{extract|currentdate|add=-1y}} → 30 November 2025
  • {{extract|currentdate|add=-1y|show=year}} → 2025
Of course #time can do easily do the above. For a module, if all that is wanted is last year, try
local last_year = require('Module:Date')._current.year - 1
Johnuniq (talk) 23:46, 30 September 2025 (UTC)Reply
Actually, Module:Date is not needed.
local last_year = os.date('!*t').year - 1
Johnuniq (talk) 23:51, 30 September 2025 (UTC)Reply
Stellar! Thanks so much for the info! - Zackmann (Talk to me/What I been doing) 00:46, 1 October 2025 (UTC)Reply

Month abbreviations

[edit]

In my work on Module:Person date I ran into an issue where abbreviated months (i.e. Aug. Jan. etc.) were not working. I traced through my code and turns out it comes down to a call where I basically do Date('1 Apr. 2016'). By my read of lines 571-582, those abbreviations are not supported by Module:Date. I'm wondering if they can be? I've found a number of cases in articles of people posting dates with this abbreviated form. I think this is a simple change which I have mocked up in the sandbox (see the comparison). That being said, given how widely used this module is I am VERY wary of implementing any change without a thorough code review. @Johnuniq: would love to hear your thoughts. --Zackmann (Talk to me/What I been doing) 08:12, 3 October 2025 (UTC)Reply

The ustring family is very clever but it is not needed in this case. I put a more efficient version in the sandbox. It's probably ok (I ran some tests). The issue is what abbreviations should be supported as opposed to fixing the wikitext. What about '1 Ap. 2016' and similar? Johnuniq (talk) 08:50, 3 October 2025 (UTC)Reply
That looks perfect to me. I would think that the non-period abbreviations you already have spelled out in lines 571-582 cover it. A google search turned up this from the Yale Library for what that's worth. I don't think I've ever seen anyone intentionally abbreviate a month with only 2 letters (Oc or De for example) and I'm not in favor of trying to cover EVERY scenario, but covering the basic case of a period after a 3 (or in the case of Sept, a 4) letter month seems like it would be super helpful. --Zackmann (Talk to me/What I been doing) 09:08, 3 October 2025 (UTC)Reply
@Johnuniq: I tried to create Module:Date/testcases but seem to be doing something wrong because it isn't actually calling the code. If you can point me in the right direction, I'd be happy to imlement more tests... Zackmann (Talk to me/What I been doing) 21:22, 4 October 2025 (UTC)Reply
@Zackmann08: I fixed testcases. The module uses the convention that entry points callable from another module start with an underscore. That is, _Date not Date. FYI I run tests on 1270 cases on a local computer. It's too hard to do that here so that's why I never wrote a testcases page. However, testcases should exist, so thanks. You might like to have a look at Module:Date/example and its talk. Johnuniq (talk) 01:01, 5 October 2025 (UTC)Reply
This is going to require some more investigating... See Module talk:Date/testcases. Nothing is being output by the current testcases. I think I need to call the :text() method and I'm not entirely sure how to do that...
Is reassuring to know that you have a nice test suite that you run locally though! Zackmann (Talk to me/What I been doing) 01:13, 5 October 2025 (UTC)Reply
Sorry, I wasn't paying attention. You can see that I don't know anything about Module:UnitTests. Its documentation points to Module:ScribuntoUnit which a very quick glance suggests might work. Johnuniq (talk) 02:11, 5 October 2025 (UTC)Reply
I shall investigate further. Thanks! --Zackmann (Talk to me/What I been doing) 02:14, 5 October 2025 (UTC)Reply
@Johnuniq: in the meantime, what are your thoughts on bringing the sandbox version live? Were you able to run your test suite against the change? - Zackmann (Talk to me/What I been doing) 09:42, 6 October 2025 (UTC)Reply
I updated the main module. All my tests pass. Johnuniq (talk) 10:05, 6 October 2025 (UTC)Reply

Detecting format of date

[edit]

@Johnuniq: is there a way to detect the input format used to build a date? For example:

local Date = require('Module:Date')._Date
mw.log(Date('1 July 2016'))  -- Outputs 1 July 2016
mw.log(Date('July 1, 2016')) -- Outputs 1 July 2016

Now I know that I can override the output with :text('mdy') but say I do not know the format that is being supplied... I want to accept both mdy format as well as dmy format. I want to do some calculation, then return the result in the same format it was supplied. So if I started with mdy, I want to end with mdy and ditto for dmy.

Is there someway to detect which format was sent in?? If not, is this something that could be added? FWIW this is blocking a merge that I can explain further if you are interested, but suffice it to say this capability would be VERY helpful. Zackmann (Talk to me/What I been doing) 03:38, 4 December 2025 (UTC)Reply

If d is a valid date (that is, not nil), d.format is a string: dmy, mdy, ymd, error. See {{extract}} which documents show=format. I had forgotten how a module does that so I looked in Module:Date/example (linked in the doc) and it has an example. Johnuniq (talk) 04:45, 4 December 2025 (UTC)Reply
You have NO IDEA what a headache you just saved me... I thought I was going to have to write that functionality myself and I was preparing to put my head through a wall... Thank you!!!!!!! Zackmann (Talk to me/What I been doing) 04:50, 4 December 2025 (UTC)Reply

Problem with minute/second from recent change

[edit]

@Krinkle: I don't have spare thinking capacity at the moment and cannot follow the reason (phab:T416616) for the recent change at Module:Date. I'm not complaining, I'm just saying I would need some quality time to understand the point. Unfortunately the module exports _current described as "Table with the current year, month, day, hour, minute, second." in documentation on the module page. However, the change means "minute, second" is now "min, sec". I don't know of any modules using those fields but it's a big world and there may be a problem. I'll try to think about this in the coming days but a metatable like there was before might be needed? I put a demo at Module:Sandbox/Johnuniq/temp. Johnuniq (talk) 09:11, 18 February 2026 (UTC)Reply

I'm aware and found it unused (Global Search). I suggest we remove it. Even for debugging it isn't needed, as one can use os.date() in the debug console, and other modules can likewise use their own call. Krinkle (talk) 09:35, 18 February 2026 (UTC)Reply
OK, I'll think about it and try to work out why I included it. Johnuniq (talk) 10:03, 18 February 2026 (UTC)Reply

I'm repeating the phab:T416616 link above to remind myself and alert any interested page followers that there is a proposal to adopt some of Module:Date's functionality into Scribunto. The motivation is to avoid the cache period on pages using Module:Date dropping to 30 minutes. Johnuniq (talk) 07:25, 21 November 2026 (UTC)Reply

Expanded testcases ahead of mw.date refactor

[edit]

Johnuniq, I expanded Module:Date/testcases to pin down more of the module's current behavior ahead of any refactoring to use the proposed Scribunto mw.date library (T416616). All tests pass on Module talk:Date/testcases.

Added coverage:

  • Diff boundaries: month-end borrow thresholds (28-, 29-, 30-, and 31-day previous months), leap-day birthdays across leap/non-leap/century years (including 2026-02-01 - 2024-02-29 = 1y 11m 3d), Julian diffs, date1/date2 swapping on negative diffs, and the diff round-trip invariant.
  • Age and duration: remaining unit codes (dhm, dhms, hm, hms, M, s, ymdh, ymdhm, ymwdh, ymwdhm), rounding carries (e.g., 11 months 16 days carrying to the next year), and duration month/year rollovers.
  • Partial dates: leap/non-leap bounds, partial-vs-full comparisons, overlapping/negative partial diffs, and fill = true day clamping.
  • Date('currentdate'): properties, copying/overriding, arithmetic (+ 1, - 1, + '1m', + '1y', etc.), comparisons against fixed/partial/Julian dates, and diffs/ages compared against a fixed date for today.

A few existing quirks are asserted as-is and marked with FIXME? comments so any behavior change during migration is visible rather than silent:

  • Date(2020, 3, 2, 0, 3, 40) - Date(2020, 3, 1, 0, 3, 40) yields 0y 0m 0d 24:00:00 due to floating-point rounding in day + hms.
  • Date('partial', 2001, 4) == Date(2001, 4, 15) and Date('partial', 2001, 4) < Date(2001, 4, 15) are both true.
  • Cross-calendar < with a partial date always orders Julian before Gregorian regardless of year.
  • Adding years and months separately (date2 + '1y' + '11m' + 3) clamps 2024-02-29 + '1y' to 2025-02-28, whereas adding combined months ('23m') round-trips cleanly.

Please let me know if any of the assertions don't match intended behavior. ATDT (talk) 16:05, 28 November 2026 (UTC)Reply

@ATDT: It's great to see this interest in the date modules—thanks for your work. It's ten years since my main development so it's a blur now. Some quirks may be bugs but it's likely that others were attempts to be compatible with the large variety of somewhat-broken templates that Module:Date and Module:Age were intended to replace. I do recall that "orders Julian before Gregorian regardless of year" was intended as I couldn't think of any good reason to compare such dates. Given that the Gregorian calendar was adopted at wildly different times at different places, there is no sensible way to identify a precise moment in time without specifying the date and location and intention of the person writing the date. Since Gregorian dates were adopted after Julian, and since I wanted the module to do something reasonably sane, it assumes Julian < Gregorian.
Current efforts concern moving core components of Module:Date into Scribunto for better control of when cached pages are updated. If you needed to go further and think about where and how Date is used, links at User:Johnuniq/index#Age/date might be useful for further information. Some items are only properly documented at places like Module:Age and Template:Extract.
I'm looking at the FIXME comments in Module:Date/testcases, the first of which points out that Date('2021', '1', '1.5') shows that a fractional day is accepted. I can't make sense of that at the moment but my guess is that the fractional day was intended as shown in this test:
  • {{extract|2001|1|1|sortable=debug}} → 7006245191050000000♠1 January 2001 2001-01-01 at start of day
  • {{extract|2001|1|1.5|sortable=debug}} → 7006245191100000000♠1 January 2001 2001-01-01 at half way through day
  • {{extract|2001|1|1|12|sortable=debug}} → 7006245191100000000♠12:00 1 January 2001 2001-01-01 at 12 noon
That shows that half a day gives the same sort key as 12 noon so something is working that out. My attempts to make Date show half a day as a time have only produced 0 hours 0 minutes 0 seconds. I suspect that is because nothing in the input set Date's hastime to true. I would need a couple more hours to decide if that's a feature or a bug. I don't have much thinking time at the moment but I will return with more this week. Johnuniq (talk) 02:48, 29 November 2026 (UTC)Reply
After a few hours elsewhere, I just looked at the code for the "orders Julian before Gregorian regardless of year" issue. It looks like my above recollection is confused. It is true that I never saw a reason to compare Julian and Gregorian dates but, as you note, Module:Date will do that comparison if full dates are given. Perhaps I didn't want to think about the complexity of partial dates for such cases since there was no need? Johnuniq (talk) 10:22, 29 November 2026 (UTC)Reply
@Johnuniq: Thanks for looking into those! No need to dig further into the FIXMEs on my account. I put the draft mw.date change in Module:Date/sandbox (diff, details at T416616). We're planning to leave it in the sandbox until mw.date rolls out to enwiki so we can test it live first, and I'll ping here once it's ready to sync. ATDT (talk) 20:09, 29 November 2026 (UTC)Reply