Want more features on Pastebin? Sign Up, it's FREE!
Guest

How to add one day to a time obtained from time()

By: a guest on Feb 28th, 2012  |  syntax: None  |  size: 2.35 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <time.h>
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6.   time_t base = 1142085600;
  7.   for(int i = 0; i < 4; ++i) {
  8.     time_t time = base + i * 24 * 60 * 60;
  9.     std::cout << ctime(&time);
  10.   }
  11.   return 0;
  12.        
  13. Sat Mar 11 08:00:00 2006
  14. Sun Mar 12 09:00:00 2006
  15. Mon Mar 13 09:00:00 2006
  16. Tue Mar 14 09:00:00 2006
  17.        
  18. int main()
  19. {
  20.   time_t base = 1142085600;
  21.   for(int i = 0; i < 4; ++i) {
  22.     struct tm* tm = localtime(&base);
  23.     tm->tm_mday += i;
  24.     std::cout << asctime(tm);
  25.  }
  26.  return 0;
  27. }
  28.        
  29. Sat Mar 11 08:00:00 2006
  30. Sat Mar 12 08:00:00 2006
  31. Sat Mar 13 08:00:00 2006
  32. Sat Mar 14 08:00:00 2006
  33.        
  34. #include <time.h>
  35. #include <iostream>
  36.  
  37. int main()
  38. {
  39.   time_t base = 1142085600;
  40.   for(int i = 0; i < 4; ++i) {
  41.     struct tm* tm = localtime(&base);
  42.     tm->tm_mday += i;
  43.     time_t time = mktime(tm);
  44.     std::cout << asctime(tm);
  45.  }
  46.  return 0;
  47. }
  48.        
  49. Sat Mar 11 08:00:00 2006
  50. Sun Mar 12 09:00:00 2006
  51. Mon Mar 13 09:00:00 2006
  52. Tue Mar 14 09:00:00 2006
  53.        
  54. std::cout << ctime(&time);
  55.        
  56. Sat Mar 11 08:00:00 2006
  57. Sun Mar 12 08:00:00 2006
  58. Mon Mar 13 08:00:00 2006
  59. Tue Mar 14 08:00:00 2006
  60.        
  61. int main()
  62. {
  63.   time_t base = 1142085600;
  64.   for(int i = 0; i < 4; ++i) {
  65.     struct tm* tm = localtime(&base);
  66.     tm->tm_mday += i;
  67.     time_t next = mktime(tm);
  68.     std::cout << ctime(&next);
  69.  }
  70.  return 0;
  71. }
  72.        
  73. int main()
  74. {
  75.   time_t base = 1142085600;
  76.   for(int i = 0; i < 4; ++i) {
  77.     struct tm* tm = localtime(&base);
  78.     tm->tm_mday += i;
  79.     tm->tm_isdst = -1;        // don't know if DST is in effect, please determine
  80.                               // this for me
  81.     time_t next = mktime(tm);
  82.     std::cout << ctime(&next);
  83.  }
  84.  return 0;
  85. }
  86.        
  87. int main()
  88. {
  89.     // 28 March 2009 05:00:00 GMT ( local - 08:00 (MSK) )
  90.     time_t base = 1238216400;
  91.  
  92.     std::time_t start_date_t = base;
  93.     std::time_t end_date_t = base;
  94.  
  95.     std::tm start_date = *std::localtime(&start_date_t);
  96.     std::tm end_date = *std::localtime(&end_date_t);
  97.  
  98.     end_date.tm_mday += 1;
  99. //    end_date.tm_isdst = -1;
  100.  
  101.     std::time_t b = mktime(&start_date);
  102.     std::time_t e = mktime(&end_date);
  103.  
  104.     std::string start_date_str(ctime(&b));
  105.     std::string stop_date_str(ctime(&e));
  106.  
  107.     cout << " begin (MSK) (DST is not active): " << start_date_str;
  108.     cout << " end   (MSD) (DST is active):     " << stop_date_str;
  109. }
  110.        
  111. begin (MSK) (DST is not active): Sat Mar 28 08:00:00 2009
  112. end   (MSD) (DST is active):     Sun Mar 29 09:00:00 2009
clone this paste RAW Paste Data