Menkir

Menkir

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.

Showing Posts 1 to 2

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”
Menkir

Menkir OP

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: .

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
lawik
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./ The firs...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews