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

[UDUNITS #ODN-661035]: Problem converting date from hours units wrt a ref time

Dear Prakash Barman,

> I need to convert time value present in a netcdf file, encoded in
> UDUNITS (hrs since 1900-01-01 00:00) to general "yyyy-mm-dd hh" format. The
> following code I am using. But what ever be the input, it always produces
> 2001  1 11 20 32 24.0 as output. Please help me in this regard....
> #include <stdio.h>
> #include <stdlib.h>
> #include <udunits2.h>
> int main(void)
> {
> const char *ref_value    = "hour since 1900-01-01 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");
> }
> if( (u_unit = ut_parse(u_system, ref_value, UT_ASCII)) == NULL ){
> fprintf(stderr, "Unable to the unit string %s!\n", ref_value);
> }
> time = 937944;
> ut_decode_time(time, &year, &month, &day, &hour, &minute,
> &second,&resolution);
> printf("%4d %2d %2d %2d %2d %3.1f\n", year, month, day, hour,
> minute, second);
> return(EXIT_SUCCESS);
> }

You should use a true calendaring package to convert temporal values.

Having said that, you can use the UDUNITS-2 package to do approximately what 
you want (the UDUNITS-2 package doesn't handle leap-seconds). Create a unit 
that is "seconds since 1900-01-01 00:00:00". Obtain a converter from the 
"hours" unit to the "seconds" unit. Convert values using this converter. Encode 
a new value in the "seconds" unit by calling the function "ut_encode_time(1900, 
1, 1, 0, 0, secondsSince19000101). Obtain the corresponding calendar time by 
calling the function "ut_decode_time(encodedTime, &year, &month, &day, &hour, 
&minute, &second, &resolution).

Yes, it's not very convenient. But I would prefer that a true calendaring 
package be use for such conversions.

> Thanks
> Prakash Barman
> JRF, Dept. of Physics
> Tezpur University, Tezpur

Steve Emmerson

Ticket Details
Ticket ID: ODN-661035
Department: Support UDUNITS
Priority: Normal
Status: Closed

NOTE: All email exchanges with Unidata User Support are recorded in the Unidata inquiry tracking system and then made publicly available through the web. If you do not want to have your interactions made available in this way, you must let us know in each email you send to us.