Skip to content

Instantly share code, notes, and snippets.

@zxmarcos
Last active December 11, 2015 20:48
Show Gist options
  • Select an option

  • Save zxmarcos/4657280 to your computer and use it in GitHub Desktop.

Select an option

Save zxmarcos/4657280 to your computer and use it in GitHub Desktop.
// ===========================================================================
// Manipulação algébrica de datas
// Marcos Medeiros
// ===========================================================================
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstring>
using namespace std;
// Mostra uma data
void show_date(struct tm *curr_time)
{
cout << curr_time->tm_mday << '\\' << curr_time->tm_mon + 1 << '\\' << curr_time->tm_year + 1900 << " \\\\ "
<< curr_time->tm_hour << ':' << curr_time->tm_min << ':' << curr_time->tm_sec << endl;
}
int main()
{
time_t rawtime = time(NULL);
struct tm *curr_time;
curr_time = localtime(&rawtime);
show_date(curr_time);
cout << "Adicionando 1 dia" << endl;
// adiciona 1 dia, 60*60*24 segundos
rawtime += 86400;
curr_time = localtime(&rawtime);
show_date(curr_time);
int year, day, mon;
struct tm mytime;
cout << "Ano: "; cin >> year;
cout << "Mes: "; cin >> mon;
cout << "Dia: "; cin >> day;
memset(&mytime, 0, sizeof(mytime));
mytime.tm_year = year - 1900;
mytime.tm_mon = mon - 1;
mytime.tm_mday = day;
rawtime = mktime(&mytime);
show_date(&mytime);
rawtime += 86400;
curr_time = localtime(&rawtime);
show_date(curr_time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment