Skip to content

Commit

Permalink
time: add a .http_header_string() method on Time (#20861)
Browse files Browse the repository at this point in the history
  • Loading branch information
davlgd committed Feb 18, 2024
1 parent 2190b60 commit 38cf923
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/time/format.v
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions vlib/time/time_format_test.v
Expand Up @@ -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()
}

0 comments on commit 38cf923

Please sign in to comment.