adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
794 stars 703 forks source link

Constexpr DateTime/TimeSpan constructors #223

Open maxbla opened 3 years ago

maxbla commented 3 years ago

Feature Request: constexpr constructors for DateTime / TimeSpan

This would allow me to generate helpful error messages at compile time for DateTimes/TimeSpans that are invalid in a certain context using static_assert. Previously I have used run-time checks with Serial.println, but this method only catches mistakes if developers are looking at their serial console.

I think this is as simple as marking the constructors constexpr, but I might be missing something

maxbla commented 3 years ago

After some investigation, most TimeSpan constructors can be made constexpr, but most DateTime constructors can't without C++14 constexpr features (Arduino currently uses C++11 by default).

maxbla commented 3 years ago

If Arduino ever makes C++14 the default, This stack overflow question has some code that might be worth borrowing from.