A comprehensive Fortran module for handling date and time operations with robust error handling and formatting capabilities.
- Display current date, time, and day of week
- Show timezone information
- Calculate days left in the current year
- Leap year detection
- Error handling for invalid dates
- Properly formatted output
- Time difference calculations
fortran-datetime-utils/
├── .vscode/
│ └── tasks.json # VS Code build tasks
├── src/
│ ├── DateUtils.f90 # Date utilities module
│ └── DateTimeUtils.f90 # Date and time utilities module
├── compile.bat # Windows build script
├── compile.sh # Linux/macOS build script
├── Packing.f90 # Main program example
└── README.md # This file
- A Fortran compiler (gfortran 8.0+ recommended)
- Optional: Visual Studio Code with the Modern Fortran extension
This project can be built using VS Code with the provided tasks, or manually using gfortran:
- Open the project folder in VS Code
- Press
Ctrl+Shift+B
to build and run the program - To clean the build, select the "Clean Build" task from the Command Palette
# Run the build script
compile.bat
# Make the build script executable
chmod +x ./compile.sh
# Run the build script
./compile.sh
# Create module directory
mkdir -p build/mod
# Compile modules
gfortran -c -Jbuild/mod -Ibuild/mod -Wall -O3 src/DateUtils.f90
gfortran -c -Jbuild/mod -Ibuild/mod -Wall -O3 src/DateTimeUtils.f90
# Compile main program
gfortran -Ibuild/mod -Wall -O3 -o build/Packing src/DateUtils.f90 src/DateTimeUtils.f90 Packing.f90
# Run the program
./build/Packing # Linux/macOS
build\Packing # Windows
program DateTimeExample
use DateUtils
use DateTimeUtils
implicit none
type(DateTime) :: currentDateTime
integer :: status
! Get current date and time
status = GetDateTime(currentDateTime)
if (status == 0) then
! Format and display date information
print *, 'Current date: ', FormatDate(currentDateTime%year, currentDateTime%month, currentDateTime%day)
print *, 'Day of week: ', trim(currentDateTime%dayOfWeek)
end if
end program DateTimeExample
See the included Packing.f90
for a more comprehensive example that demonstrates additional features like time zone information and calculating days left in the year.
The DateUtils
module provides core date functionality:
- Day of week calculation
- Leap year detection
- Date validation
- Date formatting
The DateTimeUtils
module builds on DateUtils
to provide:
- DateTime type definition
- Current date/time retrieval
- Time formatting
- Timezone information
- Time difference calculations
This project is licensed under the MIT License - see the LICENSE file for details.
Created by Artem
February 2025