time: add a .http_header_string() method on Time (#20861)

This commit is contained in:
David Legrand 2024-02-18 13:45:37 +01:00 committed by GitHub
parent 2190b604a1
commit 38cf923537
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View File

@ -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 {

View File

@ -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()
}