Question

Étant donné un time_t ou struct timeval, comment puis-je obtenir le TimeVal ou Time_T de Midnight EST / EDT (fuseau horaire local) ce jour-là? Comme en supposant que le fuseau horaire local est EST / EDT, étant donné un time_t correspondant à dire 2010-11-30 08:00:00 est / EDT, la réponse attendue est un Time_T qui correspond à 2010-11-30 00:00:00 est /EDT

Tentative 1 (Incorrect: car il ne gère pas DST et suppose que EST / EDT est toujours à 5 heures derrière UTC):

time_t RewindToMidnight ( const time_t temp_time_t_ )
{
  return ( (5*3600) + ((( temp_time_t_ - 5*3600 )/86400 ) * 86400) );
}

Tentative 2 (Incorrect: Puisqu'il renvoie un time_t qui correspond à quand il était minuit UTC et non EST / EDT, TimeSone local):

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_ );
}

Je pense qu'il devrait y avoir une solution impliquant (i) struct tm, mktime ou (ii) boost :: local_date_time peut-être?

Était-ce utile?

La solution 3

Solution actuelle:

time_t RewindToMidnight ( const time_t & temp_time_t_ )
{
    int temp_yyyymmdd_ = iso_date_from_time_t ( temp_time_t_ );
    return time_t_from_iso_date ( temp_yyyymmdd_ );
}

int iso_date_from_time_t ( const time_t & in_time_t_ ) 
{
     tm temp_this_tm_;

     { // the following to set local dst fields of struct tm ?
         time_t tvsec_ = time(NULL);
         localtime_r ( & tvsec_, & temp_this_tm_ ) ;
     }
     localtime_r ( & in_time_t, & temp_this_tm_ ) ;

     return ( ( ( ( 1900 + temp_this_tm_.tm_year ) * 100 + ( 1 + temp_this_tm_.tm_mon ) ) * 100 ) + temp_this_tm_.tm_mday ) ;
}

time_t time_t_from_iso_date ( const int & temp_yyyymmdd_ ) 
{ 

     boost::gregorian::date d1 ( (int)( temp_yyyymmdd_/10000), 
                                 (int)( ( temp_yyyymmdd_/100) % 100), 
                                 (int)( temp_yyyymmdd_ % 100) );

     std::tm this_tm_ = to_tm ( d1 );
     return ( mktime ( & this_tm_ ) ) ;
}

S'il vous plaît donnez votre avis.

Autres conseils

Comme Time_t est temps en quelques secondes depuis l'époque (00:00:00 UTC, 1er janvier 1970), il vous suffit de vous débarrasser des secondes de la journée. Il y a 86400 secondes par jour (les secondes de saut sont normalement ignorées), le résultat devrait donc être un multiple de 86400. Par conséquent:

time_t now = time();
time_t midnight = now / 86400 * 86400
time_t local_midnight(time_t x) {
  struct tm t;
  localtime_r(&x, &t);
  t.tm_sec = t.tm_min = t.tm_hour = 0;
  return mktime(&t);
}

J'ai utilisé localtime_r car il doit être disponible car vous l'avez également utilisé dans votre réponse.

Exemple:

int main() {
  time_t now = time(0);
  cout << "local: " << asctime(localtime(&now));
  cout << "UTC:   " << asctime(gmtime(&now));
  time_t midnight = local_midnight(now);
  cout << "\n       " << asctime(localtime(&midnight));
  return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top