Converting localtime seconds into float

This is a discussion on Converting localtime seconds into float within the C Programming forums, part of the General Programming Boards category; I am trying to make a program that reads the seconds from the system time and prints them on the ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    Converting localtime seconds into float

    I am trying to make a program that reads the seconds from the system time and prints them on the screen. However, the seconds always seem to be in integer format and I wish to use it in floating point format instead, i.e. to display milliseconds. Is this possible?

    Here is the current program I am using. It's similar to the one found on the time_t page at Wikipedia.

    Code:
    #include <stdio.h>
    #include <time.h>
    main()
    {
        time_t     now;
        struct tm  ts;
        char       buf[80];
        time(&now);
        ts = *localtime(&now);
        strftime(buf, sizeof(buf), "%S", &ts);
        printf("%s\n", buf);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Why not just access the tm_sec member variable of ts ?

    Also, I don't think you're going to get any meaningful milliseconds out of this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe you're dreaming big time with millseconds.

    In any case, they would be printed (and printed over), far faster than anyone could see what they were.

    I'd suggest using 1/10'ths of a second as your shortest timer interval.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21