[hivex][PATCH] Increase filetime printing resolution to sub-second
Signed-off-by: Alex Nelson <ajnelson@cs.ucsc.edu> --- xml/hivexml.c | 20 +++++++++++++++++--- 1 files changed, 17 insertions(+), 3 deletions(-) diff --git a/xml/hivexml.c b/xml/hivexml.c index 5030c24..98b90c5 100644 --- a/xml/hivexml.c +++ b/xml/hivexml.c @@ -185,6 +185,8 @@ filetime_to_8601 (int64_t windows_ticks) char *ret; time_t t; struct tm *tm; + int64_t sub_seconds; + size_t ftd; /* # chars formatted so far. */ if (windows_ticks == 0LL) return NULL; @@ -194,16 +196,28 @@ filetime_to_8601 (int64_t windows_ticks) if (tm == NULL) return NULL; - ret = malloc (TIMESTAMP_BUF_LEN); + sub_seconds = windows_ticks % WINDOWS_TICK; + /* Trim trailing zeroes from fractional part. */ + while (sub_seconds % 10 == 0 && sub_seconds > 0) { + sub_seconds /= 10; + } + + ret = calloc (TIMESTAMP_BUF_LEN, sizeof (char)); if (ret == NULL) { - perror ("malloc"); + perror ("calloc"); exit (EXIT_FAILURE); } - if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%TZ", tm) == 0) { + if (strftime (ret, TIMESTAMP_BUF_LEN, "%FT%T", tm) == 0) { perror ("strftime"); exit (EXIT_FAILURE); } + ftd = strlen (ret); + + if (snprintf (ret + ftd, TIMESTAMP_BUF_LEN - ftd, ".%" PRIi64 "Z", sub_seconds) == 0) { + perror ("snprintf"); + exit (EXIT_FAILURE); + } return ret; } -- 1.7.6.4
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
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
Alex Nelson wrote:
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
Hi Alex, Yes, the test case addition would be welcome on bug-gnulib.
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 %N directive is defined to produce zero-padded nanoseconds. As such, it must represent 10 as 000000010, not 00000001. If you choose to put that string after a decimal point, you're changing the semantics to "fractional seconds", at which point you can safely post-process it to remove trailing '0's.
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.
If you can settle for less resolution, you may want to use e.g., %6N or %3N: $ date +%T.%3N 11:03:00.728 $ date +%T.%6N 11:03:01.463570
On Dec 13, 2011, at 02:04 , Jim Meyering wrote:
Alex Nelson wrote:
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
Hi Alex,
Yes, the test case addition would be welcome on bug-gnulib. Thanks, I've submitted 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 %N directive is defined to produce zero-padded nanoseconds. Ok. I haven't been able to find this with Google; where is it documented?
As such, it must represent 10 as 000000010, not 00000001. If you choose to put that string after a decimal point, you're changing the semantics to "fractional seconds", at which point you can safely post-process it to remove trailing '0's. Post-processing to remove 0's seems less efficient to me than not producing the trailing 0's at all. I take it there is no similar nanosecond % directive that prints down to the most significant digit?
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.
If you can settle for less resolution, you may want to use e.g., %6N or %3N:
$ date +%T.%3N 11:03:00.728
$ date +%T.%6N 11:03:01.463570 It's my preference to not restrict the resolution. Come to think about it, this %nN directive could afford to go into that test-strftime program, too. I'll submit another patch once the first one goes through.
Thanks, Jim! --Alex
Alex Nelson wrote:
On Dec 13, 2011, at 02:04 , Jim Meyering wrote:
Alex Nelson wrote: ...
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 %N directive is defined to produce zero-padded nanoseconds. Ok. I haven't been able to find this with Google; where is it documented?
date --help gives a clue: %N nanoseconds (000000000..999999999)
As such, it must represent 10 as 000000010, not 00000001. If you choose to put that string after a decimal point, you're changing the semantics to "fractional seconds", at which point you can safely post-process it to remove trailing '0's.
Post-processing to remove 0's seems less efficient to me than not
You're worried about run-time efficiency? If that's it, I suggest that you measure first. If you're worried about your coding efficiency, that would have to be balanced against changing a long-established API that is in use by everything from coreutils to emacs. (i.e., it won't change)
producing the trailing 0's at all. I take it there is no similar nanosecond % directive that prints down to the most significant ... If you can settle for less resolution, you may want to use e.g., %6N or %3N:
$ date +%T.%3N 11:03:00.728
$ date +%T.%6N 11:03:01.463570
It's my preference to not restrict the resolution. Come to think about it, this %nN directive could afford to go into that test-strftime program, too. I'll submit another patch once the first one goes through.
Please just combine them into one, since they're so small and similar.
participants (3)
-
Alex Nelson -
Jim Meyering -
Richard W.M. Jones