"Fossies" - the Fresh Open Source Software Archive

Member "slib-3b6/iso8601.scm" (30 Mar 2015, 2284 Bytes) of package /linux/privat/slib-3b6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Lisp source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 ;;;; "iso8601.scm" ISO-8601 time conversion routines
    2 ;;; Copyright (C) 2014 Aubrey Jaffer
    3 ;
    4 ;Permission to copy this software, to modify it, to redistribute it,
    5 ;to distribute modified versions, and to use it for any purpose is
    6 ;granted, subject to the following restrictions and understandings.
    7 ;
    8 ;1.  Any copy made of this software must include this copyright notice
    9 ;in full.
   10 ;
   11 ;2.  I have made no warranty or representation that the operation of
   12 ;this software will be error-free, and I am under no obligation to
   13 ;provide any services, by way of maintenance, update, or otherwise.
   14 ;
   15 ;3.  In conjunction with products arising from the use of this
   16 ;material, there shall be no use of my name in any advertising,
   17 ;promotional, or sales literature without prior written consent in
   18 ;each case.
   19 
   20 (require 'posix-time)
   21 (require 'printf)
   22 (require 'scanf)
   23 
   24 ;;@code{(require 'iso-8601)}
   25 ;;@ftindex iso-8601
   26 
   27 ;;@body
   28 ;;@1 is the time in seconds since 00:00:00 GMT, January 1, 1970.
   29 ;;@0 returns an expanded ISO 8601 format string for the date and time.
   30 (define (time->iso-8601 time)
   31   (let ((tms (cddr (cdddr (reverse (vector->list (gmtime time)))))))
   32     ;;(print 'tms tms)
   33     (apply sprintf #f "%04d-%02d-%02dT%02d:%02d:%02d"
   34        (+ 1900 (car tms)) (+ 1 (cadr tms)) (cddr tms))))
   35 
   36 ;;@body
   37 ;;@1 is a time in seconds since 00:00:00 GMT, January 1, 1970.
   38 ;;@0 returns a compact ISO 8601 format string for the date and time.
   39 (define (time->iso8601 time)
   40   (let ((tms (cddr (cdddr (reverse (vector->list (gmtime time)))))))
   41     ;;(print 'tms tms)
   42     (apply sprintf #f "%04d%02d%02dT%02d%02d%02d" (+ 1900 (car tms)) (+ 1 (cadr tms)) (cddr tms))))
   43 
   44 ;;@body
   45 ;;@1 is a string in ISO 8601 format, either compact or expanded.
   46 ;;@0 returns that time in seconds since 00:00:00 GMT, January 1, 1970.
   47 (define (iso-8601->time str)
   48   (define tim (make-vector 9 0))
   49   (define (ttry format)
   50     (sscanf str
   51         format
   52         (vector-ref tim 5)
   53         (vector-ref tim 4)
   54         (vector-ref tim 3)
   55         (vector-ref tim 2)
   56         (vector-ref tim 1)
   57         (vector-ref tim 0)))
   58   (cond ((or (<= 4 (ttry "%04d-%02d-%02dT%02d:%02d:%02d") 6)
   59          (<= 4 (ttry "%04d%02d%02dT%02d%02d%02d") 6))
   60      (vector-set! tim 5 (+ -1900 (vector-ref tim 5)))
   61      (vector-set! tim 4 (+ -1 (vector-ref tim 4)))
   62      (gmktime tim))
   63     (else #f)))