Skip to content

Commit

Permalink
x.encoding.asn1: fix time creation to also accommodate negative timez…
Browse files Browse the repository at this point in the history
…one offsets (#22861)
  • Loading branch information
kimshrier authored Nov 16, 2024
1 parent 8300a06 commit 3009373
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vlib/x/encoding/asn1/time.v
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn UtcTime.from_time(t time.Time) !UtcTime {
s := utime.custom_format(default_utctime_format) // 20241113060446+0
// Its rather a hack, not efieient as should be.
// TODO: make it better
str := s.split('+')
str := s.split_any('+-')
val := str[0] + 'Z'
utc := UtcTime.new(val)!
return utc
Expand Down Expand Up @@ -231,7 +231,7 @@ pub fn GeneralizedTime.from_time(t time.Time) !GeneralizedTime {
u := t.local_to_utc()
s := u.custom_format(default_genztime_format)
// adds support directly from time.Time
src := s.split('+')
src := s.split_any('+-')
val := src[0] + 'Z'
gt := GeneralizedTime.new(val)!

Expand Down
25 changes: 25 additions & 0 deletions vlib/x/encoding/asn1/time_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// that can be found in the LICENSE file.
module asn1

import os
import time

fn test_serialize_utctime_basic() ! {
Expand Down Expand Up @@ -42,6 +43,18 @@ fn test_create_utctime_from_std_time() ! {
assert tinp_back.value == inp
}

fn test_create_utctime_from_std_time_with_negative_offset() ! {
tz := os.getenv('TZ')
os.setenv('TZ', 'utc 1', true)

defer {
os.setenv('TZ', tz, true)
}

now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50)
UtcTime.from_time(now)!
}

fn test_serialize_utctime_error_without_z() ! {
// this input does not contains zulu 'Z' part
inp := '191215190210'
Expand Down Expand Up @@ -115,3 +128,15 @@ fn test_create_generalizedtime_from_std_time() ! {

assert g_utc_back.value == s
}

fn test_create_generalizedtime_from_std_time_with_negative_offset() ! {
tz := os.getenv('TZ')
os.setenv('TZ', 'utc 1', true)

defer {
os.setenv('TZ', tz, true)
}

now := time.new(year: 2024, month: 11, day: 13, hour: 17, minute: 45, second: 50)
GeneralizedTime.from_time(now)!
}

0 comments on commit 3009373

Please sign in to comment.