jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

Add a date library

Open sparkprime opened this issue 11 years ago • 7 comments
trafficstars

Code to convert to/from tuples like {year: x, month:x, ... seconds: x } into textual / other structured representations (seconds since epoch, etc).

sparkprime avatar Nov 06 '14 04:11 sparkprime

@oconnorr

sparkprime avatar Jan 13 '16 23:01 sparkprime

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.

oconnorr avatar Jan 13 '16 23:01 oconnorr

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.

sparkprime avatar Jan 20 '16 00:01 sparkprime

Could we make use of cctz for this?

davidzchen avatar Jan 22 '16 06:01 davidzchen

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.

oconnorr avatar Jan 22 '16 14:01 oconnorr

Any update on this?

simonrouse9461 avatar Jun 28 '24 12:06 simonrouse9461

+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"),
}

daniel-falk avatar Oct 12 '24 10:10 daniel-falk