I need to make a function which takes a timestamp (the time in milliseconds, type long
) and transforms it into readable time (Y-M-D H:M:S); however, after that, I have to overload the function such that if the function doesn't get a parameter it will return the current date.
I know how to make the function to transform from the given long parameter to readable time, but I don't know how to overload the function.
#include <iostream>
#include <cstdio>
#include <ctime>
#include <string>
using namespace std;
string timeToString(long timestamp)
{
const time_t rawtime = (const time_t)timestamp;
struct tm * dt;
char timestr[30];
char buffer [30];
dt = localtime(&rawtime);
strftime(timestr, sizeof(timestr), "%Y-%m-%d %X", dt);
sprintf(buffer,"%s", timestr);
string stdBuffer(buffer);
return stdBuffer;
}
int main()
{
cout << timeToString(1538123990) << "\n";
}