I just learned that a leap second will be added at the end of this month (june 2015). I'm a little bit affraid about that, because I manage my own time class. I'm not sure it's robust enough.
I created a class Instant
working with a unsigned long long int
member variable ms
which is the number of milliseconds from 01/01/1600
at 0:00:00
.
When I want to get the current time, I call this function :
void Instant::setCurrentTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
this->ms = NBR_DAYS_TO_EPOCH; // 135140
this->ms *= NBR_SECONDS_IN_DAY; // 86400
this->ms += tv.tv_sec;
this->ms *= NBR_MILLISECONDS_IN_SECOND; // 1000
this->ms += tv.tv_usec/1000;
}
My first question is : How leap seconds affect this number ? Is it already taken into account in the tv.tv_sec
value ? How ?
My second question is about the conversion to human-readable format. I don't post it here because my function is quite huge (I can post it if required), but the idea is the following :
- I first compute the number of seconds in the current day, and then the number of minutes and hours ;
- I compute the number of days before the current day ;
- Then I compute the number of cycles of 400 years (periodic process), the number of cycles of 100 years in the current cycle of 400 years, and the number of cycles of 4 years in the current cycle of 4 years ;
- From that, I deduce the year (leap or not), the month, the day, and the day of the week ;
- Then I check if the summer time is activated, and I update the values according to the zone (europe, usa before 2007, usa from 2007) ;
- All of that without taking into account the 25 leap seconds added from 1970.
For the moment, my class is not able to say "we are 30/06/2015
at 23:59:60
", because my number of seconds is limited to 59
. But also because I don't know how to manage that. what could be a good way ?