|
| 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 |
0 commit comments