0

I need to calculate timestamp in milliseconds for two weeks old date from the current date.

As of now this is the way I am calculating current timestamp in milliseconds -

struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t current_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds

Now I am not sure how do I get the timestamp in milliseconds for two weeks back from the current date? I am interested in making a function which returns me the timestamp in milliseconds for two weeks back from the current date.

I recently started working with C++ so not sure what is the right way to check out a timestamp in milliseconds for two weeks old. I will be running this code on Ubuntu 12.04.

Update:-

Below is the code I have -

#include <ctime>
#include <chrono>
#include <iostream>

int main()
{
    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<std::chrono::milliseconds>\
                    (lastTwoWeeks.time_since_epoch()).count();
    std::cout << "Time stamp in milliseconds since UNIX epoch start: "\
              << millis << std::endl;

    return 0;
}
john
  • 11,311
  • 40
  • 131
  • 251

2 Answers2

1

With C++11, following may help:

#include <ctime>
#include <chrono>
#include <iostream>

int main()
{
    const auto now = std::chrono::system_clock::now();
    auto twoWeeks = std::chrono::hours(24 * 14);
    auto lastTwoWeeks = now - twoWeeks;

    // display time_point:
    std::time_t tt = std::chrono::system_clock::to_time_t(lastTwoWeeks);
    std::cout << "last Two Weeks is: " << ctime(&tt);

    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Do you know what happens if the time is changed in the previous two weeks? Does chronos 'fix' the timestamp captured 2 or more weeks ago? – 2785528 Aug 06 '14 at 02:13
  • @DOUGLASO.MOEN: I don't understand your question. `now` returns a fixed time point based on OS. `ctime` returns a readable local date. the rest is pure arithmetic and computation. – Jarod42 Aug 06 '14 at 02:31
  • Let me try again. A timestamp set 1.7 weeks ago will get a value, call it 27. If the wall clock changes, as in an automatic time update from its clock server, or dst starting or ending, does that time stamp (captured 1.7 weeks ago) change to a (not-27) value ? – 2785528 Aug 06 '14 at 03:39
  • @DOUGLASO.MOEN: `now` returns basically the number of seconds since epoch (`1970`), so as I understand your question, no, changing the date of the OS doesn't change the result of the previous calls. – Jarod42 Aug 06 '14 at 08:05
1

To expand on @Jarod42's answer, if you want to get the time stamp since the epoch start (1/1/1970) in milliseconds, append the following two lines to @Jarod42's code:

auto millis = std::chrono::duration_cast<std::chrono::milliseconds>\
                (lastTwoWeeks.time_since_epoch()).count();
std::cout << "Time stamp in milliseconds since UNIX epoch start: "\
          << millis << std::endl;
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Thanks for the extra tip. I have updated the question with the code I am going to use. Does that look right to you in which I am using your epoch suggestion as well? – john Aug 06 '14 at 01:16
  • @user2809564 if you want to compute how many milliseconds passed between 1/1/1970 and two weeks ago, then yes, the code is correct. – vsoftco Aug 06 '14 at 01:17
  • Thanks makes sense now. I guess I don't have C++11 portability as of now in my environment. Is there any other way to write this thing out? – john Aug 06 '14 at 21:11