An open API service indexing awesome lists of open source software.

https://github.com/stackbuilders/workdays

Workday calculations in Haskell.
https://github.com/stackbuilders/workdays

Last synced: 10 months ago
JSON representation

Workday calculations in Haskell.

Awesome Lists containing this project

README

          

# Workdays: Workday calculations in Haskell

> “Let us sit here for five minutes and think of nothing.”

The workdays library allows to do workday (and weekday) calculations
in Haskell.

Based on the [workdays](https://pypi.python.org/pypi/workdays/) Python
package.

## Examples

(Tested with GHC 8.0.1.)

In order to use the workdays library, import the Workdays module:

```
λ> import Workdays
```

To specify dates, we need to create values of `Date` using the `Date`
constructor:

```
λ> :type Date
Date :: Year -> Month -> DayOfMonth -> Date
```

A value of type `Date` is a year (`Integer`), a month (`Int`), and a
day of month (`Int`).

Here's December 1, 2016:

```
λ> Date 2016 12 01
Date {dateYear = 2016, dateMonth = 12, dateDay = 1}
```

The `workdays` function is used to calculate the number of workdays
between two dates:

```
λ> :type workdays
workdays :: Date -> Date -> Set Date -> Integer
```

This function takes start and end dates, and a set of dates to
exclude.

Let's calculate the number of workdays between December 1, 2016 and
December 31, 2016 without excluding dates:

```
λ> workdays (Date 2016 12 01) (Date 2016 12 31) []
22
```

If there are no dates to exclude, we're calculating weekdays. We can
use the `weekdays` function instead:

```
λ> weekdays (Date 2016 12 01) (Date 2016 12 31)
22
```

To create sets of dates, we need to import the `Set` data type from
the containers library:

```
λ> import Data.Set (Set)
```

And we can activate the overloaded lists language extension to create
sets as lists:

```
λ> :set -XOverloadedLists
```

Let's use some holidays in Colombia as examples. For now, just
Christmas Day:

```
λ> holidays = [Date 2016 12 25] :: Set Date
```

We can use this set as the set of dates to exclude from the workdays
calculation:

```
λ> workdays (Date 2016 12 01) (Date 2016 12 31) holidays
22
```

Unfortunately, Christmas Day is a Sunday, so the result is still 22.
Some countries observe Christmas Day on Monday, but not Colombia.

Let's add December 8, 2016 (the Feast of the Immaculate Conception) to
the set of dates to exclude:

```
λ> holidays = [Date 2016 12 08, Date 2016 12 25] :: Set Date
```

And try again:

```
λ> workdays (Date 2016 12 01) (Date 2016 12 31) holidays
21
```

The result is now 21, which is the actual number of workdays for
December, 2016 in Colombia.

## License

Licensed under the MIT License.