gcc printf conversion specification for struct timeval

Discussion in 'C Programming' started by PawelCarqowski, Nov 29, 2007.

  1. Hallo group members,
    Do You know any conversion specification for this. I imagine:

    struct timeval time;
    printf("%T\n", time);

    regards,
    Pawel
     
    PawelCarqowski, Nov 29, 2007
    #1
    1. Advertising

  2. PawelCarqowski

    santosh Guest

    PawelCarqowski wrote:

    > Hallo group members,
    > Do You know any conversion specification for this. I imagine:
    >
    > struct timeval time;
    > printf("%T\n", time);


    struct timeval is a GNU C extension. There are no specific format
    specifiers for this but since the structure members are of type long
    int you can use the 'ld' format specifier for printing them.

    For furher GNU C related questions please ask in a GNU group or in a
    Linux group.
     
    santosh, Nov 29, 2007
    #2
    1. Advertising

  3. PawelCarqowski

    Ian Collins Guest

    santosh wrote:
    > PawelCarqowski wrote:
    >
    >> Hallo group members,
    >> Do You know any conversion specification for this. I imagine:
    >>
    >> struct timeval time;
    >> printf("%T\n", time);

    >
    > struct timeval is a GNU C extension.


    It's POSIX.

    --
    Ian Collins.
     
    Ian Collins, Nov 29, 2007
    #3
  4. PawelCarqowski

    santosh Guest

    Ian Collins wrote:

    > santosh wrote:
    >> PawelCarqowski wrote:
    >>
    >>> Hallo group members,
    >>> Do You know any conversion specification for this. I imagine:
    >>>
    >>> struct timeval time;
    >>> printf("%T\n", time);

    >>
    >> struct timeval is a GNU C extension.

    >
    > It's POSIX.


    You're right, thanks.
     
    santosh, Nov 29, 2007
    #4
  5. On Thu, 29 Nov 2007 10:43:25 +0100, PawelCarqowski wrote:

    > Hallo group members,
    > Do You know any conversion specification for this. I imagine:
    >
    > struct timeval time;
    > printf("%T\n", time);
    >
    > regards,
    > Pawel

    I use
    printf( "%ld.%.6ld\n", time.tv_sec, time.tv_usec);
    which produces output like
    1194509197.415800
     
    Duncan Muirhead, Nov 29, 2007
    #5
  6. PawelCarqowski

    Spoon Guest

    santosh wrote:

    > struct timeval is a GNU C extension.


    http://opengroup.org/onlinepubs/009695399/basedefs/sys/time.h.html

    The <sys/time.h> header shall define the timeval structure that
    includes at least the following members:

    time_t tv_sec Seconds.
    suseconds_t tv_usec Microseconds.

    The time_t and suseconds_t types shall be defined as described
    in <sys/types.h>.

    time_t and clock_t shall be integer or real-floating types.

    The type suseconds_t shall be a signed integer type capable of
    storing values at least in the range [-1, 1000000].

    > There are no specific format specifiers for this [...]


    Right.

    (However, glibc does allow custom conversion specifiers.)

    http://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html

    > but since the structure members are of type long int
    > you can use the 'ld' format specifier for printing them.


    AFAIU, it is possible for time_t to be wider than long int, but
    I'm not sure. Consider a platform where time_t is 64 bits wide
    and long int is only 32 bits wide.

    > For furher GNU C related questions please ask in a GNU group
    > or in a Linux group.


    comp.unix.programmer for Unix-related questions.
    gnu.gcc.help for GCC-specific questions.
     
    Spoon, Nov 29, 2007
    #6
  7. PawelCarqowski

    Spoon Guest

    Duncan Muirhead wrote:

    > PawelCarqowski wrote:
    >
    >> Do You know any conversion specification for this. I imagine:
    >>
    >> struct timeval time;
    >> printf("%T\n", time);

    >
    > I use
    > printf( "%ld.%.6ld\n", time.tv_sec, time.tv_usec);
    > which produces output like
    > 1194509197.415800


    What does it print when tv_usec = 666? :)
     
    Spoon, Nov 29, 2007
    #7
  8. PawelCarqowski

    James Kuyper Guest

    Spoon wrote:
    ....
    > AFAIU, it is possible for time_t to be wider than long int, but
    > I'm not sure. Consider a platform where time_t is 64 bits wide
    > and long int is only 32 bits wide.


    time_t can be any arithmetic type. It could even be _Imaginary; which
    has some interesting :) implications.
     
    James Kuyper, Nov 29, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Quon

    struct timeVal

    Tim Quon, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    700
    Tim Quon
    Sep 26, 2003
  2. Tony Houghton

    sizeof(struct timeval)

    Tony Houghton, Mar 14, 2006, in forum: Python
    Replies:
    8
    Views:
    4,000
  3. sapsi

    Using struct timeval in python

    sapsi, Sep 8, 2007, in forum: Python
    Replies:
    2
    Views:
    577
    sapsi
    Sep 8, 2007
  4. Joe

    adding timeval's

    Joe, Oct 26, 2008, in forum: C++
    Replies:
    5
    Views:
    1,057
    James Kanze
    Oct 28, 2008
  5. bauran

    printf conversion specification

    bauran, Oct 17, 2009, in forum: C Programming
    Replies:
    5
    Views:
    446
    Seebs
    Oct 18, 2009
Loading...

Share This Page