Solved

C++ compare timestamp in string format

Posted on 2012-03-13
4
674 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now