# std::time::SystemTime misses a checked\_add function

**URL:** <https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785>\
**Category:** help\
**Created:** [October 30, 2018, 4:36am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785 "2018-10-30T04:36:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgeisler](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sgeisler/32/15189_2.png) [@sgeisler](https://users.rust-lang.org/u/sgeisler)\
**Post date:** [October 30, 2018, 4:36am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/1 "2018-10-30T04:36:52Z")

</div>

Hi,

I'm trying to convert an untrusted user-supplied UNIX timestamp to a `SystemTime`. The standard way to do this described in the documentation seems to be adding a `Duration` created from the timestamp to `SystemTime::UNIX_EPOCH`. The Problem with this is that there could be overflows that I can't handle well that way. A workaround would be to define a maximum for the timestamp, but since the `SystemTime` struct is opaque and makes no promises regarding it's range that will always just be a heuristic that works for most practical systems. So for me as a library author who has to work with untrusted user input this API isn't really usable without giving a bunch of caveats to my downstream users.

The obvious solution to this would be to add a `checked_add` function to `SystemTime`. Is such a proposal on it's way or was it rejected and I'm missing something?

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sfackler/32/132_2.png) [@sfackler](https://users.rust-lang.org/u/sfackler)\
**Post date:** [October 30, 2018, 5:18am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/2 "2018-10-30T05:18:53Z")

</div>

I don't think you're missing anything - it seems like a straightforward thing we should add!

---

<div class="post-metadata">

**Author:** ![sgeisler](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sgeisler/32/15189_2.png) [@sgeisler](https://users.rust-lang.org/u/sgeisler)\
**Post date:** [October 30, 2018, 5:36am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/3 "2018-10-30T05:36:56Z")

</div>

I don't know too much about the rust std-lib development process, would this need a RFC or just a PR?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cad97/32/5076_2.png) [@CAD97](https://users.rust-lang.org/u/CAD97)\
**Post date:** [October 30, 2018, 7:05am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/4 "2018-10-30T07:05:40Z")

</div>

For a relatively small libs change (especially one relatively straightforward), just make a PR explaining the addition and why it's desired. It can FCP inline there (and will for sure during stabilization or dropping if the PR is merged).

[My `ManuallyDrop::take` PR](https://github.com/rust-lang/rust/pull/55421) can serve as an example here. (You don't need to file a tracking issue immediately, just before it's merged.)

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [October 30, 2018, 7:10am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/5 "2018-10-30T07:10:47Z")

</div>

It won't even need a tracking issue. Implementing a stable trait for a stable type is also stable right away. (After riding the nightly-beta-stable release train, of course.)

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/sfackler/32/132_2.png) [@sfackler](https://users.rust-lang.org/u/sfackler)\
**Post date:** [October 30, 2018, 2:16pm UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/6 "2018-10-30T14:16:05Z")

</div>

This would be an inherent method, not a trait implementation, but yes, a direct PR is fine!

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [October 30, 2018, 4:04pm UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/7 "2018-10-30T16:04:49Z")

</div>

Oh, I got my wires crossed -- `CheckedAdd` is in num-traits.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:50am UTC](https://users.rust-lang.org/t/std-systemtime-misses-a-checked-add-function/21785/8 "2023-01-12T08:50:30Z")

</div>

Closed on January 12, 2023, 8:50am UTC


