dateYMD
Converts a serial Julian date into day, month and year values
You're viewing an older version of this page (#194). View the current version.
Interface
#include <codecogs/units/date/dateymd.h>
using namespace Units::Date;
Overview
This function converts a serial Julian date, into a year, month and day, thereby performing the inverse of date. For the Gregorian calendar this conversion is achieved using:
See:
The opposite of this function is date
Example 1
#include <stdio.h>
#include <codecogs/units/date/dateymd.h>
using namespace Units::Date;
int main()
{
int d,m,y;
dateYMD(36922, y, m, d);
printf("\n The Gregorian Date is: day=%d month=%d year=%d", d, m, y); // 6 May 1987
dateYMD(36922, y, m, d, cal_Julian);
printf("\n The Julian Date is: day=%d month=%d year=%d", d, m, y); // 23 April 1987
return 0;
}Parameters
- Gregorian (default)
- Julian
- cal_Excel - same as Gregorian, we can see little benefit of subtracting 1900 from the year (easily done yourself!)
References
Tondering, C. 2003. http://www.tondering.dk/claus/cal/node3.html
FUNCTION
dateYMD
This function uses a number of rules to work out the most likely date, returning the individual elements (Year, Month and Day) of the date. Unlike the Excel version this function is extremely forgiving, it will therefore interpret most date forms and will not return an error.
Anyone wanting a challenge can try optimising this function and including an option to allow alternative defaults.
Example 1
This example is intended to illustrates the type of dates that can be processed, rather than to being functional (see date for a further examples).
#include <stdio.h>
#include <codecogs/units/date/date.h>
using namespace Units::Date;
void main()
{
int d,m,y;
dateYMD("3245/4 20", y,m,d, cal_Excel); // 20 April 3245
dateYMD("4February 4", y,m,d); // 4 February 0004
dateYMD("40 mar 1664", y,m,d); // 40 March 1664 !!!
dateYMD("24-10/40", y,m,d, cal_Excel); // 24 October 1940
dateYMD("12@31@4", y,m,d); // 12 April 0031
dateYMD("16 5 march", y,m,d, cal_Excel); // 16 March 2005
dateYMD("jansomerubbish4 10", y,m,d, cal_Excel, 10); // 4 January 2010
}Parameters
- Any separator that isn't numeric or alphabetic can be used (i.e. non alphanumeric). So you can not have negative years, because '-' is often used as a separator.
- Only the first 3 terms will be processed. If only 2 terms are specified, today's year is added to the end.
- All things being equal the default date order is: day, month, year. However:
- if the Year is definitely first, the default becomes year, month, day.
- if the Month is definitely first, the default becomes month, day, year.
- Any number>31 is assumed to be year. So "1985/3/4' is the 4 March 1985 (N.B. in this example date order is now year/month/day)
- Numbers less than or equal to 12 get a higher 'month' weighting, so "3/30/12" will be assumed to the 3 December 1930.
- If the month is obvious then the function checks the remaining numbers against possible length for that month. So "31 February 2" is 2 February 1931, because February doesn't have 31 days. However, "31 February 2002" will return 31 February 2002, because neither 31 nor 2002 are valid days for February, so we switch to the default format of day/month/year. Equally "20 March 3", is 20 March 2003.
- Only the first 3 characters of a text month description are used, the rest is ignored.
Parameters
This function can extract an illegal date, e.g. dateYMD("31 February 2004",y,m,d) will return the 31st February 2004. To be confident of having a legitimate date, use date instead, e.g. date("31 February 2004") will return the 2 March 2004.
Will Bateman (Sep 2004)