[udunits] Problem converting "days since ..." YYYY/MM

I'm currently building a Fortran 2003 interface for Udunits 2. I'm
having some trouble testing it though. I need UDUNITS to convert "days
since ..." to YYYY/MM and vice versa. In my testing, I became
convinced that the interface didn't work properly. But I then did some
testing in C and found that I'm probably not calling UDUNITS properly
in either language. Below is code to illustrate what my issue is. I've
used Udunits 2 in the context of using David Pierce's calcalcs
extensions of udunits 2's functionality. This involved calling
ut_read_xml, ut_parse and then utCalendar2_cal. utCalendar2_cal
recieves the ut_unit type variable passed from ut_parse so it knows
what the referene time is. In using udunits 2 only, I call
ut_read_xml, ut_parse and then ut_decode_time. The result however is
relative to January 1, 2001 00:00:00. How do I make udunits2 aware of
my preferred time origin?

Also, is it possible to suppress the "... overrides prefixed-unit ..."
warnings upon calling ut_read_xml?


Chad


Example code:

/*
gcc -Wall -Wextra -pedantic -std=c89 udunits2_demo.c
-I/usr/local/udunits-2.1.21/include -L/usr/local/udunits-2.1.21/lib
-ludunits2 -lexpat -lm && ./a.out
*/

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

int main(void)
{
        const char *units = "days since 1800-1-1 00:00:0.0";

        ut_system *u_system;
        ut_unit *u_unit;

        double time;
        int year, month, day, hour, minute;
        double second, resolution;

        if( (u_system = ut_read_xml(NULL)) == NULL ){
                fprintf(stderr, "Unable to initialize the udunits2 library!\n");
                exit(EXIT_FAILURE);
        }

        if( (u_unit = ut_parse(u_system, units, UT_ASCII)) == NULL ){
                fprintf(stderr, "Unable to the unit string %s!\n", units);
                exit(EXIT_FAILURE);
        }

        time = 18277.5;

        ut_decode_time(time, &year, &month, &day, &hour, &minute, &second,
&resolution);

        fprintf(stdout, "%4d %2d %2d %2d %2d %3.1f\n", year, month, day,
hour, minute, second);

/*
output is: 2001  1  1  5  4 37.5
should be: 1850  1 16 12  0  0.0
*/

        return(EXIT_SUCCESS);
}



  • 2011 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the udunits archives: