Giter Club home page Giter Club logo

cl-rfc3339's Introduction

CL-RFC3339

A trivial Common Lisp library to convert local-time timestamps, existing RFC 3339 strings or Unix epoch times (integer or double float) to a canonical RFC 3339 compliant format in UTC with millisecond granularity. For example

2015-12-28T02:59:00.000Z

This is the same format returned by the JavaScript function

Date.prototype.toISOString()

which was [standardized in ECMA-262 5th edition] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString).

To be clear, RFC 3339 leave some wriggle room. For example, the literal T above may be a space, something the PostgreSQL project tends to use. But, to paraphrase section 5.1 of the above RFC 3339, by standardizing on a canonical format viz:

  • always using timezeone Z (that is, UTC)
  • always using millisecond granularity

the resulting time strings are all the same length and all of the same format and so may be sorted and compared as strings and yet provide the expected result as times.

For those people storing times without access to a first class time type (eg. in JSON) such a standard format is desireable.

Of course, using Unix time in milliseconds is easier still (and smaller and faster) but has the drawback that 1451271540000 is rather opaque compared to the above string.

Documentation

There is but one exported symbol, the generic function rfc3339-string:

CL-USER> (defpackage :example
           (:use :cl :cl-rfc3339 :local-time))
(in-package :example)

(rfc3339-string (now))
"2014-12-18T20:31:35.743Z"

(rfc3339-string (timestamp-to-unix (now)))
"2014-12-18T20:31:50.000Z"

(rfc3339-string *)
"2014-12-18T20:31:50.000Z"

(rfc3339-string "2014-12-18T20:24:46.000001Z")
"2014-12-18T20:24:46.000Z"

(rfc3339-string "2014-12-18T20:24:46.499001Z")
"2014-12-18T20:24:46.499Z"

(rfc3339-string "2014-12-18T20:24:46.4999Z")
"2014-12-18T20:24:46.499Z"

(let ((str "2014-12-18T06:56:45.000Z"))
  (rfc3339-string (+ 173d-3 (timestamp-to-unix (parse-rfc3339-timestring str)))))
"2014-12-18T06:56:45.173Z"

Install

Navigate to your ~/quicklisp/local-projects directory and do git clone https://github.com/gtod/cl-rfc3339.git. Then at your REPL evaluate:

(ql:register-local-projects)
(ql:quickload :cl-rfc3339)

Tests

(ql:quickload :cl-rfc3339-test)
(cl-rfc3339-test:run)

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.