Unidata / UDUNITS-2

API and utility for arithmetic manipulation of units of physical quantities
http://www.unidata.ucar.edu/software/udunits
Other
59 stars 36 forks source link

Weird behavior of ut_format() for timestamp unit refers "2001-01-01 00:00:00" #88

Closed himotoyoshi closed 3 years ago

himotoyoshi commented 4 years ago

For a timestamp unit that refers to "2001-01-01 00:00:00”, the creation of a definition or a name string using ut_format() fails. When referring to other times, there seems to be no problem.

The timestamp “2001-01-01 00:00:00" has an encoded value of 0 (i.e. origin of internal time value). When decoding this encoded value using ut_decode_time(), the resolution value is returned as 0.0. This 0.0 value of resolution seems to cause to occur this behavior, because the resolution value is related to the decimal length calculated as “-(int)floor(log10(resolution))” in the internal function “printTimestamp()”.

The following code is a sample program reproducing that situation.

/*
  Weird behavior of timestamp unit refers "2001-01-01 00:00:00"
*/

#include <stdio.h>
#include <stdlib.h>
#include "udunits2.h"

int
main ()
{
  ut_system *usys;
  ut_unit   *unit;
  char buf[128];
  int  len;

  ut_set_error_message_handler(ut_ignore);
  usys = ut_read_xml(NULL);

  unit = ut_parse(usys, "day since 2001-01-01 00:00:00", UT_ASCII);
/*  unit = ut_parse(usys, "day since 2002-01-01 00:00:00", UT_ASCII); */

  if ( ! unit ) {
    printf("failed to create unit object");
    exit(1);
  }

  len = ut_format(unit, buf, sizeof(buf), UT_ASCII | UT_NAMES);  

  printf("length of formatted string : %d\n", len);

  if ( len > 0 ) {
    printf("formatted string : %s\n", buf);
  }

  ut_free(unit);
}

This code results the following,

% ./a.out 
length of formatted string : -1

Thanks.

semmerson commented 3 years ago

I've fixed this bug.

The fix will be in the next release.

Thanks for letting me know.