Template talk:YMD2MJD
Contents
Documentation[edit]
{{YMD2MJD}} converts a calendar date given as yyyy mm dd (year, month, day) to a Modified Julian Date. MJD needs only five decimal digits for dates in this and the previous century, JD needs seven. The remaining numerical precision (after the decimal point) is available for timestamps. As shown for MJD2sec the 12 available digits for #expr: output allow unfortunately only 1/100 seconds for current dates.
Dates starting with 0200-03-01 in the Common Era before the introduction of the Gregorian calendar 1582-10-15 (Rome), 1752-09-14 (UK), etc. use the proleptic Gregorian calendar.
Dates with null year (1 BC) or negative years (2 BC, and earlier) are handled as in the w:en:proleptic Julian calendar (the Julian calendar with Augustus rules on leap years was actually not applied before 8 AD, the initial Caesar's Julian calendar starting sometime after 45 BC using different triennial lead year rules suspended for 12 years by Augustus, so all years before 8 AD are also handled as in the proleptic Julian calendar), with astronomical year numbering (ISO year 0 is 1 BC, ISO year -1 is 2 BC, and so on). "Negative" dates are rarely used, but if they are used they are generally Julian dates. One famous example is 1 January 4713 BC (ISO year -4712) at noon, the conventional start of the JD epoch.
From 0200-03-01 12:00:00 to 0300-02-28 23:59:59, the Julian calendar and the proleptic Gregorian calendar both agree. So this template uses a smooth transition from Julian to proleptic Gregorian calendar dates at an arbitrary date anywhere within this third century, instead of using the legal date of change from the Julian calendar to the Gregorian calendar a dozen of centuries later (or more, because the effective date of adoption depends on countries or cultures) with an abrupt shift of dates (e.g. 10 days were skipped from the calendar when the Gregorian calendar was first adopted in 1582 or in the 17th century, 11 days were skipped in the 18th century, 12 days in the 19th century, 13 days in the 20th and 21st centuries).
Please note that there are already proposals for another calendar reform in the year 2800 or 4000, not only proleptic dates are dubious. The advantage of counting seconds or days is to avoid such transition problems.
Julian dates (JD) also count days, the difference between modified Julian dates (MJD) and JD is constant:
- MJD = JD - 2400000.5, JD starts at 12:00 UTC
- JD = MJD + 2400000.5, MJD starts at 00:00 UTC
This template does not count days starting at noon conventionally in the Julian calendar, but still count them starting at midnight. In the old Roman calendars, times in the morning were considered part of the previous day, but were still reported in reference to the next day with some count of hours before noon on thar next day.
For example, the Julian date "17 August 80, 03:00 before noon" in the morning was formally part of the 16 August day and should noted numerically as "0080-08-17 -03:00 JD", or "0080-08-17 03:00bn JD", or "0080-08-16 09:00am JD" (modern 12-hour clock convention), but not "0080-08-16 09:00 JD" (ambiguous 12-hour clock) or "0080-08-16 21:00 JD" (modern 24-hour clock) which both remain ambiguous. So, this template still uses the hour=09 parameter value, not the hour=-03 parameter value (which would be computed as 3 hours before the start of the day at midnight), and will interpret the parameter hour=12 as meaning noon (in the 24-hour clock convention).
This practice for noting time of day before and after noon disappeared when the day was officially divided in 24 equal hours (including the night hours) starting at midnight and always counted positively, instead of a single clock with hours of variable lengths (depending on seasons), counted positively or negatively, and only when the Sun was visible (and where nightly hours not counted, or just reported counting the whole night at the same date from the previous noon). In these ancient period, accurate clocks still did not exist, so time was very imprecise and most documents do not specify it but just indicate the same date for the morning and afternoon and the modern nightly time "02:00am" ("02:00" since midnight in 24-hour clock notation) would be 10 hours before noon in old texts, if it was measurable.
Note also that this template also uses a single timezone, UTC time, without daylight saving. To convert a local time, just add the local timezone offset to the numeric value of the hour parameter.
Usage[edit]
- {{YMD2MJD}} converts the current date: , when no year parameter is specified.
- {{YMD2MJD|yyyy}} converts the date on 1 January in the specified year, at noon.
- {{YMD2MJD|yyyy|mm|dd}} converts a given date, at noon, see below for details.
- {{YMD2MJD|yyyy|mm|dd|hh|MM|ss}} converts a given date, at specified time, see below for details.
- {{#expr: (3+{{YMD2MJD}} ) mod 7}} = 0, {{CURRENTDOW}} = 0
- {{YMD2MJD|1858|11|17}} = 0, begin of MJD epoch
- {{YMD2MJD|1995|09|27}} = 49987, USNO test date (49987)
- All years before the 3rd century are interpreted in the Julian calendar (years before Christian era are passed negatively — up to yyyy=0000, which means 1 BC, the year immediately before 1 AD).
- All years after the 3rd century are interpreted in the Gregorian calendar (or proleptic Gregorian calendar).
- Years in the 3rd century (from 0201 to 0300) are interpreted identically in both calendars.
Test suite[edit]
Below you find a part of a ported REXX MJD test suite, it includes 1970-01-01, the base of POSIX timestamps (counting seconds instead of days). It also contains some obscure dates where the author ran into debugging problems. The test dates for 1895, 1896, 1900, and 2000 are adapted from JD.
Code | Result | MJD | JD | REXX | Note |
---|---|---|---|---|---|
{{YMD2MJD|-4713|12|30|12|00|00}} = | -2400000.5 | ? | ? | ? | Start of proleptic Julian calendar? |
{{YMD2MJD|-0044|02|28}} = | -694954 | -694953 | 1705047.5 | -16378 | End of proleptic Julian calendar |
{{YMD2MJD|-0044|03|01}} = | -694952 | -694952 | 1705048.5 | -16377 | Start of Julian calendar |
{{YMD2MJD|0000|03|01}} = | -678881 | -678881 | 1721119.5 | -306 | |
{{YMD2MJD|0001|01|01}} = | -678575 | -678575 | 1721425.5 | 0 | |
{{YMD2MJD|0001|01|31}} = | -678545 | -678545 | 1721455.5 | 30 | |
{{YMD2MJD|0001|02|01}} = | -678544 | -678544 | 1721456.5 | 31 | |
{{YMD2MJD|0001|02|28}} = | -678517 | -678517 | 1721483.5 | 58 | |
{{YMD2MJD|0001|03|01}} = | -678516 | -678516 | 1721484.5 | 59 | |
{{YMD2MJD|0001|08|31}} = | -678333 | -678333 | 1721667.5 | 242 | |
{{YMD2MJD|0001|09|01}} = | -678332 | -678332 | 1721668.5 | 243 | |
{{YMD2MJD|0004|02|29}} = | -677421 | -677421 | 1722579.5 | 1154 | |
{{YMD2MJD|0004|03|01}} = | -677420 | -677420 | 1722580.5 | 1155 | |
{{YMD2MJD|0300|02|28}} = | -569310 | -569299 | 1830701.5 | 109276 | (actual Julian date: 0300-02-29) |
{{YMD2MJD|0300|03|01}} = | -569309 | -569298 | 1830702.5 | 109277 | Start of the proleptic Gregorian calendar (for the smooth transition; identical Julian date) |
{{YMD2MJD|0400|02|28}} = | -532786 | -569299 | 1830701.5 | 109276 | End of the Julian calendar (for the smooth transition; identical Gregorian date) |
{{YMD2MJD|0400|03|01}} = | -532784 | -569298 | 1830702.5 | 109277 | (equivalent Julian date: 0400-02-29) |
{{YMD2MJD|1259|03|02}} = | -219040 | -219040 | 2180960.5 | 459535 | |
{{YMD2MJD|1516|12|30}} = | -124870 | -124870 | 2275130.5 | 553705 | |
{{YMD2MJD|1582|10|14}} = | -100841 | -100841 | 2299159.5 | ? | End of the proleptic Gregorian calendar (actual Julian date: 1582-10-04) |
{{YMD2MJD|1582|10|15}} = | -100840 | -100840 | 2299160.5 | 577735 | Start of the Gregorian calendar (equivalent Julian date: 1582-10-05, where Gregorian calendar is not applied) |
{{YMD2MJD|1752|09|14}} = | -38779 | -38779 | 2361221.5 | 639796 | |
{{YMD2MJD|1858|11|17}} = | 0 | 0 | 2400000.5 | 678575 | |
{{YMD2MJD|1895|02|28}} = | 13252 | 13252 | 2413252.5 | 691827 | |
{{YMD2MJD|1895|03|01}} = | 13253 | 13253 | 2413253.5 | 691828 | |
{{YMD2MJD|1896|02|29}} = | 13618 | 13618 | 2413618.5 | 692193 | |
{{YMD2MJD|1896|03|01}} = | 13619 | 13619 | 2413619.5 | 692194 | |
{{YMD2MJD|1900|02|28}} = | 15078 | 15078 | 2415078.5 | 693653 | |
{{YMD2MJD|1900|03|01}} = | 15079 | 15079 | 2415079.5 | 693654 | |
{{YMD2MJD|1970|01|01}} = | 40587 | 40587 | 2440587.5 | 719162 | |
{{YMD2MJD|1977|01|01}} = | 43144 | 43144 | 2443144.5 | 721719 | |
{{YMD2MJD|2000|02|29}} = | 51603 | 51603 | 2451603.5 | 730178 | |
{{YMD2MJD|2000|03|01}} = | 51604 | 51604 | 2451604.5 | 730179 | |
{{YMD2MJD|2738|11|28}} = | 321424 | 321424 | 2721424.5 | 999999 |
Input ranges[edit]
Interpretation of input parameters out of standard ranges: enabled
- {{YMD2MJD|2000.5|1|1}} = 51726 // denormalized year with fraction (half-year)
- {{YMD2MJD|2000|07|01}} = 51726 // year normalized by adding 6 months
- {{YMD2MJD|2000|13|01}} = 51910 // unnormalized month (>12)
- {{YMD2MJD|2001|01|01}} = 51910 // month normalized to next year
- {{YMD2MJD|2000|00|31}} = 51543 // unnormalized month (<1)
- {{YMD2MJD|2000|-1|01}} = 51483 // month normalized to previous year
- {{YMD2MJD|2000|01|00}} = 51543 // unnormalized day (<1)
- {{YMD2MJD|1999|12|31}} = 51543 // day normalized to previous month
- {{YMD2MJD|2000|01|32}} = 51575 // unnormalized day (>31 in January)
- {{YMD2MJD|2000|02|01}} = 51575 // day unnormalized to next month
- {{YMD2MJD|2000|1|1.5}} = 51544.5 // fraction of day (half day)
- {{YMD2MJD|2000|1|1|12}} = 51544.5 // fraction of day normalized as time (noon)
Minimal and maximal date not resulting in exponential notation:
- {{YMD2MJD|-2737848929|9|18}} = -999999999998
- {{YMD2MJD|-2737848929|9|19}} = -999999999997
- {{YMD2MJD|2737908865|11|11}} = 999999999999
- {{YMD2MJD|2737908865|11|12}} = 1000000000000
Smooth proleptic Gregorian to Julian calendar date transition:
- {{YMD2MJD|-4712|01|01}} = -2399999, proleptic Julian calendar.
- {{MJD|-4712|01|01|12}} = -2399998.5, Julian Day 0 at noon.
- {{YMD2MJD|-4712|01|02}} = -2399998, JD = +0.5 (4713 BC).
Start of 1st century (on 0001-01-01 BC (Anno Domini) in Julian calendar, a leap year
- {{YMD2MJD|0000|01|01}} = -678941, 1st day March 1 BC (Start of Julian CE)
- ...
- {{YMD2MJD|0000|12|31}} = -678576, 1st day March 1 BC (Start of Julian CE)
Start of 1st century (on 0001-01-01 CE in Julian calendar, NOT a leap year
- {{YMD2MJD|0001|01|01}} = -678516, 1st day March 1 (Start of Julian CE)
- {{YMD2MJD|0001|02|28}} = -678516, 1st day March 1 (Start of Roman CE)
- {{YMD2MJD|0001|03|01}} = -678516, 1st day March 1 (Start of Roman CE)
- ...
- {{YMD2MJD|0099|12|31}} = -642417, 1st day March 1 (Start of Roman CE)
- {{YMD2MJD|0100|01|01}} = -642416, 1st day January 100
- ...
- {{YMD2MJD|0100|02|28}} = -642358, last day February 100 (Gregorian)
- {{YMD2MJD|0100|02|29}} = -642357, last day February 100 (Julian)
End of 1st century (on 0100-02-29 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar)
Start of 2rd century (on 0100-03-01 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar):
- {{YMD2MJD|0100|03|01}} = -642356
- {{YMD2MJD|0100|03|02}} = -642355, {{MJD2D|-642357}} = 1 (Gregorian proleptic)
- ...
- {{YMD2MJD|0199|12|31}} = -605892, last day December 199
- {{YMD2MJD|0200|01|01}} = -605891, 1st day January 200
- ...
- {{YMD2MJD|0200|02|27}} = -605834, {{MJD2D|-605836}} = 26 (Gregorian proleptic)
- {{YMD2MJD|0200|02|28}} = -605833, last day February 200 (Gregorian proleptic)
- {{YMD2MJD|0200|02|29}} = -605832, last day February 200 (Julian)
End of 2rd century (on 0200-02-29 in Julian calendar, a leap year, but NOT leap in Gregorian poleptic calendar)
- {{YMD2MJD|0200|02|30}} = -605831, next day
Start of 3rd century (on 0200-03-01 in both calendars):
- {{YMD2MJD|0200|03|01}} = -605831, {{MJD2D|-605833}} = 1
- ...
- {{YMD2MJD|0299|12|31}} = -569369, last day December 299
- {{YMD2MJD|0300|01|01}} = -569368, 1st day January 300
- ...
- {{YMD2MJD|0300|02|28}} = -569310, last day February 300
End of 3rd century (on 0300-02-29 in Julian calendar, a leap year).
- {{YMD2MJD|0300|02|29}} = -569309, 300 was no Gregorian leap year, next day
Maps to start of 4th century (on 0300-03-01 in Gregorian calendar, NOT a leap year): 1st difference
Optional substitution works if all three parameters and subst=subst: are specified, or for the current date {{subst:YMD2MJD|subst=subst:}}.
See also[edit]
- template:YMD2MJD (edit•talk•links•history)
- template:Dectime (edit•talk•links•history)
- template:MJD (edit•talk•links•history)
- template:MJD2D (edit•talk•links•history)
- template:MJD2sec (edit•talk•links•history)
- template:Sec2MJD (edit•talk•links•history)
- template:Timestamp2MJD (edit•talk•links•history)
- template:Gsd (edit•talk•links•history)
- w:Template:Gregorian serial date
Discussion[edit]
- Add issues below as you see fit, sign with ~~~~
Empty output with denormal numbers[edit]
- "0.9900000001 - floor(0.9900000001 / 0.03) * 0.03" = "1.0000000827404E-10"
- This uses PHP's fmod() function but arguments are left as double, no rounding occurs and precision is kept
- Expected value: 1E-10 with 8 digits of precision
- The result is: 1.0000000827404E-10 (excellent order of magnitude and precision)
- "0.9900000001 mod 0.03" = "Expression error: Division by zero"
- This uses PHP's fmod() function but arguments are incorrectly downcasted to float, forcing rounding and loss of precision.
- Expected value: 1E-10 with 8 digits of precision
- The result was: 4.4408920985006E-16 (bad: 6 orders of magnitude lost, out of precision)
- Now this is incorrect (the result is an empty string)
- "(2 * pi) - floor( (2 * pi) / (pi / 6) ) * (pi / 6)" = "0"
- Expected value: 0 with an absolute error not exceeding pi/2*1E-14
- Effective result: 0 (excellent, but could be by chance; for other divisors set to (PI/N), where N is an odd prime, the max error of PI/2/N*1E-14 is respected)
- "(2 * pi) mod (pi / 6)" = "Expression error: Division by zero"
- Expected value: 0 with an absolute error not exceeding pi/2*1E-14
- Effective result was: 1.0000003602961E-10 (very bad, the absolute error is too large by a factor of about 1E7)
- Now this is incorrect (the result is an empty string)
So beware of implementations using MOD, floor() is always better. verdy_p (talk) 15:34, 27 December 2012 (UTC)