0.6.2-dev0
FORCES
FORtran lib for Comp. Env. Sys.
Loading...
Searching...
No Matches
mo_datetime.f90 File Reference

Types to deal with datetimes. More...

Go to the source code of this file.

Data Types

module  mo_datetime::puredate
 This is a container to hold only a date. More...
 
module  mo_datetime::puretime
 This is a container to hold only a time. More...
 
module  mo_datetime::datetime
 This is a container to hold a date-time. More...
 
module  mo_datetime::timedelta
 This is a container to hold a defined time span. More...
 

Modules

module  mo_datetime
 Types to deal with datetimes.
 

Functions/Subroutines

pure type(timedelta) function, public mo_datetime::zero_delta ()
 zero time delta
 
pure type(timedelta) function, public mo_datetime::one_week ()
 one week time delta
 
pure type(timedelta) function, public mo_datetime::one_day ()
 one day time delta
 
pure type(timedelta) function, public mo_datetime::one_hour ()
 one hour time delta
 
pure type(timedelta) function, public mo_datetime::one_minute ()
 one minute time delta
 
pure type(timedelta) function, public mo_datetime::one_second ()
 one second time delta
 
pure type(puretime) function, public mo_datetime::midnight ()
 midnight (00:00)
 
pure type(puretime) function, public mo_datetime::midday ()
 midday (12:00)
 
pure type(puretime) function, public mo_datetime::day_hour (hour)
 time for given hour
 
type(datetime) function, public mo_datetime::now ()
 get current datetime
 
type(puredate) function, public mo_datetime::today ()
 get todays puredate
 
type(puretime) function, public mo_datetime::currently ()
 get current puretime
 
pure integer(i4) function mo_datetime::weekday (year, month, day)
 day of the week
 
pure logical function, public mo_datetime::is_leap_year (year)
 whether a given year is a leap year
 
pure integer(i4) function, public mo_datetime::days_in_month (year, month)
 number of days in a given month
 
pure integer(i4) function, public mo_datetime::days_in_year (year)
 number of days in a given year
 
pure integer(i4) function mo_datetime::days_before_year (year)
 number of days before a given year since year 1
 
pure subroutine mo_datetime::doy_to_month_day (year, doy, month, day)
 get date from day of the year
 
subroutine mo_datetime::check_year (year)
 check if a given year is valid
 
subroutine mo_datetime::check_month (month)
 check if a given month is valid
 
subroutine mo_datetime::check_day (year, month, day)
 check if a given day is valid
 
subroutine mo_datetime::check_hour (hour)
 check if a given hour is valid
 
subroutine mo_datetime::check_minute (minute)
 check if a given minute is valid
 
subroutine mo_datetime::check_second (second)
 check if a given second is valid
 
subroutine mo_datetime::check_datetime (year, month, day, hour, minute, second)
 check if a datetime is valid
 
type(datetime) function mo_datetime::dt_init (year, month, day, hour, minute, second)
 initialize a datetime
 
type(datetime) function mo_datetime::dt_from_string (string)
 datetime from string
 
type(datetime) function mo_datetime::dt_from_cf (string, value)
 datetime from cf-string and value
 
pure type(datetime) function mo_datetime::dt_from_date_time (in_date, in_time)
 datetime from date and time
 
pure type(datetime) function mo_datetime::dt_from_julian (julian, calendar)
 datetime from fractional julian day
 
type(datetime) function mo_datetime::dt_replace (this, year, month, day, hour, minute, second)
 new datetime with specified fields
 
pure subroutine mo_datetime::dt_copy_dt (this, that)
 copy a datetime
 
pure subroutine mo_datetime::dt_copy_d (this, that)
 copy a datetime from a date
 
pure type(puredate) function mo_datetime::get_date (this)
 date of the datetime
 
pure type(puretime) function mo_datetime::get_time (this)
 time of the datetime
 
pure character(19) function mo_datetime::dt_str (this)
 string representation of the datetime
 
pure real(dp) function mo_datetime::dt_julian (this, calendar)
 datetime as fractional julian day
 
pure integer(i4) function mo_datetime::dt_weekday (this)
 day of the week
 
pure integer(i4) function mo_datetime::dt_doy (this)
 day of the year
 
pure logical function mo_datetime::is_new_year (this)
 datetime is a new year
 
pure logical function mo_datetime::is_new_month (this)
 datetime is a new month
 
pure logical function mo_datetime::is_new_week (this)
 datetime is a new week
 
pure logical function mo_datetime::is_new_day (this)
 datetime is a new day
 
pure logical function mo_datetime::is_new_hour (this)
 datetime is a new hour
 
pure logical function mo_datetime::is_new_minute (this)
 datetime is a new month
 
pure logical function mo_datetime::dt_eq (this, that)
 equal comparison of datetimes
 
pure logical function mo_datetime::dt_eq_d (this, that)
 equal comparison of datetime and date
 
pure logical function mo_datetime::dt_neq (this, that)
 not equal comparison of datetimes
 
pure logical function mo_datetime::dt_neq_d (this, that)
 not equal comparison of datetime and date
 
pure logical function mo_datetime::dt_lt (this, that)
 less than comparison of datetimes
 
pure logical function mo_datetime::dt_lt_d (this, that)
 less than comparison of datetime and date
 
pure logical function mo_datetime::dt_gt (this, that)
 greater than comparison of datetimes
 
pure logical function mo_datetime::dt_gt_d (this, that)
 greater than comparison of datetime and date
 
pure logical function mo_datetime::dt_leq (this, that)
 less than or equal comparison of datetimes
 
pure logical function mo_datetime::dt_leq_d (this, that)
 less than or equal comparison of datetime and date
 
pure logical function mo_datetime::dt_geq (this, that)
 greater than or equal comparison of datetimes
 
pure logical function mo_datetime::dt_geq_d (this, that)
 less than or equal comparison of datetime and date
 
pure type(datetime) function mo_datetime::dt_add_td (this, that)
 add a timedelta to a datetime
 
pure type(datetime) function mo_datetime::td_add_dt (that, this)
 add a timedelta to a datetime
 
pure type(datetime) function mo_datetime::dt_sub_td (this, that)
 subtract a timedelta from a datetime
 
pure type(timedelta) function mo_datetime::dt_sub_dt (this, that)
 difference between two datetimes
 
pure type(timedelta) function mo_datetime::dt_sub_d (this, that)
 difference between datetime and date
 
type(puredate) function mo_datetime::d_init (year, month, day)
 initialize a date
 
type(puredate) function mo_datetime::d_from_string (string)
 date from string
 
pure type(puredate) function mo_datetime::d_from_julian (julian, calendar)
 date from fractional julian day
 
type(puredate) function mo_datetime::d_replace (this, year, month, day)
 new date with specified fields
 
pure type(datetime) function mo_datetime::to_datetime (this)
 convert date to a datetime
 
pure integer(i4) function mo_datetime::to_ordinal (this)
 convert date to number of days since year 1
 
pure character(10) function mo_datetime::d_str (this)
 string representation of the date
 
pure real(dp) function mo_datetime::d_julian (this, calendar)
 date as fractional julian day
 
pure integer(i4) function mo_datetime::d_weekday (this)
 day of the week
 
pure integer(i4) function mo_datetime::d_doy (this)
 day of the year
 
pure logical function mo_datetime::d_is_new_year (this)
 date is a new year
 
pure logical function mo_datetime::d_is_new_month (this)
 date is a new month
 
pure logical function mo_datetime::d_is_new_week (this)
 date is a new week
 
pure logical function mo_datetime::d_eq (this, that)
 equal comparison of dates
 
pure logical function mo_datetime::d_eq_dt (this, that)
 equal comparison of date and datetime
 
pure logical function mo_datetime::d_neq (this, that)
 not equal comparison of dates
 
pure logical function mo_datetime::d_neq_dt (this, that)
 not equal comparison of date and datetime
 
pure logical function mo_datetime::d_lt (this, that)
 less than comparison of dates
 
pure logical function mo_datetime::d_lt_dt (this, that)
 less than comparison of date and datetime
 
pure logical function mo_datetime::d_gt (this, that)
 greater than comparison of dates
 
pure logical function mo_datetime::d_gt_dt (this, that)
 greater than comparison of date and datetime
 
pure logical function mo_datetime::d_leq (this, that)
 less than or equal comparison of dates
 
pure logical function mo_datetime::d_leq_dt (this, that)
 less than or equal comparison of date and datetime
 
pure logical function mo_datetime::d_geq (this, that)
 greater than or equal comparison of dates
 
pure logical function mo_datetime::d_geq_dt (this, that)
 greater than or equal comparison of date and datetime
 
pure type(puredate) function mo_datetime::d_add_td (this, that)
 add a timedelta to a date
 
pure type(puredate) function mo_datetime::td_add_d (that, this)
 add a timedelta to a date
 
pure type(puredate) function mo_datetime::d_sub_td (this, that)
 subtract a timedelta from a date
 
pure type(timedelta) function mo_datetime::d_sub_d (this, that)
 difference between two dates
 
pure type(timedelta) function mo_datetime::d_sub_dt (this, that)
 difference between date and datetime
 
type(puretime) function mo_datetime::t_init (hour, minute, second)
 initialize a time
 
type(puretime) function mo_datetime::t_from_string (string)
 time from string
 
pure type(puretime) function mo_datetime::t_from_day_second (day_second)
 time from day second
 
pure subroutine mo_datetime::t_copy (this, that)
 copy a time
 
type(puretime) function mo_datetime::t_replace (this, hour, minute, second)
 new time with specified fields
 
pure character(8) function mo_datetime::t_str (this)
 string representation of the time
 
pure integer(i4) function mo_datetime::t_day_second (this)
 time to second of the day
 
pure logical function mo_datetime::t_is_new_day (this)
 time is a new day / midnight
 
pure logical function mo_datetime::t_is_new_hour (this)
 time is a new hour
 
pure logical function mo_datetime::t_is_new_minute (this)
 time is a new month
 
pure logical function mo_datetime::t_eq (this, that)
 equal comparison of times
 
pure logical function mo_datetime::t_neq (this, that)
 not equal comparison of times
 
pure logical function mo_datetime::t_lt (this, that)
 less than comparison of times
 
pure logical function mo_datetime::t_gt (this, that)
 greater than comparison of times
 
pure logical function mo_datetime::t_leq (this, that)
 less than or equal comparison of times
 
pure logical function mo_datetime::t_geq (this, that)
 greater than or equal comparison of times
 
pure type(puretime) function mo_datetime::t_add_td (this, that)
 add a timedelta to a time
 
pure type(puretime) function mo_datetime::td_add_t (that, this)
 add a timedelta to a time
 
pure type(puretime) function mo_datetime::t_sub_td (this, that)
 subtract a timedelta from a time
 
pure type(timedelta) function mo_datetime::t_sub_t (this, that)
 difference between two times
 
pure type(timedelta) function mo_datetime::td_init (days, seconds, minutes, hours, weeks)
 initialize a timedelta
 
pure type(timedelta) function mo_datetime::td_abs (this)
 absolute timedelta
 
pure integer(i8) function mo_datetime::td_total_seconds (this)
 timedelta in seconds (may need i8)
 
pure type(timedelta) function mo_datetime::from_total_seconds (total_seconds)
 
pure subroutine mo_datetime::td_copy (this, that)
 copy a timedelta
 
pure logical function mo_datetime::td_eq (this, that)
 equal comparison of timedeltas
 
pure logical function mo_datetime::td_neq (this, that)
 not equal comparison of timedeltas
 
pure logical function mo_datetime::td_lt (this, that)
 less than comparison of timedeltas
 
pure logical function mo_datetime::td_gt (this, that)
 greater than comparison of timedeltas
 
pure logical function mo_datetime::td_leq (this, that)
 less than or equal comparison of timedeltas
 
pure logical function mo_datetime::td_geq (this, that)
 greater than or equal comparison of timedeltas
 
pure type(timedelta) function mo_datetime::td_add (this, that)
 adding two timedeltas
 
pure type(timedelta) function mo_datetime::td_sub (this, that)
 adding two timedeltas
 
pure type(timedelta) function mo_datetime::td_neg (this)
 negative timedelta
 
pure type(timedelta) function mo_datetime::td_pos (this)
 positive timedelta
 
pure type(timedelta) function mo_datetime::td_mul1 (this, that)
 multiply a timedelta with an integer
 
pure type(timedelta) function mo_datetime::td_mul2 (that, this)
 multiply a timedelta with an integer
 
pure type(timedelta) function mo_datetime::td_mul1_dp (this, that)
 multiply a timedelta with a real
 
pure type(timedelta) function mo_datetime::td_mul2_dp (that, this)
 multiply a timedelta with a real
 
pure type(timedelta) function mo_datetime::td_div (this, that)
 divide a timedelta by an integer
 
pure type(timedelta) function mo_datetime::td_div_dp (this, that)
 divide a timedelta by a real
 
pure real(dp) function mo_datetime::td_div_td (this, that)
 divide a timedelta by a timedelta
 

Variables

integer(i4), parameter, public mo_datetime::year_days = 365_i4
 days in standard year
 
integer(i4), parameter, public mo_datetime::leap_year_days = 366_i4
 days in leap year
 
integer(i4), parameter, public mo_datetime::year_months = 12_i4
 months in year
 
integer(i4), parameter, public mo_datetime::week_days = 7_i4
 days in week
 
integer(i4), parameter, public mo_datetime::day_hours = 24_i4
 hours in day
 
integer(i4), parameter, public mo_datetime::clock_hours = 12_i4
 hours on a clock
 
integer(i4), parameter, public mo_datetime::hour_minutes = 60_i4
 minutes in hour
 
integer(i4), parameter, public mo_datetime::minute_seconds = 60_i4
 seconds in minute
 
integer(i4), parameter, public mo_datetime::day_minutes = DAY_HOURS * HOUR_MINUTES
 minutes in day
 
integer(i4), parameter, public mo_datetime::day_seconds = DAY_MINUTES * MINUTE_SECONDS
 seconds in day
 
integer(i4), parameter, public mo_datetime::hour_seconds = HOUR_MINUTES * MINUTE_SECONDS
 seconds in hour
 
integer(i4), parameter, public mo_datetime::week_hours = WEEK_DAYS * DAY_HOURS
 hours in week
 
integer(i4), parameter, public mo_datetime::week_minutes = WEEK_DAYS * DAY_MINUTES
 minutes in week
 
integer(i4), parameter, public mo_datetime::week_seconds = WEEK_DAYS * DAY_SECONDS
 seconds in week
 
integer(i4), parameter mo_datetime::min_year = 1_i4
 minimum for year
 
integer(i4), parameter mo_datetime::max_year = 9999_i4
 maximum for year
 

Detailed Description

Types to deal with datetimes.

This module provides four types to deal with date and time

  1. puredate : containing year, month and day
  2. puretime : containing hour, minute and second
  3. datetime : combination of date and time
  4. timedelta : difference between two datetimes (or dates) in days and (sub-day) seconds

These type can be used in arithmetic operations (+, -, *, /) and can be compared (<, >, <=, >=, ==, /=) where it makes sense.

The following example demonstrates the functionality:

program main
implicit none
type(datetime) :: date1, date2, date3, date4, date5
type(puredate) :: day1
type(puretime) :: time1
type(timedelta) :: delta1
! create dates add time-deltas
date1 = datetime(2000, 2, 28)
date2 = date1 + one_day()
print*, date2%str()
date3 = date1 + 2 * one_day()
print*, date3%str()
! substract half a day
delta1 = one_day() / 2
date4 = date3 - delta1
! compare dates/times
print*, "is midday: ", date4%time() == midday()
print*, "date4 after date2: ", date4 > date2
! create from date and time
date5 = datetime(date1%date(), date4%time())
print*, date5%str()
! create from datetime string
date5 = datetime("2023-05-08 12:32:30")
day1 = date("2023-05-08")
time1 = time("12:32:30")
print*, date5 == time1%with_date(day1)
print*, date5 == day1%with_time(time1)
print*, date5 == datetime(day1, time1)
! use cf-convention string and value
date5 = datetime("seconds since 1992-10-8 15:15:42", day_seconds - hour_seconds)
print*, date5%str()
end program main
Types to deal with datetimes.
pure type(puretime) function, public midday()
midday (12:00)
integer(i4), parameter, public day_seconds
seconds in day
pure type(timedelta) function, public one_day()
one day time delta
integer(i4), parameter, public hour_seconds
seconds in hour
This is a container to hold a date-time.
This is a container to hold only a date.
This is a container to hold only a time.
This is a container to hold a defined time span.

Several special constants are provided as well:

  • integer constants for duration ratios:
    • year_days : days in standard year (365)
    • leap_year_days : days in leap year (366)
    • year_months : months in year (12)
    • week_days : days in week (7)
    • day_hours : hours in day (24)
    • hour_minutes : minutes in hour (60)
    • minute_seconds : seconds in minute (60)
    • day_minutes : minutes in day (1440)
    • day_seconds : seconds in day (86400)
    • hour_seconds : seconds in hour (3600)
    • week_hours : hours in week (168)
    • week_minutes : minutes in week (10080)
    • week_seconds : seconds in week (604800)

Provided convenience routines:

  • midnight and midday : puretime for special day times
  • day_hour : puretime for each hour of the day (0-23)
  • zero_delta , one_week , one_day , one_hour , one_minute and one_second : special timedelta values
  • currently :
  • today :
  • now :
  • is_leap_year :
  • days_in_month :
  • days_in_year :

A date is assumed to be given in the gregorian calender. That means, there is a leap year (February has 29 days instead of 28) if:

  • year is divisible by 4
  • year is not divisible by 100 or it is divisible by 400
Note
Dates before 1582-10-15 should be used with caution. The gregorian calender replaced the julian calender and advanced the date by 10 days: Thursday 4 October 1582 was followed by Friday 15 October 1582. Using this module for erlier dates will assume the proleptic gregorian calendar.
Version
0.1
Authors
Sebastian Mueller
Date
May 2023

Definition in file mo_datetime.f90.