Created
January 8, 2017 23:11
-
-
Save spencerwi/39130aeeacdead899b9f6adbca34b49c to your computer and use it in GitHub Desktop.
whenwas.fsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fsharpi --exec | |
open System | |
type DateScale = Hour | Hours | Day | Days | Week | Weeks | |
type DateDirection = Ago | Forward | |
let parseDirection strInput = | |
match strInput with | |
| "forward" | "from now" -> Forward | |
| "ago" | "back" -> Ago | |
| _ -> failwith "Unrecognized direction" | |
;; | |
let parseScale strInput = | |
match strInput with | |
| "hour" -> Hour | |
| "hours" -> Hours | |
| "day" -> Day | |
| "days" -> Days | |
| "week" -> Week | |
| "weeks" -> Weeks | |
| _ -> failwith "Unrecognized interval" | |
;; | |
let getDate interval scale direction = | |
let hoursToAdd = match scale with | |
| Hour | Hours -> interval | |
| Day | Days -> interval * 24 | |
| Week | Weeks -> interval * 24 * 7 | |
in | |
let signedHours = match direction with | |
| Ago -> -1 * hoursToAdd | |
| Forward -> hoursToAdd | |
in | |
signedHours | |
|> float | |
|> System.DateTime.Now.AddHours | |
;; | |
let main (args: string[]) = | |
if args.Length <> 3 then | |
printfn "USAGE: whenwas N UNITS (ago|forward)" | |
1 | |
else | |
let intervalAsNumber = int args.[0] | |
let parsedScale = args.[1].ToLower() |> parseScale | |
let parsedDirection = args.[2].ToLower() |> parseDirection | |
let computedDate = getDate intervalAsNumber parsedScale parsedDirection | |
printfn "%A" computedDate | |
0 | |
fsi.CommandLineArgs | |
|> Array.toList | |
|> List.tail | |
|> List.toArray | |
|> main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment