From 38cf923537aa79bcde9cefc09dc322d61ef651b2 Mon Sep 17 00:00:00 2001 From: David Legrand <1110600+davlgd@users.noreply.github.com> Date: Sun, 18 Feb 2024 13:45:37 +0100 Subject: [PATCH] time: add a .http_header_string() method on Time (#20861) --- vlib/time/format.v | 8 ++++++++ vlib/time/time_format_test.v | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/vlib/time/format.v b/vlib/time/format.v index 9c7a24a1bfad4d..d3e2f9f280d3de 100644 --- a/vlib/time/format.v +++ b/vlib/time/format.v @@ -470,6 +470,14 @@ pub fn (t Time) utc_string() string { return utc_string } +// http_header_string returns a date string in the format used in HTTP headers, as defined in RFC 2616. +pub fn (t Time) http_header_string() string { + day_str := t.weekday_str() + month_str := t.smonth() + http_header_string := '${day_str}, ${t.day} ${month_str} ${t.year} ${t.hour:02d}:${t.minute:02d}:${t.second:02d} GMT' + return http_header_string +} + // mceil returns the least integer value greater than or equal to x. fn mceil(x f64) f64 { if x > 0 { diff --git a/vlib/time/time_format_test.v b/vlib/time/time_format_test.v index e92472c6bac2d8..a939f4601c1a11 100644 --- a/vlib/time/time_format_test.v +++ b/vlib/time/time_format_test.v @@ -86,3 +86,7 @@ fn test_get_fmt_str() { fn test_utc_string() { assert 'Fri, 11 Jul 1980 21:23:42 UTC' == time_to_test.utc_string() } + +fn test_http_header_string() { + assert 'Fri, 11 Jul 1980 21:23:42 GMT' == time_to_test.http_header_string() +}