Skip to content

New functions monthLong and monthShort to get the name of the month #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ See some basic examples [here](examples).
* [*isocalendar*](#isocalendar)
* [*isoformat*](#isoformat)
* [*isValid*](#isvalid)
* [*monthLong*](#monthlong)
* [*monthShort*](#monthshort)
* [*now*](#now)
* [*secondsSinceEpoch*](#secondssinceepoch)
* [*strftime*](#strftime)
Expand Down Expand Up @@ -196,6 +198,8 @@ type :: datetime
procedure,pass(self),public :: weekdayShort
procedure,pass(self),public :: isoweekdayShort
procedure,pass(self),public :: yearday
procedure,pass(self),public :: monthLong
procedure,pass(self),public :: monthShort

! private methods
procedure,pass(self),private :: addMilliseconds
Expand Down Expand Up @@ -293,6 +297,12 @@ pure elemental integer function getMonth(self)
```
Returns the month of a `datetime` instance.

#### See also

* [*monthLong*](#monthLong)

* [*monthShort*](#monthShort)

[Back to top](#top)
<hr>

Expand Down Expand Up @@ -469,6 +479,68 @@ print *, a % isValid() ! .false.
[Back to top](#top)
<hr>

### monthLong<a id="monthlong"></a>

```fortran
pure elemental character(len=9) function monthLong(self)
class(datetime),intent(in) :: self
```

Returns the full name of the month.

#### Example usage

```fortran
use datetime_module,only:datetime

type(datetime) :: a

! Initialize:
a = datetime(2013,1,1)

print *, a % monthLong() ! January
```

#### See also

* [*getMonth*](#getmonth)

* [*monthShort*](#monthshort)

[Back to top](#top)
<hr>

### monthShort<a id="monthshort"></a>

```fortran
pure elemental character(len=3) function monthShort(self)
class(datetime),intent(in) :: self
```

Returns the short (3-letter) name of the month.

#### Example usage

```fortran
use datetime_module,only:datetime

type(datetime) :: a

! Initialize:
a = datetime(2013,1,1)

print *, a % monthShort() ! Jan
```

#### See also

* [*getMonth*](#getmonth)

* [*monthLong*](#monthLong)

[Back to top](#top)
<hr>

### now<a id="now"></a>

```fortran
Expand Down
21 changes: 21 additions & 0 deletions src/datetime_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ module datetime_module
procedure, pass(self), public :: weekdayLong
procedure, pass(self), public :: weekdayShort
procedure, pass(self), public :: yearday
procedure, pass(self), public :: monthLong
procedure, pass(self), public :: monthShort

! private methods
procedure, pass(self), private :: addMilliseconds
Expand Down Expand Up @@ -613,6 +615,25 @@ pure elemental character(3) function isoweekdayShort(self)
isoweekdayShort = days(self % isoweekday())
end function isoweekdayShort

pure elemental character(9) function monthLong(self)
! Returns the full name of the month.
class(datetime), intent(in) :: self
character(9), parameter :: &
months(*) = ['January ', 'February ', 'March ', 'April ', &
'May ', 'June ', 'July ', 'August ', &
'September', 'October ', 'November ', 'December ']
monthLong = months(self%getMonth())
end function monthLong

pure elemental character(3) function monthShort(self)
! Returns the short (3-letter) name of the month.
class(datetime), intent(in) :: self
character(3), parameter :: &
months(*) = ['Jan', 'Feb', 'Mar', 'Apr', &
'May', 'Jun', 'Jul', 'Aug', &
'Sep', 'Oct', 'Nov', 'Dec']
monthShort = months(self%getMonth())
end function monthShort

function isocalendar(self)
! Returns an array of 3 integers, year, week number, and week day,
Expand Down
106 changes: 105 additions & 1 deletion tests/datetime_tests.f90
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ subroutine test_datetime
print *

! Test counter; modify if adding new tests
ntests = 192
ntests = 216

call initialize_tests(tests, ntests)

Expand Down Expand Up @@ -881,6 +881,110 @@ subroutine test_datetime

print '(71("-"))'

! datetime % monthLong()

a = datetime(2014, 1, 1)
tests(n) = assert(a % monthLong() == 'January', 'datetime % monthLong(), January')
n = n + 1

a = datetime(2014, 2, 1)
tests(n) = assert(a % monthLong() == 'February', 'datetime % monthLong(), February')
n = n + 1

a = datetime(2014, 3, 1)
tests(n) = assert(a % monthLong() == 'March', 'datetime % monthLong(), March')
n = n + 1

a = datetime(2014, 4, 1)
tests(n) = assert(a % monthLong() == 'April', 'datetime % monthLong(), April')
n = n + 1

a = datetime(2014, 5, 1)
tests(n) = assert(a % monthLong() == 'May', 'datetime % monthLong(), May')
n = n + 1

a = datetime(2014, 6, 1)
tests(n) = assert(a % monthLong() == 'June', 'datetime % monthLong(), June')
n = n + 1

a = datetime(2014, 7, 1)
tests(n) = assert(a % monthLong() == 'July', 'datetime % monthLong(), July')
n = n + 1

a = datetime(2014, 8, 1)
tests(n) = assert(a % monthLong() == 'August', 'datetime % monthLong(), August')
n = n + 1

a = datetime(2014, 9, 1)
tests(n) = assert(a % monthLong() == 'September', 'datetime % monthLong(), September')
n = n + 1

a = datetime(2014,10, 1)
tests(n) = assert(a % monthLong() == 'October', 'datetime % monthLong(), October')
n = n + 1

a = datetime(2014,11, 1)
tests(n) = assert(a % monthLong() == 'November', 'datetime % monthLong(), November')
n = n + 1

a = datetime(2014,12, 1)
tests(n) = assert(a % monthLong() == 'December', 'datetime % monthLong(), December')
n = n + 1

print '(71("-"))'

! datetime % monthShort()

a = datetime(2014, 1, 1)
tests(n) = assert(a % monthShort() == 'Jan', 'datetime % monthShort(), January')
n = n + 1

a = datetime(2014, 2, 1)
tests(n) = assert(a % monthShort() == 'Feb', 'datetime % monthShort(), February')
n = n + 1

a = datetime(2014, 3, 1)
tests(n) = assert(a % monthShort() == 'Mar', 'datetime % monthShort(), March')
n = n + 1

a = datetime(2014, 4, 1)
tests(n) = assert(a % monthShort() == 'Apr', 'datetime % monthShort(), April')
n = n + 1

a = datetime(2014, 5, 1)
tests(n) = assert(a % monthShort() == 'May', 'datetime % monthShort(), May')
n = n + 1

a = datetime(2014, 6, 1)
tests(n) = assert(a % monthShort() == 'Jun', 'datetime % monthShort(), June')
n = n + 1

a = datetime(2014, 7, 1)
tests(n) = assert(a % monthShort() == 'Jul', 'datetime % monthShort(), July')
n = n + 1

a = datetime(2014, 8, 1)
tests(n) = assert(a % monthShort() == 'Aug', 'datetime % monthShort(), August')
n = n + 1

a = datetime(2014, 9, 1)
tests(n) = assert(a % monthShort() == 'Sep', 'datetime % monthShort(), September')
n = n + 1

a = datetime(2014,10, 1)
tests(n) = assert(a % monthShort() == 'Oct', 'datetime % monthShort(), October')
n = n + 1

a = datetime(2014,11, 1)
tests(n) = assert(a % monthShort() == 'Nov', 'datetime % monthShort(), November')
n = n + 1

a = datetime(2014,12, 1)
tests(n) = assert(a % monthShort() == 'Dec', 'datetime % monthShort(), December')
n = n + 1

print '(71("-"))'

! Timedelta tests
tests(n) = assert(timedelta() == timedelta(0, 0, 0, 0, 0), &
'timedelta empty constructor')
Expand Down