From 2ecb8929858cb4c6f222d74097f6e8fc6b19da92 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 11 Dec 2022 10:23:05 +0200 Subject: [PATCH] time: add Time.format_rfc3339() method --- vlib/time/format.v | 7 +++++++ vlib/time/time_test.v | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/vlib/time/format.v b/vlib/time/format.v index 4950c5624d..bff6fa83e7 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -20,6 +20,13 @@ 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.microsecond / 1000):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 { + return '${t.year:04d}-${t.month:02d}-${t.day:02d}T${t.hour:02d}:${t.minute:02d}:${t.second:02d}.${(t.microsecond / 1000):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.microsecond:06d}' diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index 2514e7e9ce..2772aeb78f 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -93,6 +93,10 @@ fn test_format_ss_milli() { assert '1980-07-11 21:23:42.123' == time_to_test.format_ss_milli() } +fn test_format_rfc3339() { + assert '1980-07-11T21:23:42.123Z' == time_to_test.format_rfc3339() +} + fn test_format_ss_micro() { assert '11.07.1980 21:23:42.123456' == time_to_test.get_fmt_str(.dot, .hhmmss24_micro, .ddmmyyyy)