Given time_t or struct timeval, how can I get the timeval or time_t of midnight EST / EDT (local time zone) on this day? Since under the assumption that the local time zone is EST / EDT, taking into account time_t, corresponding, for example, 2010-11-30 08:00:00 EST / EDT, the expected answer is time_t, which corresponds to 2010-11-30 00: 00:00 EST / EDT
Attempt 1 (incorrect: since it does not handle DST and assumes that EST / EDT is always 5 hours behind UTC):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
return ( (5*3600) + ((( temp_time_t_ - 5*3600 )/86400 ) * 86400) );
}
Attempt 2 (Incorrect: since it returns a time_t value that corresponds to when it was midnight UTC, not EST / EDT, local time zone):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
boost::posix_time::ptime temp_ptime_ = boost::posix_time::from_time_t ( temp_time_t_ );
boost::gregorian::date temp_date_ = temp_ptime_.date();
boost::posix_time::ptime temp_ptime_midnight_ ( temp_date_,
boost::posix_time::time_duration ( 0, 0, 0 ) );
return to_time_t ( temp_ptime_midnight_ );
}
time_t to_time_t ( const boost::posix_time::ptime & temp_ptime_ )
{
boost::posix_time::ptime temp_epoch_ptime_(boost::gregorian::date(1970,1,1));
boost::posix_time::time_duration::sec_type temp_sec_type_ = ( temp_ptime_ - temp_epoch_ptime_ ).total_seconds();
return time_t ( temp_sec_type_ );
}
I believe there should be a solution that includes (i) struct tm, mktime or (ii) boost :: local_date_time maybe?