go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/lucicfg/testdata/misc/time.star (about)

     1  load("@stdlib//internal/time.star", "time")
     2  
     3  sec = lambda v: time.duration(v * 1000)
     4  
     5  def test_duration_type():
     6      # Basic starlark stuff.
     7      assert.eq(type(sec(1)), "duration")
     8      assert.eq(str(sec(3600)), "1h0m0s")
     9  
    10      # Comparisons.
    11      assert.true(sec(123) == sec(123))
    12      assert.true(sec(123) != sec(456))
    13      assert.true(sec(123) < sec(456))
    14      assert.true(sec(456) > sec(123))
    15      assert.true(sec(123) <= sec(123))
    16      assert.true(sec(123) >= sec(123))
    17  
    18      # duration +- duration binary ops.
    19      assert.eq(sec(1) + sec(2), sec(3))
    20      assert.eq(sec(2) - sec(1), sec(1))
    21  
    22      # duration */ int binary op.
    23      assert.eq(sec(1) * 10, sec(10))
    24      assert.eq(10 * sec(1), sec(10))
    25      assert.eq(sec(10) / 10, sec(1))
    26      assert.eq(time.duration(7500) % sec(1), 500)
    27  
    28      # duration / duration binary op.
    29      assert.eq(sec(100) / sec(5), 20)
    30  
    31      # +- unary ops.
    32      assert.eq(+sec(1), sec(1))
    33      assert.eq(-sec(1), sec(-1))
    34  
    35  def test_type_mismatches():
    36      # Not compatible with ints, by design.
    37      assert.fails(lambda: sec(1) + 1, "unknown binary op")
    38      assert.fails(lambda: 1 + sec(1), "unknown binary op")
    39      assert.fails(lambda: sec(1) > 1, "not implemented")
    40      assert.fails(lambda: 1 > sec(1), "not implemented")
    41  
    42      # Can't multiply two durations.
    43      assert.fails(lambda: sec(1) * sec(2), "unknown binary op")
    44  
    45      # Can't divide ints by duration.
    46      assert.fails(lambda: 10 / sec(1), "unknown binary op")
    47  
    48  def test_helpers():
    49      assert.eq(60 * time.second + time.hour, 61 * time.minute)
    50      assert.eq(3600 * time.second / time.hour, 1)
    51      assert.eq(time.second * 3600 / time.hour, 1)
    52      assert.eq(time.truncate(2 * time.hour + time.minute, time.hour), 2 * time.hour)
    53  
    54  def test_epoch():
    55      assert.eq(time.epoch(time.short_date, "2020-01-13 16:21:33", "America/Denver"), 1578957693)
    56      assert.eq(time.epoch(time.short_date, "2020-01-09 19:00:00", "America/Denver"), 1578621600)
    57      assert.eq(time.epoch(time.short_date, "2020-01-10 18:21:25", "America/Denver"), 1578705685)
    58      assert.eq(time.epoch(time.short_date, "1969-12-31 17:00:00", "America/Denver"), 0)
    59      assert.eq(time.epoch(time.short_date, "1969-12-31 16:00:00", "America/Los_Angeles"), 0)
    60  
    61  def test_days_of_week():
    62      good_cases = {
    63          "": [],
    64          "   ": [],
    65          ",,,": [],
    66          "Mon": [1],
    67          "moN": [1],
    68          " Mon, ": [1],
    69          "Sun": [7],
    70          "Tue-Wed": [2, 3],
    71          " ,Tue - Wed ,": [2, 3],
    72          "Sun,Tue-Wed": [2, 3, 7],
    73          "Mon,Tue,Wed,Thu,Fri,Sat,Sun": [1, 2, 3, 4, 5, 6, 7],
    74          "Fri,Mon,Tue,Wed,Thu,Sat,Sun": [1, 2, 3, 4, 5, 6, 7],
    75          "Tue,Tue, Mon-Wed": [1, 2, 3],
    76      }
    77      for spec, out in good_cases.items():
    78          assert.eq(time.days_of_week(spec), out)
    79  
    80      bad_cases = {
    81          "huh": '"huh" is not a valid 3-char abbreviated day of the week',
    82          "mon-huh": '"huh" is not a valid 3-char abbreviated day of the week',
    83          "Sun-Mon": 'bad range "Sun-Mon" - "Sun" is later than "Mon"',
    84      }
    85      for spec, err in bad_cases.items():
    86          assert.fails(lambda: time.days_of_week(spec), err)
    87  
    88  test_duration_type()
    89  test_type_mismatches()
    90  test_helpers()
    91  test_epoch()
    92  test_days_of_week()