I have string below.
std::string _valid = "2014-03-28 16:45:59";
int y1, M1, d1, h1, m1, s1;
sscanf_s(_valid.c_str(), "%d-%d-%d %d:%d:%d", &y1, &M1, &d1, &h1, &m1, &s1);
Now, I can get year, month, day and so on...
Now, I have to determine whether that time is earlier than current datetime.
time_t now = time(NULL);
But, time_t always returns me the time in unix timestamp.
How can I compare the date?
Set to struct tm , then generate time_t by function mktime . You can compare the value of two time_t directly.
You can also use SYSTEMTIME and FILETIME from windows.h. Set to SYSTEMTIME then generate FILETIME by function SystemTimeToFileTime . Compare two FILETIME by function CompareFileTime . To compute the distance between two FILETIME, I'm writing the follow ugly code:
__int64 duration = ((*(__int64 *)&ft2) - (*(__int64 *)&ft1));
Note that SYSTEMTIME has higher resolution than struct tm.
I think you are looking for the mktime function. The details are here http://linux.die.net/man/3/mktime , but in a nutshell...
#include <time.h>
time_t mktime(struct tm *tm);
where the struct tm is defined in time.h as...
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.