github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/local_date_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 TestLocalDateTimeOf(t *testing.T) {
    11  	for _, tt := range []struct {
    12  		datetime     chrono.LocalDateTime
    13  		expectedDate chrono.LocalDate
    14  		expectedTime chrono.LocalTime
    15  	}{
    16  		{
    17  			datetime:     chrono.LocalDateTime{},
    18  			expectedDate: chrono.LocalDateOf(1970, chrono.January, 1),
    19  			expectedTime: chrono.LocalTimeOf(0, 0, 0, 0),
    20  		},
    21  		{
    22  			datetime:     chrono.LocalDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 100000000),
    23  			expectedDate: chrono.LocalDateOf(2020, chrono.March, 18),
    24  			expectedTime: chrono.LocalTimeOf(12, 30, 0, 100000000),
    25  		},
    26  	} {
    27  		t.Run(tt.datetime.String(), func(t *testing.T) {
    28  			date, time := tt.datetime.Split()
    29  			if date != tt.expectedDate {
    30  				t.Errorf("datetime.Split() date = %s, want %s", date, tt.expectedDate)
    31  			}
    32  
    33  			if time.Compare(tt.expectedTime) != 0 {
    34  				t.Errorf("datetime.Split() time = %s, want %s", time, tt.expectedTime)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestOfLocalDateTime(t *testing.T) {
    41  	datetime := chrono.OfLocalDateTime(
    42  		chrono.LocalDateOf(2020, chrono.March, 18),
    43  		chrono.LocalTimeOf(12, 30, 0, 100000000),
    44  	)
    45  
    46  	date, time := datetime.Split()
    47  	if expected := chrono.LocalDateOf(2020, chrono.March, 18); date != expected {
    48  		t.Errorf("datetime.Split() date = %s, want %s", date, expected)
    49  	}
    50  
    51  	if expected := chrono.LocalTimeOf(12, 30, 0, 100000000); time != expected {
    52  		t.Errorf("datetime.Split() time = %s, want %s", time, expected)
    53  	}
    54  }
    55  
    56  func TestLocalDateTime_Compare(t *testing.T) {
    57  	for _, tt := range []struct {
    58  		name     string
    59  		d        chrono.LocalDateTime
    60  		d2       chrono.LocalDateTime
    61  		expected int
    62  	}{
    63  		{"earlier", chrono.LocalDateTimeOf(2020, chrono.March, 18, 11, 0, 0, 0), chrono.LocalDateTimeOf(2020, chrono.March, 18, 12, 0, 0, 0), -1},
    64  		{"later", chrono.LocalDateTimeOf(2020, chrono.March, 18, 13, 30, 0, 0), chrono.LocalDateTimeOf(2020, chrono.March, 18, 13, 29, 55, 0), 1},
    65  		{"equal", chrono.LocalDateTimeOf(2020, chrono.March, 18, 15, 0, 0, 1000), chrono.LocalDateTimeOf(2020, chrono.March, 18, 15, 0, 0, 1000), 0},
    66  	} {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			if v := tt.d.Compare(tt.d2); v != tt.expected {
    69  				t.Errorf("t.Compare(t2) = %d, want %d", v, tt.expected)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestLocalDateTime_Add(t *testing.T) {
    76  	t.Run("valid add", func(t *testing.T) {
    77  		datetime := chrono.LocalDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 0)
    78  		duration := chrono.DurationOf(48*chrono.Hour + 1000*chrono.Nanosecond)
    79  
    80  		if !datetime.CanAdd(duration) {
    81  			t.Errorf("datetime = %s, datetime.CanAdd(%s) = false, want true", datetime, duration)
    82  		}
    83  
    84  		added := datetime.Add(duration)
    85  		expected := chrono.LocalDateTimeOf(2020, chrono.March, 20, 12, 30, 0, 1000)
    86  
    87  		if added.Compare(expected) != 0 {
    88  			t.Errorf("datetime = %s, datetime.Add(%s) = %s, want %s", datetime, duration, added, expected)
    89  		}
    90  	})
    91  
    92  	t.Run("invalid add to low", func(t *testing.T) {
    93  		datetime := chrono.MinLocalDateTime()
    94  		duration := chrono.DurationOf(-1 * chrono.Nanosecond)
    95  
    96  		if datetime.CanAdd(duration) {
    97  			t.Errorf("datetime = %s, datetime.CanAdd(%s) = true, want false", datetime, duration)
    98  		}
    99  
   100  		func() {
   101  			defer func() {
   102  				if r := recover(); r == nil {
   103  					t.Error("expecting panic that didn't occur")
   104  				}
   105  			}()
   106  
   107  			datetime.Add(duration)
   108  		}()
   109  	})
   110  
   111  	t.Run("invalid add to high", func(t *testing.T) {
   112  		datetime := chrono.MaxLocalDateTime()
   113  		duration := chrono.DurationOf(1 * chrono.Nanosecond)
   114  
   115  		if datetime.CanAdd(duration) {
   116  			t.Errorf("datetime = %s, datetime.CanAdd(%s) = true, want false", datetime, duration)
   117  		}
   118  
   119  		func() {
   120  			defer func() {
   121  				if r := recover(); r == nil {
   122  					t.Error("expecting panic that didn't occur")
   123  				}
   124  			}()
   125  
   126  			datetime.Add(duration)
   127  		}()
   128  	})
   129  }
   130  
   131  func TestLocalDateTime_AddDate(t *testing.T) {
   132  	for _, tt := range []struct {
   133  		name      string
   134  		datetime  chrono.LocalDateTime
   135  		addYears  int
   136  		addMonths int
   137  		addDays   int
   138  		expected  chrono.LocalDateTime
   139  	}{
   140  		{"nothing", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, 0, 0, chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0)},
   141  		{"add years", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 105, 0, 0, chrono.LocalDateTimeOf(2125, chrono.March, 18, 0, 0, 0, 0)},
   142  		{"sub years", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), -280, 0, 0, chrono.LocalDateTimeOf(1740, chrono.March, 18, 0, 0, 0, 0)},
   143  		{"add months", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, 6, 0, chrono.LocalDateTimeOf(2020, chrono.September, 18, 0, 0, 0, 0)},
   144  		{"sub months", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, -2, 0, chrono.LocalDateTimeOf(2020, chrono.January, 18, 0, 0, 0, 0)},
   145  		{"add days", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, 0, 8, chrono.LocalDateTimeOf(2020, chrono.March, 26, 0, 0, 0, 0)},
   146  		{"sub days", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, 0, -15, chrono.LocalDateTimeOf(2020, chrono.March, 3, 0, 0, 0, 0)},
   147  		{"time package example", chrono.LocalDateTimeOf(2011, chrono.January, 1, 0, 0, 0, 0), -1, 2, 3, chrono.LocalDateTimeOf(2010, chrono.March, 4, 0, 0, 0, 0)},
   148  		{"normalized time package example", chrono.LocalDateTimeOf(2011, chrono.October, 31, 0, 0, 0, 0), 0, 1, 0, chrono.LocalDateTimeOf(2011, chrono.December, 1, 0, 0, 0, 0)},
   149  		{"wrap around day", chrono.LocalDateTimeOf(2020, chrono.March, 18, 0, 0, 0, 0), 0, 0, 20, chrono.LocalDateTimeOf(2020, chrono.April, 7, 0, 0, 0, 0)},
   150  	} {
   151  		t.Run(tt.name, func(t *testing.T) {
   152  			if ok := tt.datetime.CanAddDate(tt.addYears, tt.addMonths, tt.addDays); !ok {
   153  				t.Errorf("date = %s, date.CanAddDate(%d, %d, %d) = false, want true", tt.datetime, tt.addYears, tt.addMonths, tt.addDays)
   154  			}
   155  
   156  			if date := tt.datetime.AddDate(tt.addYears, tt.addMonths, tt.addDays); date.Compare(tt.expected) != 0 {
   157  				t.Errorf("date = %s, date.AddDate(%d, %d, %d) = %s, want %s", tt.datetime, tt.addYears, tt.addMonths, tt.addDays, date, tt.expected)
   158  			}
   159  		})
   160  	}
   161  
   162  	for _, tt := range []struct {
   163  		name     string
   164  		datetime chrono.LocalDateTime
   165  		addDays  int
   166  	}{
   167  		{"underflow", chrono.MinLocalDateTime(), -1},
   168  		{"overflow", chrono.MaxLocalDateTime(), 1},
   169  	} {
   170  		t.Run(tt.name, func(t *testing.T) {
   171  			if ok := tt.datetime.CanAddDate(0, 0, tt.addDays); ok {
   172  				t.Errorf("date = %s, date.CanAddDate(0, 0, %d) = true, want false", tt.datetime, tt.addDays)
   173  			}
   174  
   175  			func() {
   176  				defer func() {
   177  					if r := recover(); r == nil {
   178  						t.Error("expecting panic that didn't occur")
   179  					}
   180  				}()
   181  
   182  				tt.datetime.AddDate(0, 0, tt.addDays)
   183  			}()
   184  		})
   185  	}
   186  }
   187  
   188  func TestLocalDateTime_Sub(t *testing.T) {
   189  	for _, tt := range []struct {
   190  		dt1  chrono.LocalDateTime
   191  		dt2  chrono.LocalDateTime
   192  		diff chrono.Duration
   193  	}{
   194  		{
   195  			chrono.LocalDateTimeOf(2020, chrono.January, 5, 12, 0, 0, 0),
   196  			chrono.LocalDateTimeOf(2020, chrono.January, 3, 6, 0, 0, 0),
   197  			chrono.DurationOf(54 * chrono.Hour),
   198  		},
   199  		{
   200  			chrono.LocalDateTimeOf(2020, chrono.January, 5, 12, 0, 0, 22),
   201  			chrono.LocalDateTimeOf(2020, chrono.January, 5, 12, 0, 0, 40),
   202  			chrono.DurationOf(-18 * chrono.Nanosecond),
   203  		},
   204  	} {
   205  		t.Run(fmt.Sprintf("%s - %s", tt.dt1, tt.dt2), func(t *testing.T) {
   206  			if d := tt.dt1.Sub(tt.dt2); d.Compare(tt.diff) != 0 {
   207  				t.Errorf("dt1.Sub(dt2) = %v, want %v", d, tt.diff)
   208  			}
   209  		})
   210  	}
   211  }
   212  
   213  func TestLocalDateTime_In(t *testing.T) {
   214  	dt := chrono.LocalDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 0)
   215  	output := dt.In(chrono.OffsetOf(2, 30))
   216  
   217  	expected := chrono.OffsetDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 0, 2, 30)
   218  	if output.Compare(expected) != 0 {
   219  		t.Errorf("dt.In = %s, want %s", output, expected)
   220  	}
   221  }
   222  
   223  func TestLocalDateTime_UTC(t *testing.T) {
   224  	dt := chrono.LocalDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 0)
   225  	output := dt.UTC()
   226  
   227  	expected := chrono.OffsetDateTimeOf(2020, chrono.March, 18, 12, 30, 0, 0, 0, 0)
   228  	if output.Compare(expected) != 0 {
   229  		t.Errorf("dt.UTC() = %s, want %s", output, expected)
   230  	}
   231  }