如何生成随机时间?

How to generate a random Time?

如何在 Time::UNIX_EPOCHTime.local 之间生成随机 Time

我需要它来打乱一个数组,然后按 Time 字段排序。

Time.unix(Random.new.rand(Time::UNIX_EPOCH.to_unix..Time.local.to_unix))

https://play.crystal-lang.org/#/r/8g06

或者,您可以使用固定时间(例如 UNIX_EPOCH)并添加随机时间跨度。

Time::UNIX_EPOCH.shift seconds: rand(1_000_000_000)