Unidata / thredds

THREDDS Data Server v4.6
https://www.unidata.ucar.edu/software/tds/v4.6/index.html
266 stars 179 forks source link

Date truncated by cast to long #1355

Open mdtdhi opened 3 years ago

mdtdhi commented 3 years ago

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 commented 3 years ago

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

mdtdhi commented 3 years ago

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 commented 3 years ago

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

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

On Mon, Mar 1, 2021 at 5:15 PM mdtdhi notifications@github.com wrote:

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 12:59:59.

IDY25402.APS3.group2.slv.2021022600.025.surface.zip https://github.com/Unidata/netcdf-java/files/6065225/IDY25402.APS3.group2.slv.2021022600.025.surface.zip

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Unidata/netcdf-java/issues/620#issuecomment-788421838, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEVZQBYI4WU56NGVQIMIIDTBQU3TANCNFSM4YL3YWOA .

JohnLCaron commented 3 years ago

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

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

JohnLCaron commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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)