  module datetime
  implicit none
  
  Contains
!-------------------------------------------------------------------------------
! Uses built-in f90 subroutine date_and_time.
! Return the current date in this format:
! date: 05 sep 2000
! time:    13.52.58
!-------------------------------------------------------------------------------
  subroutine date_time(date,time)
   character(len=11) :: date
   character(len=8)  :: today
   character(len=11) :: now
   character(len=8) :: time

    call date_and_time(today,now)
    date = today(7:8) // ' ' // month(today(5:6)) // ' ' // today(1:4)
    time = now(1:2) // '.' // now(3:4) // '.' // now(5:6)
  end  subroutine date_time
  
!-------------------------------------------------------------------------------
! Returns the character-representation of the month.
!-------------------------------------------------------------------------------
  function month(mo)
   character(len=3) :: month
   character(len=2) :: mo

   select case (mo)
	case ("01") ; month = 'jan'; case ("02") ; month = 'feb';
	case ("03") ; month = 'mar'; case ("04") ; month = 'apr';
	case ("05") ; month = 'may'; case ("06") ; month = 'jun';
	case ("07") ; month = 'jul'; case ("08") ; month = 'aug';
	case ("09") ; month = 'sep'; case ("10") ; month = 'oct';
	case ("11") ; month = 'nov'; case ("12") ; month = 'dec';
	case default
	write (*,*) 'Oops, something went wrong with the date: 1'; stop
   end select
  end function month
  end module datetime

!-----------------------------------------------------------
!  main program
!-----------------------------------------------------------
  program writedate
!-----------------------------------------------------------
!   writes date and time at the beginning of output file
!-----------------------------------------------------------
  use datetime
  implicit none
  character(len=8) :: time
  character(len=11) :: date
  integer ::  output_unit=10

     open(output_unit, file='output')
     call  date_time(date,time)
     write (output_unit, '("# date: ",A11)') date
     write (output_unit, '("# time: ",A11)') time

  end program writedate

