Skip to content

Commit 3b2db6b

Browse files
committed
Merge pull request #21 from milancurcic/devel
Devel
2 parents b5cc297 + 3e95c6e commit 3b2db6b

File tree

12 files changed

+868
-705
lines changed

12 files changed

+868
-705
lines changed

Makefile

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1+
#
2+
# datetime-fortran Makefile
3+
#
4+
########################################################################
5+
# Compiler and flags
16

7+
### GNU ###
28
FC = gfortran
3-
FCFLAGS = -Wall -O0 -C -fbacktrace
4-
5-
# Rules
6-
all: datetime.f90
7-
$(FC) -c $(FCFLAGS) datetime.f90
8-
ar r libdatetime.a datetime.o
9-
10-
.PHONY:
9+
FCFLAGS = -cpp -g -O0 -C -fbacktrace
10+
11+
### Intel ###
12+
#FC = ifort
13+
#FCFLAGS = -cpp -g -O0 -C -traceback -assume realloc_lhs
14+
15+
### Cray ###
16+
#FC = ftn
17+
#FCFLAGS = -O 0 -e Z -g
18+
19+
########################################################################
20+
# Targets
21+
22+
.PHONY: all datetime tests
23+
24+
all: tests
25+
26+
datetime:
27+
@echo "Building $@"
28+
$(MAKE) FC=$(FC) FCFLAGS="$(FCFLAGS)" --directory=src/lib
29+
30+
tests: datetime
31+
@echo "Building $@"
32+
$(MAKE) FC=$(FC) FCFLAGS="$(FCFLAGS)" --directory=src/tests
33+
cp src/tests/datetime_tests .
34+
1135
clean:
12-
rm -f datetime.o datetime_module.mod libdatetime.a
36+
$(MAKE) --directory=src/lib clean
37+
$(MAKE) --directory=src/tests clean
38+
rm -v datetime_tests

src/lib/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# datetime-fortran Makefile
3+
#
4+
.SUFFIXES: .f90 .o
5+
.PHONY: all datetime clean
6+
7+
OBJS = datetime.o mod_datetime.o mod_timedelta.o mod_strftime.o mod_clock.o mod_constants.o
8+
9+
.f90.o:
10+
$(FC) -c $(FCFLAGS) $<
11+
12+
all:
13+
$(MAKE) datetime
14+
ar ruv libdatetime.a $(OBJS)
15+
16+
datetime: datetime.o
17+
18+
datetime.o: datetime.f90 mod_clock.o mod_datetime.o mod_strftime.o mod_timedelta.o
19+
mod_clock.o: mod_clock.f90 mod_datetime.o mod_timedelta.o
20+
mod_datetime.o: mod_datetime.f90 mod_constants.o mod_strftime.o mod_timedelta.o
21+
mod_timedelta.o: mod_timedelta.f90
22+
mod_strftime.o: mod_strftime.f90
23+
mod_constants.o: mod_constants.f90
24+
25+
clean:
26+
rm -f *.o *.mod *.a

src/lib/datetime.f90

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
!
2+
! datetime-fortran - A Fortran library for date and time manipulation
3+
! Copyright (c) 2013-2016, Wavebit Scientific LLC
4+
! All rights reserved.
5+
!
6+
! Licensed under the BSD-3 clause license. See LICENSE for details.
7+
!
8+
module datetime_module
9+
!=======================================================================
10+
!
11+
! Version: 1.3.0
12+
!
13+
! Last update: 2016-01-24
14+
!
15+
! Author: Milan Curcic <mcurcic@wavebitscientific.com>
16+
! Wavebit Scientific LLC
17+
!
18+
! Description: A Fortran module that provides time and date manipulation
19+
! facilities. Conforms to Fortran 2003 standard.
20+
!
21+
! Contains:
22+
!
23+
! types:
24+
!
25+
! datetime - Main datetime object
26+
! timedelta - Time difference object
27+
! clock - A generic clock container
28+
! tm_struct - For compatibility with C/C++ procedures
29+
!
30+
! datetime methods:
31+
!
32+
! procedure :: getYear
33+
! procedure :: getMonth
34+
! procedure :: getDay
35+
! procedure :: getHour
36+
! procedure :: getMinute
37+
! procedure :: getSecond
38+
! procedure :: getMillisecond
39+
! procedure :: isocalendar
40+
! procedure :: isoformat
41+
! procedure :: isValid
42+
! procedure :: now
43+
! procedure :: secondsSinceEpoch
44+
! procedure :: strftime
45+
! procedure :: tm
46+
! procedure :: tzOffset
47+
! procedure :: utc
48+
! procedure :: weekday
49+
! procedure :: weekdayShort
50+
! procedure :: weekdayLong
51+
! procedure :: yearday
52+
!
53+
! timedelta methods:
54+
!
55+
! procedure :: getDays
56+
! procedure :: getHours
57+
! procedure :: getMinutes
58+
! procedure :: getSeconds
59+
! procedure :: getMilliseconds
60+
! procedure :: total_seconds
61+
!
62+
! clock methods:
63+
!
64+
! procedure :: reset
65+
! procedure :: tick
66+
!
67+
! public procedures:
68+
!
69+
! function c_strftime
70+
! function c_strptime
71+
! function date2num
72+
! function datetimeRange
73+
! function daysInMonth
74+
! function daysInYear
75+
! function isLeapYear
76+
! function num2date
77+
! function strptime
78+
! function tm2date
79+
!
80+
!=======================================================================
81+
82+
use mod_datetime, only:datetime,date2num,datetimeRange,daysInMonth,&
83+
daysInYear,isLeapYear,num2date,strptime,tm2date
84+
use mod_timedelta,only:timedelta
85+
use mod_clock, only:clock
86+
use mod_strftime, only:tm_struct,c_strftime,c_strptime
87+
88+
!=======================================================================
89+
endmodule datetime_module

src/lib/mod_clock.f90

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
!
2+
! datetime-fortran - A Fortran library for date and time manipulation
3+
! Copyright (c) 2013-2016, Wavebit Scientific LLC
4+
! All rights reserved.
5+
!
6+
! Licensed under the BSD-3 clause license. See LICENSE for details.
7+
!
8+
module mod_clock
9+
!=======================================================================
10+
!
11+
! mod_clock
12+
!
13+
!=======================================================================
14+
15+
use,intrinsic :: iso_fortran_env,only:real32,real64
16+
use,intrinsic :: iso_c_binding,only:c_char,c_int,c_null_char
17+
use :: mod_datetime,only:datetime
18+
use :: mod_timedelta,only:timedelta
19+
20+
implicit none
21+
22+
private
23+
24+
! Derived types:
25+
public :: clock
26+
27+
type :: clock
28+
29+
!! A clock object with a start, stop and current times, tick interval
30+
!! and tick methods.
31+
32+
type(datetime) :: startTime ! = datetime()
33+
type(datetime) :: stopTime ! = datetime()
34+
type(datetime) :: currentTime! = datetime()
35+
36+
type(timedelta) :: tickInterval! = timedelta(0)
37+
38+
! May become Alarm class in some future release;
39+
! for now, just a switch
40+
logical :: alarm = .false.
41+
42+
! Clock status flags
43+
logical :: started = .false.
44+
logical :: stopped = .false.
45+
46+
contains
47+
48+
procedure :: reset
49+
procedure :: tick
50+
51+
endtype clock
52+
!=======================================================================
53+
contains
54+
55+
56+
!=======================================================================
57+
pure elemental subroutine reset(self)
58+
59+
!! Resets the clock to its start time.
60+
61+
class(clock),intent(inout) :: self
62+
63+
self % currentTime = self % startTime
64+
65+
self % started = .false.
66+
self % stopped = .false.
67+
68+
endsubroutine reset
69+
!=======================================================================
70+
71+
72+
73+
!=======================================================================
74+
pure elemental subroutine tick(self)
75+
76+
!! Increments the currentTime of the clock instance by one tickInterval.
77+
78+
class(clock),intent(inout) :: self
79+
80+
if(self % stopped)then
81+
return
82+
endif
83+
84+
if(.not.self % started)then
85+
self % started = .true.
86+
self % currentTime = self % startTime
87+
endif
88+
89+
self % currentTime = self % currentTime + self % tickInterval
90+
91+
if(self % currentTime >= self % stopTime)then
92+
self % stopped = .true.
93+
endif
94+
95+
endsubroutine tick
96+
!=======================================================================
97+
endmodule mod_clock

src/lib/mod_constants.f90

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
!
2+
! datetime-fortran - A Fortran library for date and time manipulation
3+
! Copyright (c) 2013-2016, Wavebit Scientific LLC
4+
! All rights reserved.
5+
!
6+
! Licensed under the BSD-3 clause license. See LICENSE for details.
7+
!
8+
module mod_constants
9+
!=======================================================================
10+
!
11+
! mod_constants: Basic constants and time conversion factors.
12+
!
13+
!=======================================================================
14+
15+
use,intrinsic :: iso_fortran_env,only:real32,real64
16+
17+
implicit none
18+
19+
private
20+
21+
public :: zero,one,d2h,h2d,d2m,m2d,m2h,s2d,d2s,h2s,s2h,m2s,s2m,MAXSTRLEN
22+
23+
real(kind=real64),parameter :: zero = 0_real64 !! 0
24+
real(kind=real64),parameter :: one = 1_real64 !! 1
25+
26+
! Constant multipliers that transform a number
27+
! of some time unit to another:
28+
real(kind=real64),parameter :: d2h = 24_real64 !! day -> hour
29+
real(kind=real64),parameter :: h2d = one/d2h !! hour -> day
30+
real(kind=real64),parameter :: d2m = d2h*60_real64 !! day -> minute
31+
real(kind=real64),parameter :: m2d = one/d2m !! minute -> day
32+
real(kind=real64),parameter :: m2h = one/60_real64 !! minute -> hour
33+
real(kind=real64),parameter :: s2d = m2d/60_real64 !! second -> day
34+
real(kind=real64),parameter :: d2s = 86400_real64 !! day -> second
35+
real(kind=real64),parameter :: h2s = 3600_real64 !! hour -> second
36+
real(kind=real64),parameter :: s2h = one/h2s !! second -> hour
37+
real(kind=real64),parameter :: m2s = 60_real64 !! minute -> second
38+
real(kind=real64),parameter :: s2m = one/m2s !! second -> minute
39+
40+
! Maximum string length for strftime.
41+
! Constant for now; may become a preprocessor macro later.
42+
integer,parameter :: MAXSTRLEN = 99
43+
44+
!=======================================================================
45+
endmodule mod_constants

0 commit comments

Comments
 (0)