wavebitscientific / datetime-fortran

Date and time manipulation for modern Fortran
MIT License
137 stars 50 forks source link

Hour comparison in num2date is incorrect #64

Closed penguian closed 4 years ago

penguian commented 4 years ago

In num2date, mod_datetime.f90#L1238 has:

if(num2date % hour == 60)then

Surely that comparison should be to 24, not 60?

aekiss commented 4 years ago

Just a thought - would it be more error-tolerant to use >= rather than == in these adjustments, then subtracting off the factor? Otherwise it assumes rollovers of exactly 1. I guess that should always be the case, but there's probably no harm in coding more defensively... https://github.com/wavebitscientific/datetime-fortran/blob/master/src/lib/mod_datetime.f90#L1225-L1241

I'm suggesting something like

  if(num2date % millisecond >= 1000)then
    num2date % millisecond = num2date % millisecond - 1000
    call num2date % addSeconds(1)
  endif

  if(num2date % second >= 60)then
    num2date % second = num2date % second - 60
    call num2date % addMinutes(1)
  endif
  if(num2date % minute >= 60)then
    num2date % minute = num2date % minute - 60
    call num2date % addHours(1)
  endif
  if(num2date % hour >= 24)then
    num2date % hour = num2date % hour - 24
    call num2date % addDays(1)
  endif

(which will still mess up if the rollover is huge)

milancurcic commented 4 years ago

You are correct, it should compare with 24, not 60. I also like the defensive approach by @aekiss. Do either of you mind submitting a PR?