wavebitscientific / datetime-fortran

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

Cannot set seconds or others of timedelta #48

Closed dongli closed 6 years ago

dongli commented 7 years ago

Code is as following:

time_step_size = timedelta(seconds=time_step_size_in)

and error is:

Error: Component 'seconds' at (1) is a PRIVATE component of 'timedelta'

The Fortran compiler that I am using is gfortran 7.2.0.

zbeekman commented 7 years ago

I can't reproduce this... Also, you need to post more of your code.

cat foo.f90

program foo
  use mod_timedelta
  implicit none
  integer, parameter :: time_step_size_in = 5
  type(timedelta) :: time_step_size

  time_step_size = timedelta(seconds=time_step_size_in)

end program
$ gfortran --version
GNU Fortran (Homebrew GCC 7.2.0) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gfortran -Wall -Wextra -pedantic -c timedelta.f90
$ gfortran -Wall -Wextra -pedantic foo.f90 timedelta.o
$ ./a.out
milancurcic commented 7 years ago

@dongli Thank you for reporting this.

Can you show a minimal complete example of how you are using timedelta?

Specifically, what is the type of time_step_size_in? My guess is that it is a real number. timedelta_constructor function which overloads the default timedelta constructor, is designed for integer arguments only. If you try to pass a real, compiler will not resolve this interface.

An example below with integer arguments works as expected:

use datetime_module,only:timedelta
implicit none
type(timedelta) :: td

td = timedelta(seconds=60)
write(*,*)td % total_seconds()

end

In the above example, if you change 60 to 60., you can reproduce your original error message.

Note that you can specify a fraction of seconds like this:

td = timedelta(seconds=60, milliseconds=123)

but the precision is still limited to milliseconds.

This raises a few questions about the design of timedelta object:

milancurcic commented 6 years ago

No response from OP, assume resolved.