I am getting the beginning of Epoch, where I should get current time:
date.hpp:
#ifndef DATE_HPP
#define DATE_HPP
#include <time.h>
#include <iostream>
#include <sstream>
class Date
{
std::stringstream format;
struct tm *date_tm;
time_t date;
public:
Date() : date(time(NULL)), date_tm(localtime(&date)) {}
Date(std::istream &in);
Date(std::string str);
const std::string getDate();
};
#endif //DATE_HPP
date.cpp:
#include "date.hpp"
#include <iostream>
#include <sstream>
#include <iomanip>
Date::Date(std::istream &in)
{
std::cout << "enter date [mm/dd/yy]: ";
format.basic_ios::rdbuf(in.rdbuf());
format >> std::get_time(date_tm, "%m/%d/%y");
}
Date::Date(std::string str)
{
format << str;
format >> std::get_time(date_tm, "%m/%d/%y");
}
const std::string Date::getDate()
{
format << std::put_time(date_tm, "%m/%d/%y");
return format.str();
}
main.cpp:
#include "date.hpp"
#include <iostream>
int main()
{
Date now;
std::cout << now.getDate() << std::endl;
}
When I run it ./a.out
, I get 01/01/70
. Well obviously, I would expect current time, since the time(NULL)
which is used in localtime(now)
, should contain seconds from Epoch till now. So what could went wrong?