type conversion int -> duration and int (milli/nano) -> timestamp

19 views
Skip to first unread message

Dan Kortschak

unread,
Aug 23, 2023, 6:34:10 PM8/23/23
to 'Tristan Swadell' via CEL Go Discussion Forum
I have a use case where we are being handed timestamps in unix
milliseconds. This needs to be converted to a timestamp for evaluation.

The current approach is to timestamp(int(time_val)/1000). Ideally I
would like to retain the millisecond component, but this appears to
require something like 

timestamp(0) + duration(string(int(time_val))+"ms")

which is less than ideal.

Am I missing conversion options here? (before I go ahead an add helpers
for this).

Dan

Tristan Swadell

unread,
Aug 23, 2023, 7:12:12 PM8/23/23
to Dan Kortschak, 'Tristan Swadell' via CEL Go Discussion Forum
Hi Dan,

You're not missing anything; however, it might be good to create a helper for timestamp(<number>, <precision>) where the precision is a string that's similar to what we support for duration precision values, e.g. `d, h, s, ms, us, ns`. Does that make sense? I suspect that would be useful for working with date ranges across a variety of contexts and granularities.

-Tristan

--
You received this message because you are subscribed to the Google Groups "CEL Go Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cel-go-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cel-go-discuss/b436c9b07959b6a1265b40dfc8802e0f52437049.camel%40kortschak.io.

Tristan Swadell

unread,
Aug 23, 2023, 7:12:48 PM8/23/23
to Dan Kortschak, 'Tristan Swadell' via CEL Go Discussion Forum
Or something like `timestampWithPrecision(<int>, <precision>)`. You get the idea.

Cheers,

-Tristan

Dan Kortschak

unread,
Aug 23, 2023, 9:10:37 PM8/23/23
to cel-go-...@googlegroups.com
On Wed, 2023-08-23 at 16:11 -0700, 'Tristan Swadell' via CEL Go
Discussion Forum wrote:
> Hi Dan,
>
> You're not missing anything; however, it might be good to create a
> helper for timestamp(<number>, <precision>) where the precision is a
> string that's similar to what we support for duration precision
> values, e.g. `d, h, s, ms, us, ns`. Does that make sense? I suspect
> that would be useful for working with date ranges across a variety of
> contexts and granularities.
>
> -Tristan
>

Thanks, Tristan.

Dan

Reply all
Reply to author
Forward
0 new messages