fhp - date

Descripción del contenido de la página

Módulo del programa fhp para gestionar fechas y tiempos.

Etiquetas:

Código fuente

CR .( fhp-date )

\ Copyright (C) 2006,2013 Marcos Cruz (programandala.net)

\ This file is part of
\ fhp ("Forth HTML Preprocessor") version B-00-201206
\ (http://programandala.net/en.program.fhp).
\ This file provides the date and timing tools.

\ fhp is free software; you can redistribute it and/or modify
\ it under the terms of the GNU General Public License as published by
\ the Free Software Foundation; either version 2 of the License, or
\ (at your option) any later version.
\
\ fhp is distributed in the hope that it will be useful, but
\ WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
\ General Public License for more details.
\
\ You should have received a copy of the GNU General Public License
\ along with fhp; if not, see <http://gnu.org/licenses>.

\ fhp is written in the Forth language
\ with Gforth (http://gnu.org/software/gforth/).

\ .............................................................
\ History

\ 2015-02-04: Header updated; layout a bit changed.
\
\ 2006-08-18: Created with code factored from my application
\ alinome-bici.
\
\ 2006-08-31: Several words added or improved.
\
\ 2006-09-04: New words ROT>STR+ and >META-DATE
\
\ 2013-05-18: '>iso-date' and its related code have been
\ factored out, rewritten and simplified as a module of the
\ Galope library.  See
\ <http://programandala.net/en.program.galope>. The module is
\ called "yyyymmdd-to-iso.fs".

\ .............................................................
\ Code

svariable time-zone
S" +02:00" time-zone place  \ system dependent time zone
\ S" CET" time-zone place  \ system dependent time zone
\ S" Z" time-zone place  \ alternative for UTC time zone

CREATE iso-date 10 ALLOT ALIGN

: iso-year>  ( c-addr1 u1 c-addr2 -- )

  \ Receive a string in the format "yyyy[mm[dd]]",
  \ extract the year and place it at the beginning of the counted string at c-addr2.

  \ 2006-08-10

  NIP 4 SWAP place

;

: iso-month>  ( c-addr1 u1 c-addr2 -- )

  \ Receive a string in the format "yyyy[mm[dd]]",
  \ extract the month and place it into the counted string at c-addr2,
  \ that already has "yyyy".

  \ 2006-08-10

  SWAP 4 >
  IF
    DUP >R COUNT + [CHAR] - OVER C!  \ put a separator after the year
    1+ SWAP 4 CHARS + SWAP 2 CMOVE  \ copy the 2 digits of the month
    7 R> C!  \ new lenght
  ELSE
    2DROP
  THEN

  ;

: iso-day>  ( c-addr1 u1 c-addr2 -- )

  \ Receive a string in the format "yyyy[mm[dd]]",
  \ extract the day and place it into the counted string at c-addr2,
  \ that already has "yyyy-mm".

  \ 2006-08-10

  SWAP 6 >
  IF
    DUP >R COUNT + [CHAR] - OVER C!  \ put separator
    1+ SWAP 6 CHARS + SWAP 2 CMOVE  \ copy the 2 digits
    10 R> C!  \ new lenght
  ELSE
    2DROP
  THEN

  ;

: not-digit?  ( c -- flag )

  \ 2006-08-10

  DUP [CHAR] 0 <  SWAP [CHAR] 9 > OR

  ;

: left-digits  ( c-addr1 u1 -- c-addr1 u1' )

  \ Return a string removing all characters at the right of (and including) the first non digit character found.

  \ 2006-08-10

  DROP DUP
  BEGIN
    DUP CHAR+ SWAP C@ not-digit?
  UNTIL
  char- OVER -

  ;

: >iso-date  ( c-addr1 u1 -- c-addr2 u2 )

  \ Transform a date string with the format:
  \   yyyy[mm[dd]]x*y
  \ (where "yyyy", "mm" and "dd" are numeric characters
  \ and "x*y" is any string that starts with a non numeric character)
  \ into the format:
  \   yyyy[-mm[-dd]]
  \ The goal is to get the date from the name of a file that has been named that way.
  \ It is also used to convert a metadata date ("change" or "created") format: "yyyymmdd".

  \ 2006-08-10

  left-digits
  2DUP iso-date iso-year>
  2DUP iso-date iso-month>
  iso-date iso-day>
  iso-date COUNT

  ;

: >iso-full-date  ( c-addr1 u1 -- c-addr2 u2 )

  \ Transform a date string with the format:
  \   yyyymmdd
  \ (where "yyyy", "mm" and "dd" are numeric characters)
  \ into the format:
  \   yyyy-mm-ddT00:00:00Z
  \ (where Z is the time zone stored in the variable time-zone)
  \ It is used to convert a metadata date ("change" or "created") format: "yyyymmdd" into
  \ a standard date.

  \ 2009-10-22

  >iso-date  s" T00:00:00" str+  time-zone count str+

  ;

: #>html  ( u -- )

  \ Send one number (part of a date) to the HTML file
  \ after converting it into a string
  \ and putting one left zero if needed.

  \ u = year, month, day, hour, minute or second

  n>str DUP 1 =
  IF [CHAR] 0 emit>html
  THEN
  >>html

  ;

: 3#>html  ( u1 u2 u3 c -- )

  \ Send three numbers to the html file separated by a character.

  \ u1 = day or second
  \ u2 = month or minute
  \ u3 = year or hour
  \ c = separator

  \ 2006-08-17

  >R
  #>html R@ emit>html
  #>html R> emit>html
  #>html

  ;

: time>html  ( u1 u2 u3 -- )

  \ u1 = hour
  \ u2 = minute
  \ u3 = second

  \ 2006-08-17

  [CHAR] : 3#>html

  ;

: date>html  ( u1 u2 u3 -- )

  \ u1 = day
  \ u2 = month
  \ u3 = year

  \ 2006-08-17

  [CHAR] - 3#>html

  ;

: iso-date&time>html  ( u1 u2 u3 u4 u5 u6 -- )

  \ Create into the HTML file a date and time string
  \ with the ISO format.

  \ u1 = second
  \ u2 = minute
  \ u3 = hour
  \ u4 = day
  \ u5 = month
  \ u6 = year

  \ 2006-08-17
  \ 2006-08-20 Conformed to ISO.
  \ 2006-08-31 Improved.

  date>html
  S" T" >>html time>html
  time-zone COUNT >>html

  ;

: rot>str+  ( n c-addr1 u1 -- c-addr2 u2 )

  \ Convert a number into a string and append it to a given string.

  \ 2006-09-04

  ROT n>str str+

  ;

: >meta-date  ( u1 u2 u3 u4 u5 u6 -- )

  \ Return a date and time string
  \ in the format used by the <meta> tag:
  \ yyyymmdd;hhmmss

  \ u1 = second
  \ u2 = minute
  \ u3 = hour
  \ u4 = day
  \ u5 = month
  \ u6 = year

  \ 2006-09-04

  n>str rot>str+ rot>str+
  S" ;" str+
  rot>str+ rot>str+ rot>str+

  ;

false [IF]  \ obsolete!!!
: meta-date>html  ( u1 u2 u3 u4 u5 u6 -- )

  \ Create into the HTML file a date and time string
  \ in the format used by the <meta> tag:
  \ yyyymmdd;hhmmss

  \ u1 = second
  \ u2 = minute
  \ u3 = hour
  \ u4 = day
  \ u5 = month
  \ u6 = year

  \ 2006-08-31

  #>html #>html #>html
  [CHAR] ; emit>html
  #>html #>html #>html

  ;
[THEN]

.(  fhp-date ok!)