Menkir

Menkir

DatemathEx - Parser for datemath syntax

DatemathEx

DatemathEx is an Elixir library designed to parse datemath syntax using Combine.

Examples

  • Basic addition
iex(1)> DatemathEx.parse "2020-01-01||+1d"
{:ok, ~U[2020-01-02 00:00:00.000Z]}
  • Complex Expressions
iex(1)> DatemathEx.parse "2020-01-01T12:24:00||+31d+2h/h"
{:ok, ~U[2020-02-01 14:00:00Z]}

  • Relative to Now
iex(1)> DatemathEx.parse "now-1000s/h"
{:ok, ~U[2024-05-16 08:00:00Z]}
  • Timezone Offset
iex(1)> DatemathEx.parse "2020-01-01T12:00:00+02:00"
{:ok, ~U[2020-01-01 10:00:00.000000Z]}
  • Invalid Syntax
iex(1)> DatemathEx.parse "now*1h"
{:error, "Expected at least one parser to succeed at line 1, column 0."}

Current Limitations

While DatemathEx provides robust functionality for datemath parsing, there are areas where it can be improved:

  • Better Error Reporting: Enhanced error messages to provide more precise feedback on syntax errors and invalid operations.

Most Liked

Menkir

Menkir

You are absolutely right. I will consider using Combine . For me, it was about learning combinators and using NimbleParsec. The reason for choosing Timex came later when it was about time-shifting. :sweat_smile: .

Eiji

Eiji

I fully understand why you use nimble_parsec, but if you are already working with timex wouldn’t that be better to use combine instead? That would reduce dependencies as combine would be used anyway.

Not sure if remember nimble_parsec well enough, i.e. not sure if we can use label/2 in the middle of pipeline, but wouldn’t that be a more clear to read parser’s rule?

 defparsec :parse_input,
  choice([
    string("now")
    |> map({:now, []})
    |> post_traverse({:overwrite_now, []})
    |> label("UTC now anchor"),
    choice([
      datetime(),
      date()
    ])
    |> label("ISO datetime anchor")
    |> ignore_whitespace
    |> ignore(string("||"))
  ])
  |> ignore_whitespace
  |> parsec(:math_expressions)
  |> eos()
  |> reduce({:reduce_expressions, []})

With that we can:

  1. reduce code (by 4 doubled LOC)
  2. make rule more clear by moving common combinators to the end of pipe and outside of the choice combinator
  3. make rule more clear by moving ignore_whitespace and ignore(string/1) combinators after label which says that those are not part of a “ISO datetime anchor”

Where Next?

Popular in Announcing Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
Flo0807
Hello everyone! I am excited to share our heart project Backpex with you. After building several Phoenix applications, we realized that...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement