I tried using the nstrftime function instead of strftime; strftime didn't seem to
recognize %N, even when I included gnulib's strftime.h header. Then the %N format
added trailing zeroes I wasn't expecting. Unfortunately, the test-strftime.c file
didn't include a test for nanosecond-resolution display, so I wasn't sure if this
was a problem with my call or the %N specification.
I've produced a patch for gnulib to demonstrate what I was missing, attached. Jim,
should I submit it to the bug-gnulib mailing list? It is an additional test that shows
what the actual behavior of nstrftime is, though I don't know if that's what the
expected behavior is. I would expect 10 nanoseconds to be reported as
"0.00000001" seconds, not "0.000000010".
The second attached patch uses nstrftime, and gives funny-looking results. For example,
here are the first two mtimes of hivexml's output on hivex/images/large, line 1 for
the first version of this patch, line 2 using nstrftime:
<hive><mtime>2010-02-02T13:42:52.27Z</mtime><node
name="$$$PROTO.HIV"
root="1"><mtime>2010-02-02T13:42:44.626Z</mtime>
<hive><mtime>2010-02-02T13:42:52.270000000Z</mtime><node
name="$$$PROTO.HIV"
root="1"><mtime>2010-02-02T13:42:44.626000000Z</mtime>
I'm partial to the first version's output.
--Alex
On Oct 14, 2011, at 06:32 , Richard W.M. Jones wrote:
On Wed, Oct 12, 2011 at 08:26:15PM -0700, Alex Nelson wrote:
> - if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%TZ", tm) == 0) {
> + if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%T", tm) == 0) {
It turns out there's a better way to do this (which I didn't know
about either -- thanks Jim Meyering).
Using the gnulib strftime replacement function, you can use %N for
nanoseconds, and you can also use a number in front to get the
required precision.
gnulib strftime is the same function that (GNU) date uses, hence you
can try it out:
$ date '+%T.%3N' ; date '+%T.%N'
14:25:27.840
14:25:27.842204825
The gnulib {,n}strftime function takes extra parameters, one of which
carries the nanosecond value (ns). Have a look at
hivex.git/.gnulib/tests/test-strftime.c to see an example.
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v