Coder Social home page Coder Social logo

timex_poison's Introduction

TimexPoison

Easily parse timestamps using Timex as they are decoded from json by Poison.

Installation

If available in Hex, the package can be installed as:

  1. Add timex_poison to your list of dependencies in mix.exs:
def deps do
  [{:timex_poison, "~> 0.2.0"}]
end
  1. Ensure timex_poison is started before your application:
def application do
  [applications: [:timex_poison]]
end

Usage

Basic Usage

By default, timestamps will be parsed using Timex's {ISOz} formatter.

defmodule MyGreatStruct do
  use TimexPoison keys: [:created_at]
  defstruct [:name, :created_at]
end

iex> Poison.decode! ~s({"name": "Great", "created_at": "2016-07-27T08:50:08.681Z"}), as: %MyGreatStruct{}
%MyGreatStruct{name: "Great", created_at: #<DateTime(2016-07-27T08:50:08Z)>} 

Formatter Selection

Alternatively you can specify your own formatter:

defmodule WithDateFormat do
  use TimexPoison, keys: [:timestamp], format: "{RFC1123}"
  defstruct [:timestamp]
end

iex> Poison.decode(~s({"timestamp" : "Tue, 05 Mar 2013 23:25:19 +0200"}), as: %WithDateFormat{})

timex_poison's People

Contributors

mootpointer avatar

Stargazers

Jason Goodwin avatar

Watchers

James Cloos avatar  avatar

Forkers

railsmechanic

timex_poison's Issues

Elixir 1.5 - error compiling

Hey this doesn't seem to work in my project (poison 2, elixir 1.5).
I'm not very experienced with macros so...
Error during compilation

== Compilation error in file lib/command/my_command.ex ==
** (SyntaxError) lib/command/my_command.ex:2: syntax error before: 'keys':
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

Struct:

defmodule Command.MyCommand do
  use TimexPoison keys: [:production_date, :start_time, :end_time, :timestamp]

  defstruct 
            production_date: nil,
            start_time: nil,
            end_time: nil,
            timestamp: nil
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.