master_time.f90 Source File


Contents

Source Code


Source Code

module master_time_m
    implicit none

    integer, save, private :: time_save !! save the time

contains

    !> Start the timer
    subroutine tic()
        call system_clock(time_save)
    end subroutine tic

    !> Stop the timer and return the time in seconds
    subroutine toc(t)
        integer, intent(out), optional :: t !! time in seconds
        integer :: time_now, time_rate

        call system_clock(time_now, time_rate)
        time_now = (time_now - time_save)/time_rate

        if (present(t)) then
            t = time_now
        else
            write (*, "(a,i0,a)") 'Time elapsed: ', time_now, "s"
        end if
    end subroutine toc

    !> Print the current time and date
    subroutine time_print(time)
        logical, optional :: time !! If time is requested
        character(len=8) :: datstr
        character(len=10) :: timstr

        ! Get the current date and time.
        call date_and_time(datstr, timstr)

        ! Write out the date and time.
        write (*, "(A)") "Date = "//datstr(1:4)//"/"// &
            datstr(5:6)//"/"// &
            datstr(7:8)

        if (present(time)) then
            if (.not. time) return
        end if

        write (*, "(A)") "Time = "//timstr(1:2)//":"// &
            timstr(3:4)//":"// &
            timstr(5:10)

    end subroutine time_print

    !> Get the current date or time and date
    function time_stamp(time) result(t)
        logical, optional :: time   !! If time is requested
        character(:), allocatable :: t

        logical :: time_
        character(len=8) :: datstr
        character(len=10) :: timstr
        character(:), allocatable :: timstr_

        if (present(time)) then
            time_ = time
        else
            time_ = .true.
        end if

        ! Get the current date and time.
        call date_and_time(datstr, timstr)

        if (time_) then
            allocate (character(13) :: timstr_)
            timstr_ = " "//timstr(1:2)//":"// &
                      timstr(3:4)//":"// &
                      timstr(5:10)
        else
            timstr_ = ""
        end if

        t = datstr(1:4)//"/"// &
            datstr(5:6)//"/"// &
            datstr(7:8)//timstr_

    end function time_stamp

    !> Check if the year is a leap year
    logical pure function is_leap(year)
        integer, intent(in) :: year !! The year to check
        is_leap = ((mod(year, 4) == 0) .and. (mod(year, 100) /= 0)) .or. (mod(year, 400) == 0)
    end function is_leap

end module master_time_m