Reputation: 111
I want to compare datetime in C. I googled but I didn't get any proper solution. I am having datetime in string format i.e date1 = "2014-02-13 12:22:21" and date2 = "2014-02-10 12:22:21". Now, I want comparison b/w date1 and date2. Please suggest me proper solution.
Upvotes: 0
Views: 300
Reputation: 18864
If you don't run this on the critical path, Then just use strncmp()
. Else, if speed is important, parse it with strptime()
+mktime()
into UTC uint64_t microseconds/milliseconds since epoch and compare those. I would also make sure timezones are taken into account consistently across the code base.
To clarify the point raised in the comment - whether to prefer string comparison to timestamp comparison (paying parsing overhead) is solely determined by the usage pattern and can be benchmarked in a simple synthetic test.
Upvotes: 0
Reputation: 91207
Just use strcmp
. It works because with this particular date/time format, the lexicographical order is the same as the chronological order.
Upvotes: 4