Meredith Montgomery <
[email protected]> writes:
Here's an example of how to use time(). It uses %ju in printf. Where
can I find a definition of these %-specifiers?
--8<---------------cut here---------------start------------->8---
EXAMPLES
Getting the Current Time
The following example uses the time() function to calculate the time
elapsed, in seconds, since the Epoch, localtime() to convert that value
to a broken-down time, and asctime() to convert the broken-down time
values into a printable string.
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t result;
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&result)),
(uintmax_t)result);
return(0);
}
This example writes the current time to stdout in a form like this:
Wed Jun 26 10:32:15 1996
835810335 secs since the Epoch
--8<---------------cut here---------------end--------------->8---
The type there is uintmax_t, perhaps defined by inttypes.h. When I look
at the manual for inttypes.h, it suggests I should use constants such as PRIxMAX. Under msys2-64, the constant PRIxMAX expands to %ld. But my
printf under MinGW-64 does understand %ju just fine. Where is the
manual I'm missing? Thank you!
(*) The source of the information
I looked at ``man 3p time'' under msys2-64. These manual pages are
installed with the package man-pages-posix. The package itself reports
that it has something to do with https://www.kernel.org/doc/man-pages.
--8<---------------cut here---------------start------------->8---
%pacman -Si man-pages-posix
Repository : msys
Name : man-pages-posix
Version : 2017_a-1
Description : POSIX Manual Pages
Architecture : any
URL : https://www.kernel.org/doc/man-pages
Licenses : custom:posix
Groups : None
Provides : None
Depends On : man
Optional Deps : None
Conflicts With : None
Replaces : None
Download Size : 2.58 MiB
Installed Size : 2.58 MiB
Packager : CI (msys2-autobuild/310a1fa4/1493542190)
Build Date : Tue Nov 23 03:25:13 2021
Validated By : MD5 Sum SHA-256 Sum Signature
--8<---------------cut here---------------end--------------->8---
Looking it up, I find
https://man7.org/linux/man-pages/man2/time.2.html
but the example is not there.
In addition to the man pages others have cited, the canonical definition
is in the ISO C standard.
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf is a public
draft of the 2011 edition of the ISO C standard. I'm not aware of any
relevant changes in later editions.
Section 7.21.6.1 defines fprintf (printf is defined in terms of
fprintf), including the 'j' length modifier and the 'u' and 'd'
conversion specifiers.
7.27.2.4 defines the time() function.
7.20 defines the <stdint.h> header, which defines uintmax_t. If you
have an unsigned integer for which you don't know the correct printf
format string, you can convert to uintmax_t (which is guaranteed not to
lose information) and print using "%ju".
<inttypes.h> includes <stdint.h> and declares several functions and
macros like PRIxMAX. (<stdint.h> is required for all implementations; <inttypes.h> is required only for hosted implementations. Freestanding implementations needn't support any library functions.)
You can't assume that time_t is equivalent to any specific integer type,
or (as the sample code above does) that it's unsigned (it's usually
signed), or even that it's an integer type (POSIX requires it to be an
integer type; C doesn't).
Since this is comp.unix.programmer, I suppose you can assume POSIX, but
be aware of what assumptions you're making.
I would have converted to intmax_t and used "%jd" rather than "%ju",
because time_t is *usually* a signed integer type. If I wanted to be
painfully portable, I'd do something like this:
#include <time.h>
#include <stdint.h>
#include <stdio.h>
int main(void) {
const time_t now = time(NULL);
if ((time_t)1 / 2 > 0) {
// time_t is a floating-point type
printf("%Lf\n", (long double)now);
}
else if ((time_t)-1 < 0) {
// time_t is an unsigned integer type
printf("%ju\n", (uintmax_t)now);
}
else {
// time_t is a signed integer type
printf("%jd\n", (intmax_t)now);
}
}
Note that time() returns (time_t)-1 if the calendar time is not
available. If time_t is an unsigned type, that will give you the
maximum value of that type, perhaps 18446744073709551615
(0xffffffffffffffff).
--
Keith Thompson (The_Other_Keith)
[email protected]
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)