Top |
Here we have various functions dealing with calendar dates; for the most part these are designed for handling daily time-series data.
guint32 epoch_day_from_julian_ymd (int y
,int m
,int d
);
The y
, m
and d
arguments are assumed to refer to a date on
the Julian calendar. The conversion algorithm is taken from
https://en.wikipedia.org/wiki/Julian_day, where it appears to
be credited to the Department of Computer Science at UT, Austin.
guint32 nearby_epoch_day (int y
,int m
,int d
,int wkdays
);
char * ymd_extended_from_epoch_day (guint32 ed
,int julian
,int *err
);
double ymd_basic_from_epoch_day (guint32 ed
,int julian
,int *err
);
ed |
epoch day (ed >= 1). |
|
julian |
non-zero to use Julian calendar, otherwise Gregorian. |
|
err |
location to receive error code. |
an 8-digit number on the pattern YYYYMMDD (ISO 8601 basic date format) given the epoch day number, which equals 1 for the first of January in the year 1 AD, or NADBL on error.
int julian_ymd_bits_from_epoch_day (guint32 ed
,int *y
,int *m
,int *d
);
Follows the algorithm of E.G. Richards (2013), "Calendars," In S.E. Urban & P.K. Seidelmann, eds. Explanatory Supplement to the Astronomical Almanac, 3rd ed. (pp. 585-624), Mill Valley, CA: University Science Books (as set out on https://en.wikipedia.org/wiki/Julian_day).
There are other algorithms for this purpose on the internet but they are mostly wrong (at least, not right for all dates); many of them fail the round-trip test (date -> epoch day -> date) for some dates.
int iso_basic_to_extended (const double *b
,double *y
,double *m
,double *d
,int n
);
Given the array b
of ISO 8601 "basic" daily dates (YYYYMMDD as
doubles), fill out the arrays y
, m
and d
with year, month
and day.
int day_starts_month (int d
,int m
,int y
,int wkdays
,int *pad
);
int day_ends_month (int d
,int m
,int y
,int wkdays
);
int get_days_in_month (int m
,int y
,int wkdays
,int julian
);
int month_day_index (int y
,int m
,int d
,int wkdays
);
int days_in_month_before (int y
,int m
,int d
,int wkdays
);
int days_in_month_after (int y
,int m
,int d
,int wkdays
);
int daily_index_to_date (char *targ
,int y
,int m
,int idx
,int wkdays
);
Fills out targ
with the calendar data implied by
the specification of y
, m
, seq
and wkdays
,
provided this specification corresponds to an actual
calendar date.
int calendar_obs_number (const char *datestr
,const DATASET *dset
);
int calendar_date_string (char *targ
,int t
,const DATASET *dset
);
Writes to targ
the calendar representation of the date of
observation t
, in the form YY[YY]-MM-DD.
int MS_excel_date_string (char *targ
,int mst
,int pd
,int d1904
);
Writes to targ
the calendar representation of the date of
observation mst
, in the form YYYY-MM-DD if pd
is 0, 5,
6, 7 or 52 (unknown, daily, or weekly frequency), otherwise
in the appropriate format for annual, quarterly or monthly
according to pd
.
double day_of_week (int y
,int m
,int d
,int julian
,int *err
);
y |
year. |
|
m |
month, 1 to 12. |
|
d |
day in month, 1 to 31. |
|
julian |
non-zero to use Julian calendar, otherwise Gregorian. |
|
err |
location to receive error code. |
the day of the week for the supplied date
(Sunday = 0, Monday = 1, ...) or NADBL
on failure
(the date is invalid).
int n_hidden_missing_obs (const DATASET *dset
,int t1
,int t2
);
For daily data with user-supplied data strings,
determine the number of "hidden" missing observations
in the range t1
to t2
inclusive. This is
the difference between the actual number of
observations and the number that should be there,
according to the calendar. Allowance is made for
5- or 6-day data, via the data frequency given
in dset
.
int
guess_daily_pd (const DATASET *dset
);
Based on user-supplied daily date strings recorded in
dset
, try to guess whether the number of observations
per week is 5, 6 or 7 (given that some observations
may be missing).
double
easterdate (int year
);
Algorithm taken from Wikipedia page https://en.wikipedia.org/wiki/Computus under the heading "Anonymous Gregorian algorithm".
int fill_monthlen_array (double *mlen
,int t1
,int t2
,int wkdays
,int mo
,int yr
,const double *movec
,const double *yrvec
,int julian
);
Fills mlen
from t1
to t2
with the number of days in
each month/year pair. It is assumed that at least one
of movec
and yrvec
is non-NULL. The various arrays
are doubles only because in context they will be
dataset series; in concept they are integers.
mlen |
array to be filled. |
|
t1 |
starting index for fill. |
|
t2 |
stopping index for fill. |
|
wkdays |
number of days in week (7, 6 or 5). |
|
mo |
month (ignored if |
|
yr |
year (ignored if |
|
movec |
array of month values (or NULL). |
|
yrvec |
array of year values (or NULL). |
|
julian |
non-zero for Julian calendar, otherwise Gregorian. |
int gretl_strfdate (char *s
,int slen
,const char *format
,guint32 ed
);
If ed
is found to be valid, writes a string representing
the date of ed
to s
, governed by format
.
int gretl_alt_strfdate (char *s
,int slen
,int julian
,guint32 ed
);
If ed
is found to be valid, writes a string representing
the date of ed
to s
, as ISO 8601 extended.
int gretl_strftime (char *s
,int slen
,const char *format
,gint64 t
,double off_secs
);
If t
and format
are found to be valid, writes a string representing
the date and time of t
to s
, governed by format
. If off_secs
is
NADBL it is ignored, and the date and time on output are relative to
local time. Otherwise date and time are relative to the time zone
defined by off_secs
(and so a zero value gives UTC).
s |
target buffer. |
|
slen |
length of target buffer. |
|
format |
as per |
|
t |
Unix time as 64-bit integer. |
|
off_secs |
offset in seconds relative to UTC, or NADBL. |