 : encode-time ( timestamp -- string )
     timestamp>millis 10 encode-bits ;
 
-: same-msec? ( -- ? )
-    nano-count 1000 /i dup \ same-msec? get =
-    [ drop t ] [ \ same-msec? set f ] if ;
+: same-millisecond? ( -- ? )
+    nano-count 1,000,000 /i dup \ same-millisecond? get =
+    [ drop t ] [ \ same-millisecond? set f ] if ;
 
 : pack-bits ( seq -- seq' )
     5 swap [ first ] [ rest ] bi [


TR: (normalize-ulid) "ILO" "110" ; inline

: (ulid) ( same-msec? -- ulid )
    [
        last-time-string get last-random-bits get 1 +
        dup 80-bits > [ ulid-overflow ] when

PRIVATE>

: ulid ( -- ulid )
    same-millisecond? (ulid) ;

ERROR: ulid>bytes-bad-length n ;
M: ulid>bytes-bad-length summary drop "Invalid ULID length" ;
