Jan 052009
 

After doing quite a bit of C++ recently, I thought I would post my method for getting the current system time in milliseconds in C++ for both Mac OS X and Windows. The Mac version might translate to other Unix platforms, but you’ll have to check the docs or man pages.

Mac OS X

This actually returns a struct that has microsecond precision.

Windows

This code gives the milliseconds within the last minute. If you want milliseconds since epoch or some other fixed point in time it will require a bit more math on the SYSTEMTIME struct.

  20 Responses to “Getting the current system time in milliseconds with C++”

  1. […] here:  Getting the current system time in milliseconds with C++ Share and […]

  2. Can someone send a .cpp file, it won’t compile it says:
    gcc /Users/Alexandre/Desktop/timeInMS.cpp -o ~/Desktop/Time
    /usr/bin/ld: Undefined symbols:
    ___gxx_personality_v0
    collect2: ld returned 1 exit status

    Thank you!

  3. Hi

    I tried the program that you have written. But when I print ‘millis’ I get a negative value. How can I avoid it?

  4. Shishir,

    You might want to use printf( “%ld”, millis ) to avoid the negative values.

    If you’re having trouble compiling this, try “struct timeval” instead of “timeval”.

  5. After a long search i have find the good code for Windows VC++.
    #include “windows.h”
    SYSTEMTIME time;
    GetSystemTime(&time);
    WORD millis = (time.wSeconds * 1000) + time.wMilliseconds;
    is working fine for getting the time in millisecond..

    Good code

  6. […] ref:here Share this:TwitterFacebookLike this:LikeBe the first to like this. This entry was posted in Uncategorized by zahraee. Bookmark the permalink. […]

  7. Hi,
    I am student of BSCS.. I am trying to learn c and c++ nowadays. I am a beginner so i can’t understand much about this time get code from windows. If anyone please please help … How can i code it in struct.. And if anyone could provide me some material of c++ or c to learn it best. I will be very thankful to him/her…. thankyou . umar

  8. Just one correction: It’s not “wSeconds” it’s “wSecond”.

    In this line:
    WORD millis = (time.wSeconds * 1000) + time.wMilliseconds;

    Replace with:
    WORD millis = (time.wSecond * 1000) + time.wMilliseconds;

  9. anked for keeping the retro trend around. The classic Wayfarer model has been a brand staple for the better half of the century, and the iconic shape is mimicked by a plethora of other labels. Though a lot of people say that fashion has a bad taste during these times, there are exceptions. Basically, the Ray Ban Wayfarer transcends that hopeless vividness of that style era. No matter its association, you cannot put an inherently gorgeous piece of.
    ray ban arista

  10. Hi my name is Sandra and I just wanted to drop you a quick note here instead of calling you. I discovered your Getting the current system time in milliseconds with C – Invert Your Mind » Invert Your Mind page and noticed you could have a lot more visitors. I have found that the key to running a popular website is making sure the visitors you are getting are interested in your subject matter. There is a company that you can get targeted visitors from and they let you try the service for free for 7 days. I managed to get over 300 targeted visitors to day to my site. Visit them here: http://ittsy.com/it/3x

  11. If some one desires expert view on the topic of blogging
    afterward i recommend him/her to pay a visit this web site, Keep
    up the fastidious job.

  12. I got this website from my pal who informed me regarding this website and at
    the moment this time I am visiting this site and reading
    very informative content here.

  13. Does your website have a contact page? I’m having a tough time locating it but, I’d like to
    shoot you an email. I’ve got some suggestions for your blog you
    might be interested in hearing. Either way, great blog and I look forward to seeing it
    develop over time.

  14. Do you like carmines shoes? Find jordan 6 infrareds here: cheap retro sneakers

  15. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your
    blog and I look forward to your new updates.

    Feel free to surf to my web-site page labeled affiliate

  16. I have to thank you for the efforts you have
    put in writing this site. I really hope to see the
    same high-grade content by you later on as well. In fact, your creative writing abilities has encouraged me to get my own blog
    now ;)

    My webpage – famous hockey

  17. I’ve been testing out a buddy’s Samsung Geear Match as I’m trying into gettting a
    ftness tracker.

    my site Lon

  18. Hi,I read your new stuff named “Getting the current system time in milliseconds with C – Invert Your Mind » Invert Your Mind” regularly.Your writing style is awesome, keep doing what you’re doing! And you can look our website about ????? ???????? ???? 2017 http://www.matb3aa.com/album/%D9%85%D9%87%D8%B1%D8%AC%D8%A7%D9%86%D8%A7%D8%AA-2017.

  19. Thanks for some other informative website. The place elpse could I get that kind of information written in such
    an ideal manner? I have a venture that I am just now oprrating on,
    and I have been on thee look out for such information.

    Take a look at my web blog: perfect escort date in Nottingham,
    Lavonne,

  20. magnificent post, very informative. I’m wondering
    why the other specialists of this sector do not notice this.
    You should continue your writing. I’m confident, you have a great readers’ base already!

    My page sweet escorts in Nottingham (Jude)

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">