20.1: Date and Time
- Page ID
- 54387
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)The date and time functions are combined into a single system service call. The date and time values can be obtained as character strings, as integers, or both simultaneously. The options for date and time as explained in the next section followed by an example.
It must be noted that if the operating system has an incorrect date or time, the values returned to the program will also be incorrect.
Date and Time Options
The date and/or time values are are obtained from the operating system using the get_date_time()
system service call. The argument or arguments for the system service call must specify at least one of the following options:
date
= <character(8)>time
= <character(10)>zone
= <character(5)>values
= <integer values array>
As noted, each option must provide a location of where to place the results of the specified size and date type. The options are comma separated, similar to the read and write calls noted in a previous chapter. At least one argument must be included in the call.
The zone, or time zone, option will provide the time difference between local time and Coordinated Universal Time (UTC1). The character string will provide a result in hours:minutes format and the integer values will be in minutes only. However, the minutes can be easily converted to hours.
The options and associated values returned as more fully described in the following table.
Option | Data Type | Description |
---|---|---|
date | character(8) | The string returned will be in the form YYYYMMDD, where YYYY is year, MM is month, and DD is date. |
time | character(10) | The string returned will be in the form HHMMSS.SSS where HH is hour, MM is minute, SS is second, and SSS is milliseconds. |
zone | character(5) | The string returned will be in the form of \(\pm\)HHMM, where HHMM is the time difference between local time and Coordination Universal Time. |
values | integer array, 8 elements | The values will be returned in the 8 value integer array as follows:
|
Each argument is optional, but at least one argument must be included. Multiple arguments are allowed.
Date and Time Example Program
The following as an example program that obtains the date and time information from the operating system in various formats. The final results are shown for reference.
! Example program to obtain the date and time from the system. program timeDateExample ! ---------- ! Declarations. implicit none integer, dimension(8) :: valuesArr character(len=8) :: today character(len=10) :: now character(len=5) :: myzone integer :: i ! ---------- ! Display simple header. write (*,'(a)') & "Example Program for Date and Time Functions." ! ---------- ! Get date, time, and zone from system as characters. ! Display to screen for reference. call date_and_time(date=today) write (*,'(/a, a)') "Today is: ", today call date_and_time(time=now, zone=myzone) write (*,'(a, a)') "Time is: ", now write (*,'(a, a/)') "Time Zone is: ", myzone ! ---------- ! Get all date values from the system as integers. ! Display to screen for reference. call date_and_time(values=valuesArr) write (*,'(a)') "Values Array:" write (*,'(a, i4)') "Date, Year: ", valuesArr(1) write (*,'(a, i2)') "Date, Month: ", valuesArr(2) write (*,'(a, i2)') "Date, Day: ", valuesArr(3) write (*,'(a, i2)') "Time, Hour: ", valuesArr(5) write (*,'(a, i2)') "Time, Minutes: ", valuesArr(6) write (*,'(a, i2)') "Time, Seconds: ", valuesArr(7) write (*,'(a, i3)') "Time, Millseconds: ", valuesArr(8) write (*,'(/,a, i8)') & "Time difference with UTC in minutes: ", & valuesArr(4) write (*,'(a, i2, a1, i2.2 ,/)') & "Time difference with UTC in hours: ", & valuesArr(4)/60, ":", mod(valuesArr(4), 60) end program timeDateExample
While this program does not really use the time or date values for anything meaningful, it does provide an example of how the information is obtained for use in other, more complex programs. The output of this example program is shown as follows:
Today is: 20131212 Time is: 154032.491 Time Zone is: -0800 Values Array: Date, Year: 2013 Date, Month: 12 Date, Day: 12 Time, Hour: 15 Time, Minutes: 40 Time, Seconds: 32 Time, Millseconds: 491 Time difference with UTC in minutes: -480 Time difference with UTC in hours: -8:00
The UTC for Las Vegas, Nevada is indeed, -8 hours as shown. The results for the UTC will be based on the actual geographic location of where the system executing the program is located.
- For more information regarding coordinated universal time, refer to: http://en.Wikipedia.org/wiki/Coordinated_Universal_Time