stdlib icon indicating copy to clipboard operation
stdlib copied to clipboard

Implement stdlib_datetime - module for Dates, Times, and Time Deltas

Open JAi-SATHVIK opened this issue 8 months ago • 2 comments

Motivation

Motivation

Working with dates, times, and time intervals is a fundamental requirement in almost all modern scientific computing disciplines (e.g., parsing climate datasets, writing logs, calculating intervals in financial models, astrophysical simulations).

Presently, Fortran provides the date_and_time() intrinsic subroutine, which returns an integer array values(1:8) containing components like year, month, day, hour, etc. However, performing any mathematical operations on these dates—such as adding 90 days to a timestamp or finding the total seconds between two events—forces the developer to write complex calendar logic from scratch to handle leap years, varying month lengths, and timezones.

I propose the addition of a comprehensive stdlib_datetime module to provide a high-level representation of Dates, Times, and Time Deltas (Intervals). This feature would bridge a significant gap with other modern languages like Python (datetime), C++ (std::chrono), and Java (java.time).

Proposed Interfaces and Derived Types

To provide robust time handling, the module would likely define three primary derived types:

  1. datetime_type: Represents a specific point in time (Year, Month, Day, Hour, Minute, Second, Millisecond).
  2. timedelta_type: Represents a duration or an interval of time (Days, Seconds, Milliseconds).
  3. date_type / time_type: Smaller sub-types if separating only the calendar date from the clock time is deemed necessary.

Basic Usage Example:

use stdlib_datetime, only: datetime_type, timedelta_type, now, parse_date

type(datetime_type) :: t1, t2
type(timedelta_type) :: duration

! Get the current local time
t1 = now()

! Parse an ISO 8601 string easily
t2 = parse_date('2026-03-17T12:00:00Z')

! Calculate the difference between them
duration = t1 - t2

! Add an interval (e.g., 30 days) to a date
t2 = t2 + timedelta_type(days=30)

Required Functionality:

  • Constructors: from year/month/day components, from intrinsic date_and_time arrays, and from ISO 8601 strings.
  • Overloaded Operators (+, -, ==, <, >): For adding timedelta_type to datetime_type, subtracting dates to get an interval, and comparing dates.
  • Component Extraction: Helper functions to extract specific elements (e.g., t%year(), t%day_of_week(), t%is_leap_year()).
  • Formatting: Easy output to strings ('YYYY-MM-DD HH:MM:SS').

Prior Art

  • Python: The datetime module is universally used to structure Python time calculations.
  • C++: The <chrono> library provides durations, clocks, and time points.
  • MATLAB: The datetime and duration arrays.
  • Fortran libraries: There are various custom implementations scattered across GitHub (e.g., M. Hirsch's datetime-fortran), proving the extreme community demand for a standardized, native approach.

Additional Information

Implementing this module requires no complex C-bindings and no parameterized derived types (PDTs) (meaning compiler support will be near 100%). It is purely mathematical (Julian day calculations) and string manipulation, meaning it is easy to implement yet incredibly high impact for anyone doing data-wrangling in Fortran.

JAi-SATHVIK avatar Mar 17 '26 07:03 JAi-SATHVIK

I've raised a draft PR #1148 for the date time module implementation.

JAi-SATHVIK avatar Mar 17 '26 21:03 JAi-SATHVIK

Following the merge of PR #1148, we want to add two new types to make the library easier to use for simple tasks.

Goal

Right now, we only have a full datetime_type (Date + Time). We want to add smaller types for when you only need one or the other. This makes the code faster and easier to read.

New Type: date_type

  • What it is: Just the Year, Month, and Day (eg 2026-04-06).
  • Use cases: Birthdays, holidays, or daily logs.
  • Features: Leap year checks, finding the day of the week, and adding/subtracting days.

New Type: time_type

  • What it is: Just the clock time (eg 12:00:00).
  • Use cases: Daily schedules or timers.
  • Features: Handles 24-hour time (it "wraps around" at midnight) and supports timezone offsets.

Interaction with datetime_type

Composition: type(datetime_type) :: dt = datetime(my_date, my_time) Decomposition: my_date = dt%date() and my_time = dt%time() (or equivalent wrapper functions).

Proposed API Additions

! Constructors
type(date_type) :: d
type(time_type) :: t

d = date(2026, 4, 6)
t = time(12, 0, 0)

! Comparison
if (d1 < d2) print *, "d1 is earlier"

! Arithmetic
d = d + timedelta(days=30)  ! Adds 30 days
t = t + timedelta(hours=25) ! Wraps to 13:00 (1 hour later)

! Extraction from full datetime
type(datetime_type) :: now_dt
now_dt = now()
d = get_date(now_dt)
t = get_time(now_dt)

JAi-SATHVIK avatar Apr 05 '26 20:04 JAi-SATHVIK