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

     1  package chrono_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/go-chrono/chrono"
     8  )
     9  
    10  func TestOffsetTime(t *testing.T) {
    11  	time := chrono.OffsetTimeOf(12, 30, 59, 12345678, 2, 30)
    12  
    13  	hour, min, sec := time.Clock()
    14  	if hour != 12 {
    15  		t.Errorf("time.Clock() hour = %d, want 12", hour)
    16  	}
    17  
    18  	if min != 30 {
    19  		t.Errorf("time.Clock() min = %d, want 30", min)
    20  	}
    21  
    22  	if sec != 59 {
    23  		t.Errorf("time.Clock() sec = %d, want 59", sec)
    24  	}
    25  
    26  	if nsec := time.Nanosecond(); nsec != 12345678 {
    27  		t.Errorf("time.Nanosecond() = %d, want 12345678", nsec)
    28  	}
    29  
    30  	expectedOffset := chrono.OffsetOf(2, 30)
    31  	if offset := time.Offset(); offset != expectedOffset {
    32  		t.Errorf("time.Offset() = %s, want %s", offset, expectedOffset)
    33  	}
    34  }
    35  
    36  func TestOfTimeOffset(t *testing.T) {
    37  	expectedLocalTime := chrono.LocalTimeOf(12, 30, 59, 12345678)
    38  	expectedOffset := chrono.OffsetOf(3, 30)
    39  
    40  	offsetTime := chrono.OfTimeOffset(expectedLocalTime, expectedOffset)
    41  	localTime := offsetTime.Local()
    42  	offset := offsetTime.Offset()
    43  
    44  	if localTime.Compare(expectedLocalTime) != 0 {
    45  		t.Errorf("LocalTime = %s, want %s", localTime, expectedLocalTime)
    46  	}
    47  	if offset != expectedOffset {
    48  		t.Errorf("Offset = %s, want %s", offset, expectedOffset)
    49  	}
    50  }
    51  
    52  func TestOffsetTime_String(t *testing.T) {
    53  	for _, tt := range []struct {
    54  		name     string
    55  		time     chrono.OffsetTime
    56  		expected string
    57  	}{
    58  		{"simple", chrono.OffsetTimeOf(9, 0, 0, 0, 2, 30), "09:00:00+02:30"},
    59  		{"nanoseconds", chrono.OffsetTimeOf(9, 0, 0, 12345678, 2, 30), "09:00:00.012345678+02:30"},
    60  	} {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			if output := tt.time.String(); output != tt.expected {
    63  				t.Errorf("LocalTime.String() = %s, want %s", output, tt.expected)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func TestOffsetTime_BusinessHour(t *testing.T) {
    70  	time := chrono.OffsetTimeOf(25, 0, 0, 0, 2, 30)
    71  
    72  	if hour := time.BusinessHour(); hour != 25 {
    73  		t.Errorf("time.Hour() = %d, want 25", hour)
    74  	}
    75  
    76  	if hour, _, _ := time.Clock(); hour != 1 {
    77  		t.Errorf("time.Hour() = %d, want 1", hour)
    78  	}
    79  }
    80  
    81  func TestOffsetTime_Sub(t *testing.T) {
    82  	for _, tt := range []struct {
    83  		t1   chrono.OffsetTime
    84  		t2   chrono.OffsetTime
    85  		diff chrono.Extent
    86  	}{
    87  		{chrono.OffsetTimeOf(12, 0, 0, 0, 0, 0), chrono.OffsetTimeOf(6, 0, 0, 0, 0, 0), 6 * chrono.Hour},
    88  		{chrono.OffsetTimeOf(12, 0, 0, 22, 0, 0), chrono.OffsetTimeOf(12, 0, 0, 40, 0, 0), -18 * chrono.Nanosecond},
    89  		{chrono.OffsetTimeOf(12, 0, 0, 0, 2, 30), chrono.OffsetTimeOf(6, 0, 0, 0, 1, 0), 4*chrono.Hour + 30*chrono.Minute},
    90  		{chrono.OffsetTimeOf(12, 0, 0, 0, -2, 30), chrono.OffsetTimeOf(6, 0, 0, 0, -1, 0), 7*chrono.Hour + 30*chrono.Minute},
    91  	} {
    92  		t.Run(fmt.Sprintf("%s - %s", tt.t1, tt.t2), func(t *testing.T) {
    93  			if d := tt.t1.Sub(tt.t2); d != tt.diff {
    94  				t.Errorf("t1.Sub(t2) = %v, want %v", d, tt.diff)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestOffsetTime_Add(t *testing.T) {
   101  	for _, tt := range []struct {
   102  		t        chrono.OffsetTime
   103  		e        chrono.Extent
   104  		expected chrono.OffsetTime
   105  	}{
   106  		{chrono.OffsetTimeOf(12, 0, 0, 0, 0, 0), 29 * chrono.Minute, chrono.OffsetTimeOf(12, 29, 0, 0, 0, 0)},
   107  		{chrono.OffsetTimeOf(14, 45, 0, 0, 0, 0), -22 * chrono.Minute, chrono.OffsetTimeOf(14, 23, 0, 0, 0, 0)},
   108  		{chrono.OffsetTimeOf(5, 0, 0, 0, 0, 0), -7 * chrono.Hour, chrono.OffsetTimeOf(22, 0, 0, 0, 0, 0)},
   109  		{chrono.OffsetTimeOf(5, 0, 0, 0, 0, 0), -31 * chrono.Hour, chrono.OffsetTimeOf(22, 0, 0, 0, 0, 0)},
   110  	} {
   111  		t.Run(fmt.Sprintf("%s + %v", tt.t, tt.e), func(t *testing.T) {
   112  			if ok := tt.t.CanAdd(tt.e); !ok {
   113  				t.Error("t.CanAdd(e) = false, want true")
   114  			}
   115  
   116  			if added := tt.t.Add(tt.e); added.Compare(tt.expected) != 0 {
   117  				t.Errorf("t.Add(e) = %s, want %s", added, tt.expected)
   118  			}
   119  		})
   120  	}
   121  
   122  	for _, tt := range []struct {
   123  		name string
   124  		t    chrono.OffsetTime
   125  		e    chrono.Extent
   126  	}{
   127  		{"invalid duration", chrono.OffsetTimeOf(0, 0, 0, 0, 0, 0), 200 * chrono.Hour},
   128  		{"invalid time", chrono.OffsetTimeOf(90, 0, 0, 0, 0, 0), 20 * chrono.Hour},
   129  	} {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			if ok := tt.t.CanAdd(tt.e); ok {
   132  				t.Error("t.CanAdd(e) = true, want false")
   133  			}
   134  
   135  			func() {
   136  				defer func() {
   137  					if r := recover(); r == nil {
   138  						t.Error("expecting panic that didn't occur")
   139  					}
   140  				}()
   141  
   142  				tt.t.Add(tt.e)
   143  			}()
   144  		})
   145  	}
   146  }
   147  
   148  func TestOffsetTime_Split(t *testing.T) {
   149  	offsetTime := chrono.OffsetTimeOf(12, 30, 59, 12345678, 3, 30)
   150  	localTime := offsetTime.Local()
   151  	offset := offsetTime.Offset()
   152  
   153  	expectedLocalTime := chrono.LocalTimeOf(12, 30, 59, 12345678)
   154  	expectedOffset := chrono.OffsetOf(3, 30)
   155  	if localTime.Compare(expectedLocalTime) != 0 {
   156  		t.Errorf("LocalTime = %s, want %s", localTime, expectedLocalTime)
   157  	}
   158  	if offset != expectedOffset {
   159  		t.Errorf("Offset = %s, want %s", offset, expectedOffset)
   160  	}
   161  }
   162  
   163  func TestOffsetTime_UTC(t *testing.T) {
   164  	offsetTime := chrono.OffsetTimeOf(12, 30, 59, 12345678, 3, 30)
   165  	utc := offsetTime.UTC()
   166  	localTime := utc.Local()
   167  	offset := utc.Offset()
   168  
   169  	expectedLocalTime := chrono.LocalTimeOf(9, 0, 59, 12345678)
   170  	if localTime.Compare(expectedLocalTime) != 0 {
   171  		t.Errorf("time.UTC().Split() time = %s, want %s", localTime, expectedLocalTime)
   172  	}
   173  
   174  	if offset != chrono.UTC {
   175  		t.Errorf("time.UTC().Split() offset = %s, want %s", localTime, chrono.UTC)
   176  	}
   177  }
   178  
   179  func TestOffsetTime_In(t *testing.T) {
   180  	originalTime := chrono.OffsetTimeOf(12, 30, 0, 0, 3, 30)
   181  
   182  	for _, tt := range []struct {
   183  		name     string
   184  		offset   chrono.Offset
   185  		expected chrono.LocalTime
   186  	}{
   187  		{"UTC", chrono.UTC, chrono.LocalTimeOf(9, 0, 0, 0)},
   188  		{"positive", chrono.OffsetOf(5, 30), chrono.LocalTimeOf(14, 30, 0, 0)},
   189  		{"negative", chrono.OffsetOf(-5, 30), chrono.LocalTimeOf(3, 30, 0, 0)},
   190  	} {
   191  		t.Run(tt.name, func(t *testing.T) {
   192  			inOffset := originalTime.In(tt.offset)
   193  			localTime := inOffset.Local()
   194  			offset := inOffset.Offset()
   195  
   196  			if localTime.Compare(tt.expected) != 0 {
   197  				t.Errorf("time.In(%s).Split() time = %s, want %s", tt.offset, localTime, tt.expected)
   198  			}
   199  
   200  			if offset != tt.offset {
   201  				t.Errorf("time.In(%s).Split() offset = %s, want %s", tt.offset, localTime, tt.offset)
   202  			}
   203  		})
   204  	}
   205  }