jsonnet
jsonnet copied to clipboard
Add a date library
Code to convert to/from tuples like {year: x, month:x, ... seconds: x } into textual / other structured representations (seconds since epoch, etc).
@oconnorr
Do you want the Jsonnet output to be dependent on or independent of the contents of the Olson time zone database that happens to be installed on the local system?
If independent, then you cannot do date/time conversions based on time zone (you are restricted to using UTC offsets), but you could write it in Jsonnet.
If dependent, then Jsonnet isn't hermetic and some operations need to be built-in.
I can't remember who initially suggested this or what they wanted it for. But, yes I think it would have to be UTC and static offsets only, otherwise it would no-longer be hermetic.
Could we make use of cctz for this?
Using cctz, or any other tool for dealing with civil time, would make Jsonnet output dependent on the contents of /usr/share/zoneinfo whose content varies from machine to machine and from month to month.
Any update on this?
+1 on this! It would be great to be able to parse dates, compare time and date and calculate time differences. It does not / should not depend on the system timezone in my opinion, but it would be good if it has a built-in calendar so it understands conversion between different time zones. I would like to be able to do tasks such such as:
OBS: hypothetical example
local cert_config = {
issueDate: "2024-10-12T12:42:31.144210+02:00",
durationDays: 30,
};
{
// Calculate issue time as UTC
local issueDate = std.timeParse(cert_config.issueDate),
issueDate: std.timeFormat(issueDate, tz="UTC"),
// Calculate the expiration date
local endDate = std.timeAdd(issueDate, days=cert_config.durationDays),
endDate: std.timeFormat(endDate, tz="UTC"),
// Alert a week before expiration
alertDate: std.timeFormat(std.addTime(endDate, days=-7), tz="UTC"),
// Calculate the duration in seconds
validForSeconds: std.timeDiffConvert(std.timeDiff(endDate, issueDate), unit="seconds"),
}