# Converting Astropy.time.Time to datetime: Leapseconds

**URL:** <https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637>\
**Category:** Astropy\
**Created:** [March 31, 2023, 5:09pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637 "2023-03-31T17:09:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathewjowens](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/mathewjowens/32/363_2.png) [@mathewjowens](https://community.openastronomy.org/u/mathewjowens)\
**Post date:** [March 31, 2023, 5:09pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/1 "2023-03-31T17:09:54Z")

</div>

As Astropy.time.Time supports leap seconds and datetime does not, attempting to convert (Time.datetime) throws a value error:

ValueError: Time (array(2016, dtype=int32), array(12, dtype=int32), array(31, dtype=int32), array(23), array(59), array(60), array(993683)) is within a leap second but datetime does not support leap seconds

I can see why this should be the default behaviour. But is there any optional flag to get Time to ignore this and just convert to closest datetime? I couldn’t find anything in the docs.

---

<div class="post-metadata">

**Author:** ![taldcroft](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/taldcroft/32/66_2.png) [@taldcroft](https://community.openastronomy.org/u/taldcroft)\
**Post date:** [March 31, 2023, 6:01pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/2 "2023-03-31T18:01:06Z")

</div>

There is no such flag to ignore that exception. One option would be using the `ymdhms` format to do your own adjustments to ensure all the date values are legal for `datetime` and then initialize from your cleaned arrays.

---

<div class="post-metadata">

**Author:** ![mathewjowens](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/mathewjowens/32/363_2.png) [@mathewjowens](https://community.openastronomy.org/u/mathewjowens)\
**Post date:** [March 31, 2023, 6:15pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/3 "2023-03-31T18:15:07Z")

</div>

Yeah, ok, thanks. Obviously I can do a workaround.

But it kind of makes the time.datetime method somewhat redundant if you always need to check it’s not going throw a valueerror and crash your code. If there’s no option to ignore, would a warning not be better?

---

<div class="post-metadata">

**Author:** ![taldcroft](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/taldcroft/32/66_2.png) [@taldcroft](https://community.openastronomy.org/u/taldcroft)\
**Post date:** [March 31, 2023, 9:46pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/4 "2023-03-31T21:46:01Z")

</div>

That’s not a bad idea. Maybe just clip the seconds at \< 60 and warn?

---

<div class="post-metadata">

**Author:** ![mathewjowens](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/mathewjowens/32/363_2.png) [@mathewjowens](https://community.openastronomy.org/u/mathewjowens)\
**Post date:** [April 1, 2023, 7:40am UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/5 "2023-04-01T07:40:53Z")

</div>

Sounds like a good compromise to me! I can’t think of a better way of doing it.

---

<div class="post-metadata">

**Author:** ![taldcroft](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/taldcroft/32/66_2.png) [@taldcroft](https://community.openastronomy.org/u/taldcroft)\
**Post date:** [April 1, 2023, 1:33pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/6 "2023-04-01T13:33:33Z")

</div>

See [Add strict arg to Time.to\_datetime() for leap second handling by taldcroft · Pull Request #14606 · astropy/astropy · GitHub](https://github.com/astropy/astropy/pull/14606).

---

<div class="post-metadata">

**Author:** ![mathewjowens](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/mathewjowens/32/363_2.png) [@mathewjowens](https://community.openastronomy.org/u/mathewjowens)\
**Post date:** [April 1, 2023, 6:39pm UTC](https://community.openastronomy.org/t/converting-astropy-time-time-to-datetime-leapseconds/637/7 "2023-04-01T18:39:15Z")

</div>

Perfect. Thanks. A nice and tidy solution.
