-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathexample_datetime_usage.f90
More file actions
88 lines (73 loc) · 2.98 KB
/
example_datetime_usage.f90
File metadata and controls
88 lines (73 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
program example_datetime
!! Demonstrate the stdlib_datetime module functionality.
use stdlib_datetime
implicit none
type(datetime_type) :: t1, t2, t3
type(timedelta_type) :: duration
integer :: stat
print '(A)', '=== stdlib_datetime Example ==='
print *
! 1. Get the current local time
t1 = now()
print '(A,A)', 'Current local time: ', format_datetime(t1)
! 2. Get the current UTC time
t2 = now_utc()
print '(A,A)', 'Current UTC time: ', format_datetime(t2)
! 3. Parse an ISO 8601 string
t2 = parse_datetime('2026-03-17T12:00:00Z', stat)
if (stat /= 0) then
print '(A)', 'ERROR: Failed to parse date string!'
stop 1
end if
print '(A,A)', 'Parsed datetime: ', format_datetime(t2)
! 4. Calculate the difference between two datetimes
duration = t1 - t2
print '(A,A)', 'Duration (t1-t2): ', &
format_timedelta(duration)
! 5. Add 30 days to a date
t3 = t2 + timedelta_type(30, 0, 0)
print '(A,A)', 'After adding 30d: ', format_datetime(t3)
! 6. Add mixed units (1 day, 6 hours, 30 minutes)
t3 = t2 + timedelta(days=1, hours=6, minutes=30)
print '(A,A)', 'After +1d 6h 30m: ', format_datetime(t3)
! 7. Calendar utilities
print *
print '(A)', '=== Calendar Utilities ==='
print '(A,L1)', 'Is 2024 leap year? ', is_leap_year(2024)
print '(A,L1)', 'Is 2026 leap year? ', is_leap_year(2026)
print '(A,I0)', 'Days in Feb 2024: ', &
days_in_month(2, 2024)
print '(A,I0)', 'Days in Feb 2026: ', &
days_in_month(2, 2026)
print '(A,I0)', 'Day of year (t2): ', day_of_year(t2)
print '(A,I0)', 'Day of week (t2): ', day_of_week(t2)
print '(A)', ' (1=Mon, 2=Tue, ..., 7=Sun)'
! 8. Comparison operators
print *
print '(A)', '=== Comparisons ==='
t2 = parse_datetime('2026-03-17T12:00:00Z')
t3 = parse_datetime('2026-03-18T12:00:00Z')
print '(A,L1)', 'Mar17 < Mar18? ', t2 < t3
print '(A,L1)', 'Mar17 == Mar17? ', t2 == t2
! Cross-timezone equality: 12:00 UTC == 17:30 IST
t2 = datetime_type(2026, 3, 17, 12, 0, 0, 0, 0)
t3 = datetime_type(2026, 3, 17, 17, 30, 0, 0, 330)
print '(A,L1)', '12:00Z == 17:30+05:30? ', t2 == t3
! 9. Compact format_timedelta (sub-second / sub-minute)
print *
print '(A)', '=== Compact Duration Formatting ==='
duration = timedelta(milliseconds=5)
print '(A,T40,A)', 'timedelta(ms=5):', format_timedelta(duration)
duration = timedelta(milliseconds=1500)
print '(A,T40,A)', 'timedelta(ms=1500):', format_timedelta(duration)
duration = timedelta(seconds=65)
print '(A,T40,A)', 'timedelta(s=65):', format_timedelta(duration)
duration = timedelta(hours=1, minutes=2, seconds=3)
print '(A,T40,A)', 'timedelta(1h 2m 3s):', format_timedelta(duration)
! 10. Unix epoch
print *
print '(A,A)', 'Unix epoch: ', &
format_datetime(epoch())
print *
print '(A)', 'Done!'
end program example_datetime