Skip to content

Commit

Permalink
added Date.fx, the module to handle dates & calendars, example and te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
vpisarev committed May 13, 2023
1 parent 617c3bf commit 4dd2db3
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 25 deletions.
62 changes: 62 additions & 0 deletions examples/checkdate.fx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
This file is a part of ficus language project.
See ficus/LICENSE for the licensing terms
*/

import Sys, Date

val ERRVAL = -5000

fun try_parse_date_or_jdn(date: string) =
try {
Some(Date.parse(date))
} catch {
| Date.DateError(msg) =>
val jdn = date.to_int_or(ERRVAL)
if jdn == ERRVAL {
println(f"Error: cannot recognize date '{date}': please, specify correct date or JDN (non-negative integer)")
None
} else {
Some(Date.make(jdn))
}
| e =>
println(f"Error: exception {e} occured when parsing '{date}'")
None
}

fun parse(idx: int, date0: string) {
val date_ = date0.tolower()
val some_date =
if date_ == "today" || date_ == "now" {
Some(Date.today())
} else {
try_parse_date_or_jdn(date0)
}
match some_date {
| Some(date) =>
println(f"Input #{idx}: '{date0}'")
println(f" Parsed date: {date}, {date.weekday_name()}, week {date.weeknumber():02d}")
/*val gdate_str = try {
string()
} catch {
| Date.DateError _ => "out of range"
}*/
println(f" Gregorian: {date.rebase(Date.Calendar_Gregorian)}")
println(f" Julian: {date.rebase(Date.Calendar_Julian)}")
println(f" JDN: {date.jdn}")
| _ => {}
}
some_date
}

val args = Sys.arguments()
if args.empty() {
println("Usage: checkdate date1 ...")
} else {
val dates = [:: for d@i <- args {parse(i+1, d)}]
match dates {
| Some(date1) :: Some(date2) :: [] =>
println(f"Number of days between '{date1}' and '{date2}': {abs(date1 - date2)}")
| _ => {}
}
}
5 changes: 4 additions & 1 deletion examples/fst.fx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
*/

// the first test script
import testmod
import testmod, Date

val currdate = Date.today()

val str = f"
привет! 你好吗?
Today is {currdate}, {currdate.weekday_name()}.
BTW,
2*2 = {2*2},
π (the area of circle with radius 1) ~ {M_PI:.5f},
Expand Down
2 changes: 1 addition & 1 deletion lib/Builtins.fx
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ fun long(x: int32) = long(x :> int64)
fun long(x: uint32) = long(x :> int64)
fun long(x: bool) = long(x :> int64)
fun long(x: string): long
@ccode { return fx_atol(x, fx_result) }
@ccode { return fx_atol(x, 0, fx_result) }

fun int(x: ('t...)) = (for xj <- x {int(xj)})
fun uint8(x: ('t...)) = (for xj <- x {uint8(xj)})
Expand Down
Loading

0 comments on commit 4dd2db3

Please sign in to comment.