Skip to content

Commit a00e95f

Browse files
authored
Merge pull request #68 from scivision/usebug
allow work on Windows that lacks strptime
2 parents 897166f + 120a1b8 commit a00e95f

File tree

5 files changed

+149
-122
lines changed

5 files changed

+149
-122
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@ Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include
1616
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
1717
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
1818

19+
if(WIN32)
20+
enable_language(CXX)
21+
add_library(strptime OBJECT strptime.cpp)
22+
set_target_properties(strptime PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED true)
23+
target_link_libraries(datetime PRIVATE strptime)
24+
endif()
25+
1926
# tests
2027
add_executable(datetime_tests src/tests/datetime_tests.f90)
2128
target_link_libraries(datetime_tests datetime)
2229
set_target_properties(datetime_tests PROPERTIES
23-
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
30+
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
31+
LINKER_LANGUAGE Fortran)
2432
add_test(NAME datetime_tests COMMAND $<TARGET_FILE:datetime_tests>)

src/lib/datetime.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
! datetime-fortran - A Fortran library for date and time manipulation
33
! Copyright (c) 2013-2017, Wavebit Scientific LLC
44
! All rights reserved.
5-
!
5+
!
66
! Licensed under the BSD-3 clause license. See LICENSE for details.
77
!
88
module datetime_module

src/lib/mod_datetime.f90

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module mod_datetime
8585
procedure,pass(d0),private :: datetime_plus_timedelta
8686
procedure,pass(d0),private :: timedelta_plus_datetime
8787
procedure,pass(d0),private :: datetime_minus_datetime
88-
procedure,pass(d0),private :: datetime_minus_timedelta
88+
procedure,pass(d0),private :: datetime_minus_timedelta
8989
procedure,pass(d0),private :: eq
9090
procedure,pass(d0),private :: neq
9191
procedure,pass(d0),private :: gt
@@ -123,14 +123,14 @@ pure elemental type(datetime) function datetime_constructor(year,month,&
123123
integer, intent(in),optional :: year !! year
124124
integer, intent(in),optional :: month !! month
125125
integer, intent(in),optional :: day !! day
126-
integer, intent(in),optional :: hour !! hour
126+
integer, intent(in),optional :: hour !! hour
127127
integer, intent(in),optional :: minute !! minute
128128
integer, intent(in),optional :: second !! second
129129
integer, intent(in),optional :: millisecond !! millisecond
130130
real(kind=real64),intent(in),optional :: tz !! timezone offset in hours
131131

132132
if(present(year))then
133-
datetime_constructor % year = year
133+
datetime_constructor % year = year
134134
else
135135
datetime_constructor % year = 1
136136
endif
@@ -181,7 +181,7 @@ pure elemental type(datetime) function datetime_constructor(year,month,&
181181

182182

183183

184-
! datetime getters
184+
! datetime getters
185185
!=======================================================================
186186

187187
pure elemental integer function getYear(self)
@@ -273,12 +273,12 @@ pure elemental subroutine addMilliseconds(self,ms)
273273
endsubroutine addMilliseconds
274274

275275

276-
! datetime-bound methods
276+
! datetime-bound methods
277277
!=======================================================================
278278

279279
pure elemental subroutine addSeconds(self,s)
280280

281-
!! Adds an integer number of seconds to self. Called by `datetime`
281+
!! Adds an integer number of seconds to self. Called by `datetime`
282282
!! addition (`+`) and subtraction (`-`) operators.
283283

284284
class(datetime),intent(inout) :: self !! `datetime` instance
@@ -304,7 +304,7 @@ pure elemental subroutine addSeconds(self,s)
304304

305305
pure elemental subroutine addMinutes(self,m)
306306

307-
!! Adds an integer number of minutes to self. Called by `datetime`
307+
!! Adds an integer number of minutes to self. Called by `datetime`
308308
!! addition (`+`) and subtraction (`-`) operators.
309309

310310
class(datetime),intent(inout) :: self !! `datetime` instance
@@ -330,7 +330,7 @@ pure elemental subroutine addMinutes(self,m)
330330

331331
pure elemental subroutine addHours(self,h)
332332

333-
!! Adds an integer number of hours to self. Called by `datetime`
333+
!! Adds an integer number of hours to self. Called by `datetime`
334334
!! addition (`+`) and subtraction (`-`) operators.
335335

336336
class(datetime),intent(inout) :: self !! `datetime` instance
@@ -356,7 +356,7 @@ pure elemental subroutine addHours(self,h)
356356

357357
pure elemental subroutine addDays(self,d)
358358

359-
!! Adds an integer number of dayss to self. Called by `datetime`
359+
!! Adds an integer number of dayss to self. Called by `datetime`
360360
!! addition (`+`) and subtraction (`-`) operators.
361361

362362
class(datetime),intent(inout) :: self !! `datetime` instance
@@ -383,7 +383,7 @@ pure elemental subroutine addDays(self,d)
383383
self % day = self % day+daysInMonth(self % month,self % year)
384384
else
385385
exit
386-
endif
386+
endif
387387
enddo
388388

389389
endsubroutine addDays
@@ -395,7 +395,7 @@ pure elemental character(len=23) function isoformat(self,sep)
395395
!! Returns character string with time in ISO 8601 format.
396396

397397
class(datetime), intent(in) :: self !! `datetime instance`
398-
character(len=1),intent(in),optional :: sep
398+
character(len=1),intent(in),optional :: sep
399399
!! separator character, 'T' is default
400400

401401
character(len=1) :: separator
@@ -406,8 +406,8 @@ pure elemental character(len=23) function isoformat(self,sep)
406406
separator = 'T'
407407
endif
408408

409-
! TODO below is a bit cumbersome and was implemented
410-
! at a time before the interface to strftime. Now we
409+
! TODO below is a bit cumbersome and was implemented
410+
! at a time before the interface to strftime. Now we
411411
! could do something like:
412412
!
413413
! isoformat = self % strftime('%Y-%m-%d'//separator//'%H:%M:%S')
@@ -426,8 +426,8 @@ pure elemental character(len=23) function isoformat(self,sep)
426426

427427
pure elemental logical function isValid(self)
428428

429-
!! Checks whether the `datetime` instance has valid component values.
430-
!! Returns `.true.` if the `datetime` instance is valid, and `.false.`
429+
!! Checks whether the `datetime` instance has valid component values.
430+
!! Returns `.true.` if the `datetime` instance is valid, and `.false.`
431431
!! otherwise.
432432

433433
class(datetime),intent(in) :: self !! `datetime` instance
@@ -450,23 +450,23 @@ pure elemental logical function isValid(self)
450450
isValid = .false.
451451
return
452452
endif
453-
454-
if(self % hour < 0 .or. self % hour > 23)then
453+
454+
if(self % hour < 0 .or. self % hour > 23)then
455455
isValid = .false.
456456
return
457457
endif
458458

459-
if(self % minute < 0 .or. self % minute > 59)then
459+
if(self % minute < 0 .or. self % minute > 59)then
460460
isValid = .false.
461461
return
462462
endif
463463

464-
if(self % second < 0 .or. self % second > 59)then
464+
if(self % second < 0 .or. self % second > 59)then
465465
isValid = .false.
466466
return
467467
endif
468468

469-
if(self % millisecond < 0 .or. self % millisecond > 999)then
469+
if(self % millisecond < 0 .or. self % millisecond > 999)then
470470
isValid = .false.
471471
return
472472
endif
@@ -477,7 +477,7 @@ pure elemental logical function isValid(self)
477477

478478
type(datetime) function now()
479479

480-
!! Returns a `datetime` instance with current time.
480+
!! Returns a `datetime` instance with current time.
481481
!! No input arguments.
482482

483483
character(len=5) :: zone
@@ -507,7 +507,7 @@ type(datetime) function now()
507507

508508
pure elemental integer function weekday(self)
509509

510-
!! Returns the day of the week calculated using Zeller's congruence.
510+
!! Returns the day of the week calculated using Zeller's congruence.
511511
!! Returned value is an integer scalar in the range [0-6], such that:
512512
!!
513513
!! 0: Sunday
@@ -558,7 +558,7 @@ pure elemental integer function isoweekday(self)
558558
class(datetime),intent(in) :: self !! `datetime` instance
559559

560560
isoweekday = self % weekday()
561-
561+
562562
if (isoweekday == 0) then
563563
isoweekday = 7
564564
end if
@@ -632,8 +632,8 @@ pure elemental character(len=3) function isoweekdayShort(self)
632632

633633
function isocalendar(self)
634634

635-
!! Returns an array of 3 integers, year, week number, and week day,
636-
!! as defined by ISO 8601 week date. Essentially a wrapper around C
635+
!! Returns an array of 3 integers, year, week number, and week day,
636+
!! as defined by ISO 8601 week date. Essentially a wrapper around C
637637
!! `strftime` function.
638638

639639
class(datetime),intent(in) :: self !! `datetime` instance
@@ -644,7 +644,7 @@ function isocalendar(self)
644644
character(len=20) :: string
645645

646646
rc = c_strftime(string,len(string),'%G %V %u'//c_null_char,&
647-
self % tm())
647+
self % tm())
648648

649649
read(unit=string(1:4),fmt='(I4)')year
650650
read(unit=string(6:7),fmt='(I2)')week
@@ -658,10 +658,10 @@ function isocalendar(self)
658658

659659
integer function secondsSinceEpoch(self)
660660

661-
!! Returns an integer number of seconds since the UNIX Epoch,
662-
!! `1970-01-01 00:00:00`. Note that this is a wrapper around C's
663-
!! `strftime('%s')`, so the number of seconds will reflect the time
664-
!! zone of the local machine on which the function is being called.
661+
!! Returns an integer number of seconds since the UNIX Epoch,
662+
!! `1970-01-01 00:00:00`. Note that this is a wrapper around C's
663+
!! `strftime('%s')`, so the number of seconds will reflect the time
664+
!! zone of the local machine on which the function is being called.
665665

666666
class(datetime),intent(in) :: self !! `datetime` instance
667667

@@ -748,7 +748,7 @@ pure elemental character(len=5) function tzOffset(self)
748748

749749
pure elemental type(datetime) function utc(self)
750750

751-
!! Returns the `datetime` instance at Coordinated Universal Time (UTC).
751+
!! Returns the `datetime` instance at Coordinated Universal Time (UTC).
752752

753753
class(datetime),intent(in) :: self !! `datetime` instance
754754

@@ -890,8 +890,8 @@ pure elemental function datetime_minus_datetime(d0,d1) result(t)
890890

891891
pure elemental logical function gt(d0,d1)
892892

893-
!! `datetime` comparison operator that eturns `.true.` if `d0` is
894-
!! greater than `d1` and `.false.` otherwise. Overloads the
893+
!! `datetime` comparison operator that eturns `.true.` if `d0` is
894+
!! greater than `d1` and `.false.` otherwise. Overloads the
895895
!! operator `>`.
896896

897897
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -965,7 +965,7 @@ pure elemental logical function gt(d0,d1)
965965

966966
pure elemental logical function lt(d0,d1)
967967

968-
!! `datetime` comparison operator that returns `.true.` if `d0` is
968+
!! `datetime` comparison operator that returns `.true.` if `d0` is
969969
!! less than `d1` and `.false.` otherwise. Overloads the operator `<`.
970970

971971
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -979,7 +979,7 @@ pure elemental logical function lt(d0,d1)
979979

980980
pure elemental logical function eq(d0,d1)
981981

982-
!! `datetime` comparison operator that returns `.true.` if `d0` is
982+
!! `datetime` comparison operator that returns `.true.` if `d0` is
983983
!! equal to `d1` and `.false.` otherwise. Overloads the operator `==`.
984984

985985
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -1005,7 +1005,7 @@ pure elemental logical function eq(d0,d1)
10051005

10061006
pure elemental logical function neq(d0,d1)
10071007

1008-
!! `datetime` comparison operator that eturns `.true.` if `d0` is
1008+
!! `datetime` comparison operator that eturns `.true.` if `d0` is
10091009
!! not equal to `d1` and `.false.` otherwise. Overloads the operator `/=`.
10101010

10111011
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -1019,8 +1019,8 @@ pure elemental logical function neq(d0,d1)
10191019

10201020
pure elemental logical function ge(d0,d1)
10211021

1022-
!! `datetime` comparison operator. Returns `.true.` if `d0` is greater
1023-
!! than or equal to `d1` and `.false.` otherwise. Overloads the
1022+
!! `datetime` comparison operator. Returns `.true.` if `d0` is greater
1023+
!! than or equal to `d1` and `.false.` otherwise. Overloads the
10241024
!! operator `>=`.
10251025

10261026
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -1034,8 +1034,8 @@ pure elemental logical function ge(d0,d1)
10341034

10351035
pure elemental logical function le(d0,d1)
10361036

1037-
!! `datetime` comparison operator. Returns `.true.` if `d0` is less
1038-
!! than or equal to `d1`, and `.false.` otherwise. Overloads the
1037+
!! `datetime` comparison operator. Returns `.true.` if `d0` is less
1038+
!! than or equal to `d1`, and `.false.` otherwise. Overloads the
10391039
!! operator `<=`.
10401040

10411041
class(datetime),intent(in) :: d0 !! lhs `datetime` instance
@@ -1065,9 +1065,9 @@ pure elemental logical function isLeapYear(year)
10651065

10661066
pure function datetimeRange(d0,d1,t)
10671067

1068-
!! Given start and end `datetime` instances `d0` and `d1` and time
1069-
!! increment as `timedelta` instance `t`, returns an array of
1070-
!! `datetime` instances. The number of elements is the number of whole
1068+
!! Given start and end `datetime` instances `d0` and `d1` and time
1069+
!! increment as `timedelta` instance `t`, returns an array of
1070+
!! `datetime` instances. The number of elements is the number of whole
10711071
!! time increments contained between datetimes `d0` and `d1`.
10721072

10731073
type(datetime), intent(in) :: d0 !! start time
@@ -1113,8 +1113,8 @@ pure elemental integer function daysInMonth(month,year)
11131113

11141114
if(month < 1 .or. month > 12)then
11151115
! Should raise an error and abort here, however we want to keep
1116-
! the pure and elemental attributes. Make sure this function is
1117-
! called with the month argument in range.
1116+
! the pure and elemental attributes. Make sure this function is
1117+
! called with the month argument in range.
11181118
daysInMonth = 0
11191119
return
11201120
endif
@@ -1147,7 +1147,7 @@ pure elemental integer function daysInYear(year)
11471147

11481148
pure elemental real(kind=real64) function date2num(d)
11491149

1150-
!! Given a datetime instance d, returns number of days since
1150+
!! Given a datetime instance d, returns number of days since
11511151
!! `0001-01-01 00:00:00`, taking into account the timezone offset.
11521152

11531153
type(datetime),intent(in) :: d !! `datetime` instance
@@ -1174,17 +1174,17 @@ pure elemental real(kind=real64) function date2num(d)
11741174
+ d_utc % hour*h2d &
11751175
+ d_utc % minute*m2d&
11761176
+ (d_utc % second+1e-3_real64*d_utc % millisecond)*s2d
1177-
1177+
11781178
endfunction date2num
11791179

11801180

11811181

11821182
pure elemental type(datetime) function num2date(num)
11831183

1184-
!! Given number of days since `0001-01-01 00:00:00`, returns a
1184+
!! Given number of days since `0001-01-01 00:00:00`, returns a
11851185
!! correspoding `datetime` instance.
11861186

1187-
real(kind=real64),intent(in) :: num
1187+
real(kind=real64),intent(in) :: num
11881188
!! number of days since `0001-01-01 00:00:00`
11891189

11901190
integer :: year,month,day,hour,minute,second,millisecond
@@ -1247,7 +1247,7 @@ pure elemental type(datetime) function num2date(num)
12471247
type(datetime) function strptime(str,format)
12481248

12491249
!! A wrapper function around C/C++ strptime function.
1250-
!! Returns a `datetime` instance.
1250+
!! Returns a `datetime` instance.
12511251

12521252
character(len=*),intent(in) :: str !! time string
12531253
character(len=*),intent(in) :: format !! time format
@@ -1264,7 +1264,7 @@ type(datetime) function strptime(str,format)
12641264

12651265
pure elemental type(datetime) function tm2date(ctime)
12661266

1267-
!! Given a `tm_struct` instance, returns a corresponding `datetime`
1267+
!! Given a `tm_struct` instance, returns a corresponding `datetime`
12681268
!! instance.
12691269

12701270
type(tm_struct),intent(in) :: ctime !! C-style time struct

0 commit comments

Comments
 (0)