Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Date truncated by cast to long #1355

Open
mdtdhi opened this issue Mar 1, 2021 · 7 comments
Open

Date truncated by cast to long #1355

mdtdhi opened this issue Mar 1, 2021 · 7 comments

Comments

@mdtdhi
Copy link

mdtdhi commented Mar 1, 2021

Using netcdfAll-4.6.16.1 java library.

I've encountered multiple NetCDF files where the date is truncated by a second in DateUnit.makeStandardDateString(double). The implementation in 4.6.16.1 results in a date string with second precision, but I believe the date is only truncated by a millisecond.

i.e. secs = 89999.999999999985 is incorrectly rounded down to 89999999 milliseconds.

see /cdm/core/src/main/java/ucar/nc2/units/DateUnit.java
getDate (line 191)
makeDate (line 213):
return new Date(getDateOrigin().getTime() + (long) (1000 * secs));

Rounding prior to casting should solve the issue. Something like:
return new Date(getDateOrigin().getTime() + (long) Math.Round(1000 * secs));

@lesserwhirls
Copy link
Collaborator

Do you have an example file that you could share that triggers this issue?

@mdtdhi
Copy link
Author

mdtdhi commented Mar 2, 2021

Do you have an example file that you could share that triggers this issue?

See attached.

Date time should be 2021-02-27 01:00:00. Result is 2021-02-27 00:59:59.

IDY25402.APS3.group2.slv.2021022600.025.surface.zip

@JohnLCaron
Copy link
Collaborator

JohnLCaron commented Mar 7, 2021 via email

@JohnLCaron
Copy link
Collaborator

Date time should be 2021-02-27 01:00:00.

Which exact Date time in the example file are you referring to?

@JohnLCaron
Copy link
Collaborator

I dont see the problem in versions 5 and 6, though I dont have a particular field to check.
Its likely that their analysis is correct for 4.6.

@mdtdhi
Copy link
Author

mdtdhi commented Mar 11, 2021

Date time should be 2021-02-27 01:00:00.

Which exact Date time in the example file are you referring to?

Variable: 'time'

Attributes:
'axis' 'T'
'bounds' 'time_bnds'
'units' 'days since 2021-02-26 00:00:00'
'standard_name' 'time'
'long_name' 'time'
'calendar' 'gregorian'
'_ChunkSize' 1
'_CoordinateAxisType' 'Time'

@JohnLCaron
Copy link
Collaborator

version 6:

double time(time=1);
  :axis = "T"; // String
  :bounds = "time_bnds"; // String
  :units = "days since 2021-02-26 00:00:00"; // String
  :standard_name = "time"; // String
  :long_name = "time"; // String
  :calendar = "gregorian"; // String
  :_ChunkSizes = 1U;

days since 2021-02-26 00:00:00
time =
{1.0416666666666665}

(-0.020833, 1.041667) == (2021-02-25T23:30:00Z, 2021-02-27T01:00:00Z)

@lesserwhirls lesserwhirls transferred this issue from Unidata/netcdf-java Mar 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants