Changes between Version 24 and Version 25 of CodeStyle


Ignore:
Timestamp:
11/16/17 15:18:46 (2 months ago)
Author:
j.ca
Comment:

Also mention suseconds_t

Legend:

Unmodified
Added
Removed
Modified
  • CodeStyle

    v24 v25  
    8181= Portability concerns =
    8282
    83 == Printing time_t values ==
     83== Printing time_t and suseconds_t values ==
    8484
    85 POSIX specifies that time_t is an integer type but does not specify its size.  The underlying type might be "int", "long" or "long long", some systems might make it unsigned too.  This makes it hard to portably print time_t values using printf-like APIs, as a mismatch between the format string and the actual time_t size can lead to crashes or bogus representations. The safest way is to print time_t values as long long, which is at least 64 bits wide:
     85POSIX specifies that time_t is an integer type but does not specify its size.  The underlying type might be "int", "long" or "long long", some systems might make it unsigned too.  This makes it hard to portably print time_t values using printf-like APIs, as a mismatch between the format string and the actual time_t size can lead to crashes or bogus representations.
     86
     87The safest way is to print time_t values as long long, which is at least 64 bits wide and thus does not cause y2038 problems.
     88
     89Similarly, POSIX specifies suseconds_t as a signed integer, but does not specify its size.  Casting it as a long is enough.
     90
    8691{{{#!c
    87 time_t now = time(NULL);
    88 printf("%lld seconds\n", (long long)now);
     92  struct timeval now;
     93  gettimeofday(&now, NULL);                                                                                                                                                                                   
     94  printf("%lld.%06ld\n", (long long)now.tv_sec, (long)now.tv_usec);
    8995}}}
     96
    9097
    9198= Crustify rules =