G
Gary Wessle
Hi
the following code puts out
****************************************************************
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 10:50:00
1172581200
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 21:00:00
1172581200
****************************************************************
the same epoch for 2 different time_points, i.e
2007-2-28 21:00:00
2007-2-28 10:50:00
bot giving out
1172581200
here is the code, could some one please take a look and tell me whats
gone wrong.
thank you
****************************************************************
#include <iostream>
#include <string>
#include <ctime>
/* Returns local time epoch from a given time format
"2007-01-28 21:15:00" */
time_t asci2epoch(std::string s)
{
struct tm t_tm = { 0 };
// the time string
const char *t_str = s.c_str();
// get the values from the time string
sscanf(t_str, "%d-%d-%d %d:%d:%d", &(t_tm.tm_year), &(t_tm.tm_mon), &(t_tm.tm_mday), &(t_tm.tm_hour), &(t_tm.tm_min), &(t_tm.tm_sec));
// normalize ...
t_tm.tm_year -= 1900;
t_tm.tm_mon -= 1;
// the result :
time_t t = mktime(&t_tm);
t -= (1*60*60); // DST
return t;
}
int main()
{
std::cout << "Type 1 for epoch-to-text or 2 for text-to-epoch." << std::endl;
short opt;
std::cin >> opt;
if( opt == 1 )
{
time_t x;
std::cin >> x;
tm* p_tick = localtime( &x );
std::cout << "tick: " << asctime( p_tick ) << std::endl;
}
else if( opt == 2 )
{
std::string a;
std::cin >> a; // std::string a = "2007-2-5 8:45:00";
std::cout << asci2epoch(a) << std::endl;
}
else
std::cout << "wrong option." << std::endl;
}
the following code puts out
****************************************************************
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 10:50:00
1172581200
~/myProg/epoch2string $ ./proj
Type 1 for epoch-to-text or 2 for text-to-epoch.
2
2007-2-28 21:00:00
1172581200
****************************************************************
the same epoch for 2 different time_points, i.e
2007-2-28 21:00:00
2007-2-28 10:50:00
bot giving out
1172581200
here is the code, could some one please take a look and tell me whats
gone wrong.
thank you
****************************************************************
#include <iostream>
#include <string>
#include <ctime>
/* Returns local time epoch from a given time format
"2007-01-28 21:15:00" */
time_t asci2epoch(std::string s)
{
struct tm t_tm = { 0 };
// the time string
const char *t_str = s.c_str();
// get the values from the time string
sscanf(t_str, "%d-%d-%d %d:%d:%d", &(t_tm.tm_year), &(t_tm.tm_mon), &(t_tm.tm_mday), &(t_tm.tm_hour), &(t_tm.tm_min), &(t_tm.tm_sec));
// normalize ...
t_tm.tm_year -= 1900;
t_tm.tm_mon -= 1;
// the result :
time_t t = mktime(&t_tm);
t -= (1*60*60); // DST
return t;
}
int main()
{
std::cout << "Type 1 for epoch-to-text or 2 for text-to-epoch." << std::endl;
short opt;
std::cin >> opt;
if( opt == 1 )
{
time_t x;
std::cin >> x;
tm* p_tick = localtime( &x );
std::cout << "tick: " << asctime( p_tick ) << std::endl;
}
else if( opt == 2 )
{
std::string a;
std::cin >> a; // std::string a = "2007-2-5 8:45:00";
std::cout << asci2epoch(a) << std::endl;
}
else
std::cout << "wrong option." << std::endl;
}