human_readable_time.f90 2.7 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
!
Marco Govoni's avatar
Marco Govoni committed
2
! Copyright (C) 2015-2021 M. Govoni 
Marco Govoni's avatar
Marco Govoni committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
! This file is part of WEST.
!
! Contributors to this file: 
! Marco Govoni
!
!-----------------------------------------------------------------------
FUNCTION human_readable_time(time)
  !-----------------------------------------------------------------------
  !
  ! ... Given a time in seconds, the result is :
  ! ...  9999d-23h-59m-59.9s
  !
  USE kinds,      ONLY : DP
  !
  IMPLICIT NONE 
  ! 
  ! I/O
  !
  REAL(DP), INTENT(IN) :: time
  CHARACTER(20) :: human_readable_time
  !
  ! Workspace
  !
  CHARACTER(20) :: ds_temp,s_temp,m_temp,h_temp,d_temp
  REAL(DP) :: seconds
  INTEGER :: minutes,hours,days 
  !
  ! If < 0.1 s
  !
  IF(time<0.1_DP) THEN
     human_readable_time="< 00.1s"
     RETURN
  ENDIF
  !
  ! If seconds
  !
  IF(time<60.0_DP) THEN
     seconds=time
     WRITE(s_temp,'(i2.2)') INT(seconds)
     WRITE(ds_temp,'(i1.1)') INT(seconds*10.0 - 10.0*INT(seconds))
     human_readable_time=TRIM(ADJUSTL(s_temp))//"."//TRIM(ADJUSTL(ds_temp))//"s"
     RETURN
  ENDIF
  !
  ! If minutes
  !
  IF(time<3600.0_DP) THEN
     minutes=INT(time/60.0_DP)
     WRITE(m_temp,'(i2.2)') minutes
     seconds=time-minutes*60.0_DP
     WRITE(s_temp,'(i2.2)') INT(seconds)
     WRITE(ds_temp,'(i1.1)') INT(seconds*10.0_DP - 10.0_DP*INT(seconds))
     human_readable_time=TRIM(ADJUSTL(m_temp))//"m-"//TRIM(ADJUSTL(s_temp))//"."//TRIM(ADJUSTL(ds_temp))//"s"
     RETURN
  ENDIF
  !
  ! If hours
  !
  IF(time<86400.0_DP) THEN
     hours=INT(time/3600.0_DP)
     WRITE(h_temp,'(i2.2)') hours
     minutes=INT((time-hours*3600.0_DP)/60.0_DP)
     WRITE(m_temp,'(i2.2)') minutes
     seconds=time-hours*3600.0_DP-minutes*60.0_DP
     WRITE(s_temp,'(i2.2)') INT(seconds)
     WRITE(ds_temp,'(i1.1)') INT(seconds*10.0_DP - 10.0_DP*INT(seconds))
     human_readable_time=TRIM(ADJUSTL(h_temp))//"h-"//TRIM(ADJUSTL(m_temp))//"m-"//TRIM(ADJUSTL(s_temp))//&
     "."//TRIM(ADJUSTL(ds_temp))//"s"
     RETURN
  ENDIF
  !
  ! If days
  !
  days=INT(time/86400.0_DP)
  WRITE(d_temp,'(i5)') days
  hours=INT((time-days*86400.0_DP)/3600.0_DP)
  WRITE(h_temp,'(i2.2)') hours
  minutes=INT((time-days*86400.0_DP-hours*3600.0_DP)/60.0_DP)
  WRITE(m_temp,'(i2.2)') minutes
  seconds=time-days*86400.0_DP-hours*3600.0_DP-minutes*60.0_DP
  WRITE(s_temp,'(i2.2)') INT(seconds)
  WRITE(ds_temp,'(i1.1)') INT(seconds*10.0_DP - 10.0_DP*INT(seconds))
  human_readable_time=&
  TRIM(ADJUSTL(d_temp))//"d-"//TRIM(ADJUSTL(h_temp))//"h-"//TRIM(ADJUSTL(m_temp))//"m-"//TRIM(ADJUSTL(s_temp))//&
  "."//TRIM(ADJUSTL(ds_temp))//"s"
  !
END FUNCTION