Overload date() for time_t. : Time « Development « C++






Overload date() for time_t.

Overload date() for time_t.
 


#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;

class date {
  int day, month, year;
public:
  date(char *str);
  date(int m, int d, int y) {
    day = d;
    month = m;
    year = y;
  }

  date(time_t t);
  void show() {
    cout << month << '/' << day << '/';
    cout << year << '\n';
  }
};
 
date::date(char *str)
{
  sscanf(str, "%d%*c%d%*c%d", &month, &day, &year);
}

date::date(time_t t)
{
  struct tm *p;

  p = localtime(&t); // convert to broken down time
  day = p->tm_mday;
  month = p->tm_mon;
  year = p->tm_year;
}

int main()
{

  date sdate("12/31/06");    // construct date object using string


  date idate(12, 31, 04);    // construct date object using integers

  date tdate(time(NULL));

  sdate.show();
  idate.show();
  tdate.show();
  
  return 0;
}

           
         
  








Related examples in the same category

1.Use system timeUse system time
2.Using ctime() to get the current time
3.Output a time value with ctime