Skip to content

Commit

Permalink
Add implementation of 'today' with basic ffi
Browse files Browse the repository at this point in the history
For javascript and erlang. Seems to work from testing.
  • Loading branch information
michaeljones committed Jun 10, 2024
1 parent c84f05c commit 7360a43
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Temporary name.
This is a port of the [justinmimbs/date](https://package.elm-lang.org/packages/justinmimbs/date/latest) Elm library.
Done with the kind permission of the author. The API has been adjusted to match norms of the Gleam language in places.

Currently the `fromPosix` and `today` functions have not yet but ported.
Currently the `fromPosix` function have not yet been ported.
10 changes: 10 additions & 0 deletions src/date.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,16 @@ fn range_help(
// today : Task x Date
// today =
// Task.map2 fromPosix Time.here Time.now
pub fn today() -> Date {
let #(year, month_number, day) = get_year_month_day()

from_calendar_date(year, number_to_month(month_number), day)
}

@external(erlang, "date_ffi", "get_year_month_day")
@external(javascript, "./date_ffi.mjs", "get_year_month_day")
fn get_year_month_day() -> #(Int, Int, Int)

//
//
//
Expand Down
9 changes: 9 additions & 0 deletions src/date_ffi.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

-module(date_ffi).

-export([
get_year_month_day/0
]).

get_year_month_day() ->
date().
4 changes: 4 additions & 0 deletions src/date_ffi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function get_year_month_day() {
let date = new Date();
return [date.getFullYear(), date.getMonth() + 1, date.getDate()]
}
6 changes: 6 additions & 0 deletions test/date_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub fn leap_year_test() {
|> should.equal(False)
}

// // Commented out as today changes :)
// pub fn today_test() {
// date.today()
// |> should.equal(date.from_calendar_date(2024, date.Jun, 10))
// }

// module Tests exposing (suite)

// import Date exposing (Date, Interval(..), Unit(..))
Expand Down

0 comments on commit 7360a43

Please sign in to comment.