patterncppMinor
Checking whether a given timestamp is more than two weeks old
Viewed 0 times
oldthancheckingmoretwoweekstimestampwhethergiven
Problem
I am trying to see whether a given timestamp is more than two weeks old or not. If it is two weeks old, then print out "Hello" and if it is not more than two weeks old then print out "World".
I have a variable declared as
How do I check whether this timestamp is more than two weeks old and if it is two weeks old, then print out
This is the code I have which gives me the timestamp in milliseconds for two weeks back from the current date, which I then compare to see whether it is two weeks old or not.
I will be running this code on Ubuntu 12.04. What is a better way of doing this? Are there any improvements which I can do it here? In general, I need to check whether
Do I even need to use chrono time here? There are
I have a variable declared as
uint64_t which contains the timestamp in milliseconds.uint64_t data_timestamp;How do I check whether this timestamp is more than two weeks old and if it is two weeks old, then print out
"Hello" otherwise prints out "World". This is the code I have which gives me the timestamp in milliseconds for two weeks back from the current date, which I then compare to see whether it is two weeks old or not.
#include
#include
#include
int main()
{
uint64_t data_timestamp = 1406066507000;
const auto now = std::chrono::system_clock::now();
auto twoWeeks = std::chrono::hours(24 * 14);
auto lastTwoWeeks = now - twoWeeks;
auto millis = std::chrono::duration_cast\
(lastTwoWeeks.time_since_epoch()).count();
std::cout << "Time stamp in milliseconds since UNIX epoch start: "\
<< millis << std::endl;
if (data_timestamp < millis) {
std::cout << "Hello";
} else {
std::cout << "World";
}
return 0;
}I will be running this code on Ubuntu 12.04. What is a better way of doing this? Are there any improvements which I can do it here? In general, I need to check whether
data_timestamp is more than two weeks old or not. If it is two weeks old, then print "hello" otherwise prints "world."Do I even need to use chrono time here? There are
2 7 24 60 60 * 1000 = 1,209,600,000 milliseconds in two weeks, so a simple subtraction of that from the current should tell me what is more than 2 weeks old? right?Solution
-
You're already using `
You're already using `
(and just that), so you don't need . Including it anyway won't automatically allow compilation in pre-C++11 (if that was your intent here).
Also, since you're using uint64_t, you must include as well.
-
Each variable can be const since they're not being modified. This would still be a good idea for ensuring const-correctness, even though the entire scope is within main().
-
The \ characters for line-continuations are useless here; the wrapping still works.
-
Since you're not needing to flush the buffer, std::endl should be replaced with "\n". This may also give you a slight performance boost.
-
The "hello world" output can be shortened to a single-line ternary statement:
std::cout << ((data_timestamp < millis) ? "Hello" : "World");
Regarding this comparison: my tests give a signed/unsigned mismatch warning. You have already given data_timestamp an unsigned type, so auto must be deducing a signed type for millis based on the function's return type.
One way to fix this could be by changing data_timestamp to a signed type such as int64_t, though may offer more appropriate alternatives to help avoid uncertainty for this.
-
You don't need your own return 0 at the end of main()`. The C++ standard should guarantee that the compiler will do this return for you since success is implied.Code Snippets
std::cout << ((data_timestamp < millis) ? "Hello" : "World");Context
StackExchange Code Review Q#59285, answer score: 5
Revisions (0)
No revisions yet.