0

In rust, how can I convert a u32 Unix Epoch based value into a std::time::SystemTime ?

In my case, the u32 value is from a gzip header modified time.



Converse question: How can I convert SystemTime::now() to an integer type?.

JamesThomasMoon
  • 6,169
  • 7
  • 37
  • 63

1 Answers1

4

Add a Duration to SystemTime::UNIX_EPOCH:

let time = SystemTime::UNIX_EPOCH + Duration::from_secs(value);
Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • Just to make sure. Neither `SystemTime` nor UNIX timestamps account for leap seconds, so this works out? – Caesar Jul 25 '22 at 02:54
  • Yes, they're both based on UTC, meaning they do integrate leap seconds. They wouldn't account for leap seconds if they were based off of TAI. – Masklinn Jul 25 '22 at 06:08
  • @Caesar "Leap seconds" aren't a thing in UNIX timestamps. UNIX timestamps is just a count of the number of seconds past the UNIX epoch. – Colonel Thirty Two Jul 25 '22 at 17:03