Solved

C++ compare timestamp in string format

Posted on 2012-03-13
4
710 Views
Last Modified: 2012-03-14
I have a string in this format YYYYMMDDhhmmss, how to compare this string with current time in C++?
0
Comment
Question by:ltswimmer
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37718295
Given that this very format allows numeric comparison (less, greater and equal all apply), you could simply format the current time as a string of the same format and use 'strcmp()' with its less, greater and equal return values, e.g.

char acCurTime[16];
char acOtherTime[16] = "YYYYMMDDhhmmss";

strftime(acCurTime,"%Y%m%d%H%M%S",localtime(time(NULL)));

int nRetVal = strcmp(acOtherTime,acCurTime);

Open in new window


Alternatively, you could parse the string into a 'struct tm' for comparison purposes, yet the caveat is that you cannot guarantee for some of the fields, i.e. 'tm_isdst' is indeterminable from such input data.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37718304
Sorry, it is possible to determine whether it is DST or not, but the means are a bit over the top.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37718400
Oh, another side note, if you use a 'std::string' instead, the 'compare()' member will return the same results as 'strcmp()'
0
 

Author Closing Comment

by:ltswimmer
ID: 37722505
Thanks to jkr for the simple solution.  I originally used difftime() for comparison which is more complex.

It works. You will need to add a parameter for strftime() because this function doesn't allow 3 parameters.

strftime(acCurTime,14, "%Y%m%d%H%M%S",localtime(time(NULL)));
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question