Solved

C++ compare timestamp in string format

Posted on 2012-03-13
4
763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

617 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