go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/time.star (about) 1 # Tests of time module. 2 3 load('assert.star', 'assert') 4 load('time.star', 'time') 5 6 assert.true(time.now() > time.parse_time("2021-03-20T00:00:00Z")) 7 8 assert.eq(time.parse_time("2020-06-26T17:38:36Z"), time.from_timestamp(1593193116)) 9 assert.eq(time.parse_time("2020-06-26T17:38:36.123456789", format="2006-01-02T15:04:05.999999999"), time.from_timestamp(1593193116, 123456789)) 10 11 assert.eq(time.parse_time("1970-01-01T00:00:00Z").unix, 0) 12 assert.eq(time.parse_time("1970-01-01T00:00:00Z").unix_nano, 0) 13 14 t = time.parse_time("2000-01-02T03:04:05Z") 15 assert.eq(t.year, 2000) 16 assert.eq(t.in_location("US/Eastern"), time.parse_time("2000-01-01T22:04:05-05:00")) 17 assert.eq(t.in_location("US/Eastern").format("3 04 PM"), "10 04 PM") 18 19 assert.eq(t - t, time.parse_duration("0s")) 20 21 d1s = time.parse_duration("1s") 22 assert.eq(d1s - d1s, time.parse_duration("0")) 23 assert.eq(d1s + d1s, time.parse_duration("2s")) 24 assert.eq(d1s * 5, time.parse_duration("5s")) 25 assert.eq(time.parse_duration("0s") + time.parse_duration("3m35s"), time.parse_duration("3m35s")) 26 27 d10h = time.parse_duration("10h") 28 # duration attributes 29 assert.eq(10.0, d10h.hours) 30 assert.eq(10*60.0, d10h.minutes) 31 assert.eq(10*60*60.0, d10h.seconds) 32 assert.eq(10*60*60*1000, d10h.milliseconds) 33 assert.eq(10*60*60*1000000, d10h.microseconds) 34 assert.eq(10*60*60*1000000000, d10h.nanoseconds) 35 36 # duration type 37 assert.eq("time.duration", type(d10h)) 38 # duration str 39 assert.eq("10h0m0s", str(d10h)) 40 # duration hash 41 durations = { 42 d10h: "10h", 43 d1s: "10s", 44 } 45 assert.eq("10h", durations[d10h]) 46 assert.eq("10s", durations[d1s]) 47 48 # duration == duration 49 # duration != duration 50 assert.eq(time.parse_duration("1h"), time.parse_duration("1h")) 51 assert.ne(time.parse_duration("1h"), time.parse_duration("1m")) 52 # duration < duration 53 assert.lt(time.parse_duration("1m"), time.parse_duration("1h")) 54 assert.true(not time.parse_duration("1h") < time.parse_duration("1h")) 55 assert.true(not time.parse_duration("1h") < time.parse_duration("1m")) 56 # duration <= duration 57 assert.true(time.parse_duration("1m") <= time.parse_duration("1h")) 58 assert.true(time.parse_duration("1h") <= time.parse_duration("1h")) 59 assert.true(not time.parse_duration("1h") <= time.parse_duration("1m")) 60 # duration > duration 61 assert.true(not time.parse_duration("1m") > time.parse_duration("1h")) 62 assert.true(not time.parse_duration("1h") > time.parse_duration("1h")) 63 assert.true(time.parse_duration("1h") > time.parse_duration("1m")) 64 # duration >= duration 65 assert.true(not time.parse_duration("1m") >= time.parse_duration("1h")) 66 assert.true(time.parse_duration("1h") >= time.parse_duration("1h")) 67 assert.true(time.parse_duration("1h") >= time.parse_duration("1m")) 68 69 refTime = time.parse_time("2011-04-22T13:33:48Z") 70 tenHoursAfterRefTime = time.parse_time("2011-04-22T23:33:48Z") 71 72 # duration + duration = duration 73 assert.eq(d10h + d1s, time.parse_duration("10h01s")) 74 # duration + time = time 75 assert.eq(d10h + refTime, tenHoursAfterRefTime) 76 # duration - duration = duration 77 assert.eq(d10h - d1s, time.parse_duration("9h59m59s")) 78 # duration / duration = float 79 assert.eq(d10h / time.parse_duration("16m"), 37.5) 80 assert.fails(lambda: d10h / time.parse_duration("0"), "division by zero") 81 # duration / int = duration 82 assert.eq(d10h / 20, time.parse_duration("30m")) 83 assert.fails(lambda: d10h / 0, "division by zero") 84 # int / duration = error 85 assert.fails(lambda: 20 / d10h, "unsupported operation") 86 # duration / float = duration 87 assert.eq(d10h / 37.5, time.parse_duration("16m")) 88 assert.fails(lambda: d10h / 0.0, "division by zero") 89 # duration // duration = int 90 assert.eq(d10h // time.parse_duration("16m"), 37) 91 assert.fails(lambda: d10h // time.parse_duration("0"), "division by zero") 92 # duration * int = duration 93 assert.eq(d1s * 1000, time.parse_duration("16m40s")) 94 # int * duration = duration 95 assert.eq(1000 * d1s, time.parse_duration("16m40s")) 96 97 # is_valid_timezone(location) 98 assert.true(time.is_valid_timezone("UTC")) 99 assert.true(time.is_valid_timezone("US/Eastern")) 100 assert.true(not time.is_valid_timezone("UKN")) 101 102 # time(year=..., month=..., day=..., hour=..., minute=..., second=..., nanosecond=..., location=...) 103 assert.fails(lambda: time.time(2009, 6, 12, 12, 6, 10, 99, "US/Eastern"), "unexpected positional argument") 104 t1 = time.time(year=2009, month=6, day=12, hour=12, minute=6, second=10, nanosecond=99, location="US/Eastern") 105 assert.eq(t1, time.parse_time("2009-06-12T12:06:10.000000099", format="2006-01-02T15:04:05.999999999", location="US/Eastern")) 106 assert.eq(time.time(year=2012, month=12, day=31), time.parse_time("2012-12-31T00:00:00Z")) 107 assert.eq(time.time(year=2009, month=6, day=12, hour=12, minute=6, second=10, nanosecond=99, location="UTC"), time.time(year=2009, month=6, day=12, hour=12, minute=6, second=10, nanosecond=99)) 108 109 # time attributes 110 assert.eq(2009, t1.year) 111 assert.eq(6, t1.month) 112 assert.eq(12, t1.day) 113 assert.eq(12, t1.hour) 114 assert.eq(6, t1.minute) 115 assert.eq(10, t1.second) 116 assert.eq(99, t1.nanosecond) 117 assert.eq(1244822770, t1.unix) 118 assert.eq(1244822770000000099, t1.unix_nano) 119 assert.true(not time.parse_time("0001-01-01T00:00:00Z")) 120 assert.true(time.parse_time("2022-01-01T00:00:00Z")) 121 122 # time type 123 assert.eq("time.time", type(refTime)) 124 # duration str 125 assert.eq("2011-04-22 13:33:48 +0000 UTC", str(refTime)) 126 # duration hash 127 times = { 128 refTime: "refTime", 129 t1: "t1", 130 } 131 assert.eq("refTime", times[refTime]) 132 assert.eq("t1", times[t1]) 133 134 oneSecondAfterRefTime = time.parse_time("2011-04-22T13:33:49Z") 135 oneYearAfterRefTime = time.parse_time("2012-04-22T13:33:48Z") 136 oneYearBeforeRefTime = time.parse_time("2010-04-22T13:33:48Z") 137 twoYearsBeforeRefTime = time.parse_time("2009-04-22T13:33:48Z") 138 tenHoursBeforeRefTime = time.parse_time("2011-04-22T03:33:48Z") 139 140 # time == time 141 # time != time 142 assert.eq(refTime, refTime) 143 assert.ne(refTime, oneSecondAfterRefTime) 144 # time < time 145 assert.lt(oneYearBeforeRefTime, refTime) 146 assert.true(not oneYearBeforeRefTime < oneYearBeforeRefTime) 147 assert.true(not oneYearBeforeRefTime < twoYearsBeforeRefTime) 148 # time <= time 149 assert.true(oneYearBeforeRefTime <= refTime) 150 assert.true(oneYearBeforeRefTime <= oneYearBeforeRefTime) 151 assert.true(not oneYearBeforeRefTime <= twoYearsBeforeRefTime) 152 # time > time 153 assert.true(oneYearAfterRefTime > refTime) 154 assert.true(not refTime > refTime) 155 assert.true(not oneYearBeforeRefTime > refTime) 156 # time >= time 157 assert.true(oneYearAfterRefTime >= refTime) 158 assert.true(refTime >= refTime) 159 assert.true(not oneYearBeforeRefTime >= refTime) 160 # time + duration = time 161 assert.eq(refTime + d10h, tenHoursAfterRefTime) 162 # time - duration = time 163 assert.eq(refTime - d10h, tenHoursBeforeRefTime) 164 # time - time = duration 165 assert.eq(refTime - tenHoursBeforeRefTime, d10h)