[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");
> exit(EXIT_FAILURE);
> }
> 
> if( (u_unit = ut_parse(u_system, ref_value, UT_ASCII)) == NULL ){
> fprintf(stderr, "Unable to the unit string %s!\n", ref_value);
> exit(EXIT_FAILURE);
> }
> 
> 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

Regards,
Steve Emmerson

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


pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy