bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: human-time?


From: Bruno Haible
Subject: Re: human-time?
Date: Fri, 11 Jan 2008 00:15:20 +0100
User-agent: KMail/1.5.4

Hi Simon,

> right now I'm
> mostly looking for comments on the general approach and on the function
> prototype:
> 
> char *
> human_readable_time (time_t age, unsigned int units)

I would also add an 'unsigned int nanoseconds' argument. So that the module
can also be used for applications in the range of microseconds to seconds.
Someone who doesn't need nanoseconds just passes 0.

> I'd prefer if the module didn't use xvasprintf because I probably want
> to use this in a library, and x* doesn't work well there (and there are
> license problems too).

Then I would keep the 'char *' return value and document that a NULL return
value means a malloc failure (optionally with errno = ENOMEM if there can
be other failure reasons). A "char *buf, size_t bufsiz" argument pair
is just a pain to use.

> However, we could also do it like:
> 
> char *
> human_readable_time (time_t age, unsigned int units);
> 
> and allow the function to return NULL on memory allocation errors (and
> errno would be set).  Maybe this is cleaner, I find that functions that
> use fixed-size buffers are difficult to use.

I feel the same way.

> Also, there is the problem of how long a month and a year is, it isn't
> really well-defined.  This is mostly relevant if you use full precision,
> otherwise the string is just an approximation anyway.

Good point. Then it requires two pairs of
(time_t seconds, unsigned int nanoseconds) arguments, one for the start of
the time interval and one for the end of the time interval.

You want the distance between 2007-01-01 and 2008-01-01 to be "1 year"
(365 days), but also between 2008-01-01 and 2009-01-01 - here "1 year"
is 366 days.

Four things about the code:

- When units = 2, it produces results like this:
    3 days 10 hours
    3 days 1 hour
    3 days 5 minutes
    3 days 39 seconds
  But a user does not normally care about seconds for intervals of 3 days.
  Or if he does then he also does for intervals of 3 days 10 hours.
  So, I would change the output to this:
    3 days 10 hours
    3 days 1 hour
    3 days
    3 days
  In other words, when units = 2, and the primary unit is days, you should
  stop after the hours; when units = 3, after the minutes, and when units = 4,
  after the seconds.

- time_t is not the right type for a time duration; better use 'unsigned long'
  for it.

- Internationalization. I would try to do it as if all languages
  were English; let's then see which translators complain.

- When there are no configure-time tests, you can drop the .m4 file entirely
  and instead write in the module description an unconditional augmentation:
    lib_SOURCES += human-time.c

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]