From 286d39706bb3f99ddb07f3370c08e7d55dc08a72 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 8 Aug 2023 08:33:05 +0300 Subject: [PATCH] time: add a format_rfc3339_nano() method to time.Time --- vlib/time/format.v | 22 ++++++++++++++-------- vlib/time/time_test.v | 7 +++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/vlib/time/format.v b/vlib/time/format.v index 247b8e49b1..79f913610d 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -20,14 +20,6 @@ pub fn (t Time) format_ss_milli() string { return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000_000):03d}' } -// format_rfc3339 returns a date string in "YYYY-MM-DDTHH:mm:ss.123Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html) -// RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar. -// It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols. -pub fn (t Time) format_rfc3339() string { - u := t.local_to_utc() - return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond / 1_000_000):03d}Z' -} - // format_ss_micro returns a date string in "YYYY-MM-DD HH:mm:ss.123456" format (24h). pub fn (t Time) format_ss_micro() string { return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.nanosecond / 1_000):06d}' @@ -38,6 +30,20 @@ pub fn (t Time) format_ss_nano() string { return '${t.year:04d}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${t.nanosecond:09d}' } +// format_rfc3339 returns a date string in "YYYY-MM-DDTHH:mm:ss.123Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html) +// RFC3339 is an Internet profile, based on the ISO 8601 standard for for representation of dates and times using the Gregorian calendar. +// It is intended to improve consistency and interoperability, when representing and using date and time in Internet protocols. +pub fn (t Time) format_rfc3339() string { + u := t.local_to_utc() + return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond / 1_000_000):03d}Z' +} + +// format_rfc3339_nano returns a date string in "YYYY-MM-DDTHH:mm:ss.123456789Z" format (24 hours, see https://www.rfc-editor.org/rfc/rfc3339.html) +pub fn (t Time) format_rfc3339_nano() string { + u := t.local_to_utc() + return '${u.year:04d}-${u.month:02d}-${u.day:02d}T${u.hour:02d}:${u.minute:02d}:${u.second:02d}.${(u.nanosecond):09d}Z' +} + // hhmm returns a date string in "HH:mm" format (24h). pub fn (t Time) hhmm() string { return '${t.hour:02d}:${t.minute:02d}' diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 23c13351c5..6791f52e40 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -89,6 +89,13 @@ fn test_format_rfc3339() { assert res.contains('T') } +fn test_format_rfc3339_nano() { + res := time_to_test.format_rfc3339_nano() + assert res.ends_with('23:42.123456789Z') + assert res.starts_with('1980-07-1') + assert res.contains('T') +} + fn test_format_ss() { assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy) }