给定 time_t 或 struct timeval,我如何获得当天午夜 EST/EDT(本地时区)的 timeval 或 time_t?假设本地时区是 EST/EDT,假设 time_t 对应于 2010-11-30 08:00:00 EST/EDT,预期的答案是 time_t 对应于 2010-11-30 00:00:00 EST /美东时间
尝试 1(不正确:因为它不处理 DST,并假设 EST/EDT 总是比 UTC 晚 5 小时):
time_t RewindToMidnight ( const time_t temp_time_t_ )
{
return ( (5*3600) + ((( temp_time_t_ - 5*3600 )/86400 ) * 86400) );
}
尝试 2(不正确:因为它返回的 time_t 对应于 UTC 午夜时间,而不是 EST/EDT,本地时区):
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) struct tm、mktime 或 (ii) boost::local_date_time 也许?