Hi Artur (and everybody who's interested),
I'm trying to leverage your calendar-related package and have some
questions, especially about the function date_to_iso8601. First of all,
it would be useful if the doc explained a little bit more what's
actually supported. Internally the function uses the built-in strptime
and so on, so maybe it would be enough to refer to gretl's function
reference for that.
Then, I'm observing that the result for date_to_iso8601("1984",
"%Y")
is: 19831231. Is this a bug? I would have expected 19840101.
Next, my feature request would be to support other date specifications
used by gretl as well, such as for quarterly data things like "1974:3".
(It does work with such monthly specifications, apart from an apparent
bug similar to the one above.) I'm aware that quarterly data isn't
covered by ISO8601.
At the end of this message is a code proposal on how to deal with such
quarterly labels. Your date_to_iso8601 function could perhaps allow a
"Q" code in the date_format argument and do something like this:
if instring(toupper(date_format), "Q")
scalar iso8601 = gretlquarter_to_iso8601(date)
else
... < existing part of your function>
endif
What do you think?
thanks
sven
------
<hansl>
function scalar gretlquarter_to_iso8601(string date)
# This function only supports quarterly date strings
# as used and accepted by gretl itself;
# basically only patterns like "1999:3" or "1999.3".
errorif(!instring(date, ":") && !instring(date, "."), \
"not a recognized quarterly date string")
# harmonize and check
date = strstrip(strsub(date, ".", ":"))
strings parts = strsplit(date, ":")
errorif(nelem(parts) != 2, "invalid date string input")
# convert (and check)
year = atof(parts[1])
quarter = atof(parts[2])
errorif(year < 1000 || year > 9999, "year out of range")
errorif(quarter < 1 || quarter > 4, "quarter out of range")
# construct the ISO output
out = year * 10000 # year
out += ((quarter-1) * 3 + 1) * 100 # first month in quarter
out += 1 # first day in month
return out
end function
# some tests
eval gretlquarter_to_iso8601("2004:4")
eval gretlquarter_to_iso8601("20000:1") # wrong
eval gretlquarter_to_iso8601("2000-3") # also wrong
eval gretlquarter_to_iso8601("1901:12") # wrong
eval gretlquarter_to_iso8601("1900:3")
</hansl>