Alex Nelson
2011-Oct-13 03:26 UTC
[Libguestfs] [hivex][PATCH] Increase filetime printing resolution to sub-second
Signed-off-by: Alex Nelson <ajnelson at 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
Richard W.M. Jones
2011-Oct-14 13:32 UTC
[Libguestfs] [hivex][PATCH] Increase filetime printing resolution to sub-second
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