Advertisement
ikseek

Date manipulation

Jan 20th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4.  
  5. int main(int argc, const char * argv[])
  6. {
  7.     time_t currtime_t = time(NULL); // текущая дата
  8.     struct tm the_time = *localtime(&currtime_t); // текущая дата в формате tm
  9.     char buffer[80];
  10.    
  11.     strftime (buffer,80,"Now it's %c.",&the_time); // дата в виде строки
  12.     std::cout<<buffer<<std::endl;
  13.  
  14.    
  15.     the_time.tm_mday += 60; // добавить 60 дней
  16.     mktime(&the_time); // нормализация даты
  17.     strftime (buffer,80,"Now it's %c.",&the_time);
  18.     std::cout<<buffer<<std::endl;
  19.  
  20.    
  21.     the_time.tm_mday -= 365; // отнять 365 дней
  22.     mktime(&the_time); // нормализация даты
  23.     strftime (buffer,80,"Now it's %c.",&the_time);
  24.     std::cout<<buffer<<std::endl;
  25.  
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement