github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/offset_test.go (about)

     1  package chrono_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-chrono/chrono"
     7  )
     8  
     9  func TestOffsetOf(t *testing.T) {
    10  	for _, tt := range []struct {
    11  		name     string
    12  		hours    int
    13  		mins     int
    14  		expected chrono.Extent
    15  	}{
    16  		{"UTC", 0, 0, 0},
    17  		{"positive hours", 3, 0, 3 * chrono.Hour},
    18  		{"negative hours", -3, 0, -3 * chrono.Hour},
    19  		{"positive minutes", 0, 30, 30 * chrono.Minute},
    20  		{"negative minutes", 0, -30, -30 * chrono.Minute},
    21  		{"positive hours and minutes", 3, 30, 3*chrono.Hour + 30*chrono.Minute},
    22  		{"negative hours and minutes", -3, -30, -3*chrono.Hour - 30*chrono.Minute},
    23  		{"positive hours and negative minutes", 3, -30, 3*chrono.Hour + 30*chrono.Minute},
    24  		{"negative hours and positive minutes", -3, 30, -3*chrono.Hour - 30*chrono.Minute},
    25  	} {
    26  		t.Run(tt.name, func(t *testing.T) {
    27  			offset := chrono.OffsetOf(tt.hours, tt.mins)
    28  			if offset != chrono.Offset(tt.expected) {
    29  				t.Errorf("OffsetOf(%d, %d) = %s, want %s", tt.hours, tt.mins, offset, tt.expected)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestOffset_String(t *testing.T) {
    36  	for _, tt := range []struct {
    37  		name     string
    38  		value    chrono.Extent
    39  		expected string
    40  	}{
    41  		{"UTC", 0, "Z"},
    42  		{"UTC truncated", 30 * chrono.Second, "Z"},
    43  		{"positive hours", 3 * chrono.Hour, "+03:00"},
    44  		{"negative hours", -3 * chrono.Hour, "-03:00"},
    45  		{"positive hours and minutes", 3*chrono.Hour + 30*chrono.Minute, "+03:30"},
    46  		{"negative hours and minutes", -(3*chrono.Hour + 30*chrono.Minute), "-03:30"},
    47  		{"positive hours and minutes truncated", 3*chrono.Hour + 30*chrono.Minute + 59*chrono.Second, "+03:30"},
    48  		{"negative hours and minutes truncated", -(3*chrono.Hour + 30*chrono.Minute + 59*chrono.Second), "-03:30"},
    49  	} {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			offset := chrono.Offset(tt.value)
    52  			if out := offset.String(); out != tt.expected {
    53  				t.Errorf("stringified offset = %s, want %s", out, tt.expected)
    54  			}
    55  		})
    56  	}
    57  }