dbern
I’m excited to announce that TaxJar has developed and open-sourced DateTimeParser. We developed it because we found a need to parse user input into DateTimes or NaiveDateTimes. I didn’t find any existing libraries that did this not without knowing the format of the string in advance. Maybe it could help you out too?
Check out the repo:
https://github.com/taxjar/date_time_parser
Check it out at Hex:
Learn more about TaxJar: https://www.taxjar.com. Special thanks to them for investing in my time to develop this and allowing me to open-source it! Also thanks to nimble_parsec for making this much easier to develop.
Examples
iex> DateTimeParser.parse_datetime("19 September 2018 08:15:22 AM")
{:ok, ~N[2018-09-19 08:15:22]}
iex> DateTimeParser.parse_datetime("2034-01-13")
{:ok, ~N[2034-01-13 00:00:00]}
iex> DateTimeParser.parse_date("2034-01-13")
{:ok, ~D[2034-01-13]}
iex> DateTimeParser.parse_date("01/01/2017")
{:ok, ~D[2017-01-01]}
iex> DateTimeParser.parse_datetime("1/1/18 3:24 PM")
{:ok, ~N[2018-01-01T15:24:00]}
iex> DateTimeParser.parse_datetime("1/1/18 3:24 PM", assume_utc: true)
{:ok, ~U[2018-01-01T15:24:00Z]}
# the ~U is a DateTime sigil introduced in Elixir 1.9.0
iex> DateTimeParser.parse_datetime(~s|"Dec 1, 2018 7:39:53 AM PST"|)
{:ok, ~U[2018-12-01T14:39:53Z]}
# Notice that the date is converted to UTC by default
iex> {:ok, datetime} = DateTimeParser.parse_datetime(~s|"Dec 1, 2018 7:39:53 AM PST"|, to_utc: false)
iex> datetime
#DateTime<2018-12-01 07:39:53-07:00 PDT PST8PDT>
iex> DateTimeParser.parse_time("10:13pm")
{:ok, ~T[22:13:00]}
iex> DateTimeParser.parse_time("10:13:34")
{:ok, ~T[10:13:34]}
Trending in Announcing
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
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
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Other Trending Topics
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
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dbern
1.0 is releasing soon! Yesterday, I published 1.0.rc-1 as a means to dogfood the recent changes. It’ll stay there for a bit to ensure no bugs pop up.
Changes since initial release:
DateTimeParser.parse/2to give the best answer it can give. Previously, you’d have to useparse_datetime,parse_date, orparse_time. Now you can throw strings intoparseand get the most specific struct for what it could parse. For example,2019-01-01will give you a%Date{};something @ 9:30pmwill give you a%Time{}, etc.DateTimeParser.parse_datetime("2019-01-01")it would give you2019-01-01T00:00:00. Where did the time come from? It was assumed. Now in 1.x this will not be the default. Also, it would auto-convert timezone’d timestamps to UTC; now you have to opt-in to that behavior.if you’re interested, give it a shot and let me know what you think. date_time_parser | Hex
dbern
1.0 has released!
The theme of this release is no information is better than assumed information. So the breaking changes are simply to undo some defaults that we had set in pre-1.x. The biggest one was assuming a
00:00:00time when no time was found, another was converting to UTC automatically. This no longer happens by default, but you can still have these behaviors through an option.This release was dogfooded for a while, and we found some bugs in it during the RC phase, so if you’re using the library pre-1.x I recommend that you upgrade as soon as you can.
Here’s the changelog:
Breaking
parse_datetimeto no longer assume time. Previously, it would assume00:00:00if time could not be parsed. This is replaced with an opt-in optionassume_time. See next point. If you relied on this, to upgrade add the option; eg:DateTimeParser.parse_datetime(string, assume_time: true)parse_datetimeto no longer convert DateTime to UTC timezone. If you relied on this, to upgrade add the option; eg:DateTimeParser.parse_datetime(string, to_utc: true)parse_dateto no longer assume a date. Previously, it would assume the current date, and replace the found information from the string. This is replaced with an opt-in option ofassume_date. If you relied on this, to upgrade add the option; eg:DateTimeParser.parse_date(string, assume_date: true)Bugs
Features
parse/2that will respond with the best match. This function accepts all options introduced below.parse_datetime/2to accept options:assume_time: true | %Time{} | falsewith the default of false.parse_date/2to accept options:assume_date: true | %Date{} | falsewith the default of false.parse!/2,parse_datetime!/2,parse_time!/2, andparse_date!/2parsers: []option to add or disable parsers. This is helpful if you are don’t want to consider Serial or Epoch timestampsmaz
Awesome parser, thank you.