jm

Odin for scripts: small packages and a runner, on core: only
Log | Files | Refs | README

timefmt_test.odin (2096B)


      1 package timefmt
      2 
      3 import "core:testing"
      4 import "core:time"
      5 
      6 @(test)
      7 format_directives :: proc(t: ^testing.T) {
      8 	// 2026-09-23T10:41:02.250Z is a Wednesday, day 266 of the year.
      9 	tm := time.unix(1790160062, 250_000_000)
     10 	testing.expect_value(t, iso(tm, context.temp_allocator), "2026-09-23T10:41:02Z")
     11 	testing.expect_value(t, stamp(tm, context.temp_allocator), "20260923-104102")
     12 	testing.expect_value(t, date(tm, context.temp_allocator), "2026-09-23")
     13 	testing.expect_value(t, format(tm, "%a %A %b %B %e %j %y %f %I%p %u %w %s %z %Z %%", context.temp_allocator),
     14 		"Wed Wednesday Sep September 23 266 26 250 10AM 3 3 1790160062 +0000 UTC %")
     15 }
     16 
     17 @(test)
     18 parse_roundtrip :: proc(t: ^testing.T) {
     19 	tm, ok := parse("2026-09-23 10:41:02", "%Y-%m-%d %H:%M:%S")
     20 	testing.expect(t, ok)
     21 	testing.expect_value(t, time.time_to_unix(tm), i64(1790160062))
     22 
     23 	tm, ok = parse("23 Sep 2026 3:05 pm", "%d %b %Y %I:%M %p")
     24 	testing.expect(t, ok)
     25 	testing.expect_value(t, format(tm, "%H:%M", context.temp_allocator), "15:05")
     26 
     27 	tm, ok = parse("1790160062", "%s")
     28 	testing.expect(t, ok)
     29 	testing.expect_value(t, time.time_to_unix(tm), i64(1790160062))
     30 
     31 	_, ok = parse("2026-13-01", "%Y-%m-%d")
     32 	testing.expect(t, !ok, "month 13 must fail")
     33 	_, ok = parse("2026-09-23 extra", "%Y-%m-%d")
     34 	testing.expect(t, !ok, "trailing text must fail")
     35 }
     36 
     37 @(test)
     38 durations :: proc(t: ^testing.T) {
     39 	testing.expect_value(t, duration(250 * time.Millisecond, context.temp_allocator), "250ms")
     40 	testing.expect_value(t, duration(4200 * time.Millisecond, context.temp_allocator), "4.2s")
     41 	testing.expect_value(t, duration(3 * time.Minute + 12 * time.Second, context.temp_allocator), "3m12s")
     42 	testing.expect_value(t, duration(time.Hour + 2 * time.Second, context.temp_allocator), "1h0m2s")
     43 	testing.expect_value(t, duration(51 * time.Hour, context.temp_allocator), "2d3h")
     44 	testing.expect_value(t, duration(-500 * time.Microsecond, context.temp_allocator), "-500µs")
     45 }
     46 
     47 @(test)
     48 local_has_zone :: proc(t: ^testing.T) {
     49 	s := local(time.now(), "%Z", context.temp_allocator)
     50 	testing.expect(t, len(s) > 0, "zone name must not be empty")
     51 }