-2

I need a function or library to get the current system time in milliseconds from 1/1/1970 (C++) ?

I need to get a unsigned double number containing the milliseconds from 1/1/1970.

The operating system is Windows.

ADDITIONAL INFO:

Need to synchronize the server application and the client. The client is the master. The client tells the server when he needs to do something. After that the server runs on its own. I am sending the time stamp from the client that contains the current system time down to the millisecond level. The Server once received this message must do the same thing. He must get the time stamp and the difference will be the number of milliseconds to adjust its timeline. This is a matter of how to synchronize to process to do some related work at the same time.

DO NOT WANT TO USE BOOST IF IT IS POSSIBLE !

Patrik
  • 1,286
  • 1
  • 31
  • 64
  • Uuuuuh that'll be a huuuge value! – πάντα ῥεῖ Nov 26 '13 at 21:59
  • There is no such thing as an unsigned double – JustSid Nov 26 '13 at 21:59
  • OK. I do not care about the unsigned double but it must be contained completely. – Patrik Nov 26 '13 at 22:00
  • mattp, I spent 6 hours trying any possible combination. Neither Google can find me something useful. The funny part of the story is that C#, Python and Java can get this value but in C++ I can't find this the solution. – Patrik Nov 26 '13 at 22:02
  • Have you tried searching this forum? There is almost exact duplicate for this question. –  Nov 26 '13 at 22:02
  • Yes I do and nothing works. If you know something more then me please point me to that question. Thanks. – Patrik Nov 26 '13 at 22:03
  • If you paste your exact question into Google you'll get the answer to your question. –  Nov 26 '13 at 22:04
  • @Patrik OK, may be an XY problem: For what exact reason do you need it? You could of course just use the current date and time with millisecond resolution and calculate the milliseconds from the epoch as (huge) `double` value ([`std::chrono`](http://en.cppreference.com/w/cpp/chrono) might come in handy to do this). – πάντα ῥεῖ Nov 26 '13 at 22:07
  • @g-makulik, actually, even with milliseconds you'd be able to roughly get to the year 584944387 AD with 64 bit unsigned integers. Huge is relative. – KillianDS Nov 26 '13 at 22:07
  • Ok. Let me explain. I have two programs that must run in sync. One is the client in Python and the other is the server in C++. The server needs to sync itself to the client. I need the absolute time from the client and the absolute time from the server. The difference in milliseconds at two different point of times gives me the number of milliseconds to correct the server. – Patrik Nov 26 '13 at 22:10
  • @Patrik, please start reading up on NTP or PTP or something similar and don't reinvent the wheel, certainly when you need this kind of precision. High-precision time synchronization is not trivial and good solutions already exists, just pick one and use that. – KillianDS Nov 26 '13 at 22:12
  • @KillianDS 'Huge' means milliseconds will be **irrelevant** for result comparisons as `double` representations! – πάντα ῥεῖ Nov 26 '13 at 22:13
  • 1
    @g-makulik I doubt that, the current number is 1385504099.2318995 (in seconds), it hardly comes into the regions where the milliseconds (the .231) get dropped in simple calculations like +/-. But let us not go on about an irrelevant side-discussion and compromise that it's up for interpretation. – KillianDS Nov 26 '13 at 22:17
  • @KillianDS OK! You're right, didn't check that. – πάντα ῥεῖ Nov 26 '13 at 22:20
  • In my solution NTP or PTP are not acceptable. It is not a matter of exact clock. It is a matter of running two process synchronously. – Patrik Nov 26 '13 at 22:21
  • boost has "date_time" module. You won't be able to get the number you want in single call, and storing it as double is probably a bad idea. You should be able to get number of DAYS since 1970, get current time with millisecond precision, and then combine both into single value. – SigTerm Nov 26 '13 at 22:26
  • 2
    Also, when you say "nothing works", it is normally a good idea to tell what exactly you tried. – SigTerm Nov 26 '13 at 22:28
  • As far as I can tell, [solution from this question](http://stackoverflow.com/questions/6734375/c-boost-get-current-time-in-milliseconds) should work for you. – SigTerm Nov 26 '13 at 22:30
  • @SigTerm - When I said nothing works it means that I tried any solution that can be found on stackoverflow. The next time please read the complete conversation. – Patrik Nov 27 '13 at 00:18
  • @Patrik: I did read entire conversation, however because you didn't list even one solution you tried, it automatically made me think that you haven't tried every single one of them. Or haven't found every single one of them. Now, please explain why solution I linked didn't work for you. – SigTerm Nov 27 '13 at 07:16

5 Answers5

3

You can use GetSystemTimeAsFileTime to get the current time as a FILETIME. Create a SYSTEMTIME representing your desired epoch (1/1/1970) and call SystemTimeToFileTime to convert it to a FILETIME. Subtract the two FILETIMEs and scale to your desired accuracy (from 100ns units to 1ms units).

This will give you the current UTC time. If you need the local time, you'll need to convert to local time using e.g. SystemTimeToTzSpecificLocalTime.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • I achieved my goal with FILETIME time functions. There were some difficulties with the daylight saving but for the rest worked perfectly. – Patrik Nov 27 '13 at 12:01
2

It's not mandated by the standard, but I understand that all major implementations of std::chrono::system_clock use 1970-1-1 as the epoch and generally have at least millisecond resolution. So in practice you can simply do:

#include <chrono>
#include <iostream>

int main() {
    auto now = std::chrono::system_clock::now();
    std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() << '\n';
}
bames53
  • 86,085
  • 15
  • 179
  • 244
2

you can use boost for geting your time duration. boost/date_time/gregorian/gregorian.hpp provides a function date_duration() which can calculate dates from the year 1400 up to today.

Smii
  • 38
  • 5
1

Check out this std::clock() it should have the functionality you need to get the millisecond count.

Another thing to look for is "epoch" time.

Smeghead Sev
  • 418
  • 4
  • 14
0

In Windows use GetSystemTime. Here's the link to MSDN

You'll need to sum up the components, but that's trivial :)

egur
  • 7,830
  • 2
  • 27
  • 47
  • 1
    Summing up it is not enough. Not sure how to calculate the Year in seconds. There are leap years too to take into account. 365/366 days ... – Patrik Nov 26 '13 at 22:17