[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: *****SPAM***** Comparing two dates when one is po

Subject: Re: *****SPAM***** Comparing two dates when one is potentially malformed
From: "Christophe Marchand cmarchand@xxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 5 Dec 2016 08:26:21 -0000
Re: *****SPAM*****  Comparing two dates when one is  po
There is no standard function for parsing dates.

Here is the code we use, when we need to manage dates. Comments are in 
French, sorry about that, and code is XSLT 2.0. Using XSLT 3.0, you can 
use try/catch, that makes it simpler.

HPH,
Christophe

     <xd:doc>
         <xd:desc>
             <xd:p>Converts a date to ISO format : YYYY-MM-DD, with 
input format :</xd:p>
             <xd:ul>
                 <xd:li>YYYY-MM-DD (already ISO) : returns input</xd:li>
                 <xd:li>DD/MM/YYYY (ou DD-MM-YYYY ou DD/MM/YY)</xd:li>
             </xd:ul>
             <xd:param name="dateValue">Value you want to convert to 
ISO</xd:param>
             <xd:param name="sep">separator in input date 
($dateValue)</xd:param>
             <xd:param name="xsltName">name of calling xslt, to provide 
contexted logs</xd:param>
             <xd:param name="contextForErrors">context call 
string</xd:param>
         </xd:desc>
         <xd:return>The ISO converted date, if it was correct, or input 
date if not</xd:return>
     </xd:doc>
     <xsl:function name="efl:makeIsoDate" as="xs:string">
         <xsl:param name="dateValue" as="xs:string?"/>
         <xsl:param name="sep" as="xs:string"/>
         <xsl:param name="xsltName" as="xs:string"/>
         <xsl:param name="contextForErrors" as="xs:string"/>

         <xsl:variable name="s" select="string($dateValue)"/>
         <xsl:variable name="sToken" select="tokenize($s, $sep)" 
as="xs:string*"/>
         <xsl:variable name="regJJMMAAAA" as="xs:string" 
select="concat('^\d{2}', $sep, '\d{2}', $sep, '\d{4}$')"/>
         <xsl:variable name="regJMMAAAA" as="xs:string" 
select="concat('^\d', $sep, '\d{2}', $sep, '\d{4}$')"/>
         <xsl:variable name="regJJMMAA" as="xs:string" 
select="concat('^\d{2}', $sep, '\d{2}', $sep, '\d{2}')"/>
         <xsl:variable name="regJMMAA" as="xs:string" 
select="concat('^\d', $sep, '\d{2}', $sep, '\d{2}')"/>

         <xsl:choose>
             <!--sans string on ne peut rien faire : on renvoie la 
string vide-->
             <xsl:when test="not(exists($dateValue)) or empty($s)">
                 <xsl:sequence select="''"/>
             </xsl:when>
             <!-- date dC)jC  au format ISO, donc rien C  faire -->
             <xsl:when test="matches($s, '^\d{4}-\d{2}-\d{2}$')">
                 <xsl:sequence select="$s"/>
             </xsl:when>
             <!--sans sC)parateur on ne peut rien faire : on renvoie la 
string-->
             <xsl:when test="$sep = ''">
                 <xsl:sequence select="$s"/>
             </xsl:when>
             <!--La date est dans un format correct JJ/MM/AAAA, on la 
convertie en "AAAA-MM-JJ" -->
             <xsl:when test="matches($s, $regJJMMAAAA)">
                 <xsl:sequence select="concat($sToken[3], '-', 
$sToken[2], '-', $sToken[1])"/>
             </xsl:when>
             <!--La date est dans un format correct J/MM/AAAA, on la 
convertie en "AAAA-MM-JJ" en remettant le 0 devant le jour -->
             <xsl:when test="matches($s, $regJMMAAAA)">
                 <xsl:sequence select="concat($sToken[3], '-', 
$sToken[2], '-0', $sToken[1])"/>
             </xsl:when>
             <!--Le format est correct sauf l'annC)e qui est sur 2 
chiffres : on estime qu'au dela de l'annC)e courante on C)tait au siC(cle 
dernier -->
             <xsl:when test="matches($s, $regJJMMAA)">
                 <xsl:variable name="currentAAAA" 
select="year-from-date(current-date())"
                     as="xs:integer"/>
                 <xsl:variable name="currentAA__"
select="substring(string($currentAAAA), 1, 2) cast as xs:integer"
                     as="xs:integer"/>
                 <xsl:variable name="current__AA"
select="substring(string($currentAAAA), 3, 2) cast as xs:integer"
                     as="xs:integer"/>
                 <xsl:variable name="AA" select="xs:integer($sToken[3])" 
as="xs:integer"/>
                 <xsl:variable name="AAAA"
                     select="if ($AA gt $current__AA) then 
(concat($currentAA__ -1, $AA)) else (concat($currentAA__, $AA))"
                     as="xs:string"/>
                 <xsl:sequence select="concat($AAAA, '-', $sToken[2], 
'-', $sToken[1])"/>
             </xsl:when>
             <!--Le format est correct sauf l'annC)e qui est sur 2 
chiffres : on estime qu'au dela de l'annC)e courante on C)tait au siC(cle 
dernier -->
             <xsl:when test="matches($s, $regJMMAA)">
                 <xsl:variable name="currentAAAA" 
select="year-from-date(current-date())"
                     as="xs:integer"/>
                 <xsl:variable name="currentAA__"
select="substring(string($currentAAAA), 1, 2) cast as xs:integer"
                     as="xs:integer"/>
                 <xsl:variable name="current__AA"
select="substring(string($currentAAAA), 3, 2) cast as xs:integer"
                     as="xs:integer"/>
                 <xsl:variable name="AA" select="xs:integer($sToken[3])" 
as="xs:integer"/>
                 <xsl:variable name="AAAA"
                     select="if ($AA gt $current__AA) then 
(concat($currentAA__ -1, $AA)) else (concat($currentAA__, $AA))"
                     as="xs:string"/>
                 <xsl:sequence select="concat($AAAA, '-', $sToken[2], 
'-0', $sToken[1])"/>
             </xsl:when>
             <!--format non reconnu : on renvoie la string-->
             <xsl:otherwise>
                 <xsl:call-template name="efl:log">
                     <xsl:with-param name="xsltName" select="$xsltName"/>
                     <xsl:with-param name="level" select="'error'"/>
                     <xsl:with-param name="code" select="'makeIsoDate'"/>
                     <xsl:with-param name="xpathContext" 
select="$contextForErrors"/>
                     <xsl:with-param name="markup" select="false()"/>
                     <!--xsl:with-param name="description">Dans 
'<xsl:value-of select="$contextForErrors"/>', la date '<xsl:value-of 
select="$s"/>' n'est pas dans le bon format. Il se peut que le tri par 
date ne soit pas correct. Format attentu : JJ/MM/AAAA ou JJ/MM/AA au 
pire (on essayera de deviner le siC(cle...)</xsl:with-param-->
                   <!-- amC)lioration des logs : sur une seule ligne ! -->
                   <xsl:with-param name="description" 
select="concat('Dans ''',$contextForErrors,''', la date ''',$s,''' 
n''est pas dans le bon format. Il se peut que le tri par date ne soit 
pas correct. Format attentu : JJ/MM/AAAA ou JJ/MM/AA au pire (on 
essayera de deviner le siC(cle...)')"/>
                 </xsl:call-template>
                 <xsl:sequence select="$s"/>
             </xsl:otherwise>
         </xsl:choose>
     </xsl:function>

   <xd:doc>
     <xd:desc>Returns a xs:date from an input string foratted at 
DD/MM/YYYY. See efl:makeISODate</xd:desc>
     <xd:param name="date">Input string DD/MM/YYYY</xd:param>
     <xd:return>Converted date, or 1900-01-01 if input could not be 
parsed</xd:return>
   </xd:doc>
   <xsl:function name="efl:getDate" as="xs:date?">
     <xsl:param name="date" as="xs:string"/>
       <xsl:variable name="isoDate" select="efl:makeIsoDate($date, 
'[^\d]', '', '')"/>
       <xsl:choose>
         <xsl:when test="$date castable as xs:date"><xsl:value-of 
select="$date"/></xsl:when>
         <xsl:when test="matches($isoDate,'\d{4}-\d{2}-\d{2}')">
           <xsl:variable name="split" select="tokenize($isoDate,'-')"/>
           <xsl:variable name="annee" select="number($split[1])"/>
           <xsl:variable name="maxJours" as="xs:integer">
             <xsl:choose>
             <xsl:when 
test="$split[2]=('01','03','05','07','08','10','12')">31</xsl:when>
             <xsl:when test="$split[2]=('04','06','09','11')">30</xsl:when>
             <xsl:when test="$annee mod 4=0 and (not($annee mod 100=0) 
or($annee mod 400=0)) and $split[2]='02'">29</xsl:when>
<xsl:otherwise>28</xsl:otherwise>
             </xsl:choose>
           </xsl:variable>
           <xsl:choose>
             <xsl:when test="number($split[3]) &lt;= 
$maxJours"><xsl:value-of select="xs:date($isoDate)"/></xsl:when>
             <xsl:otherwise><xsl:value-of 
select="xs:date('1900-01-01')"/></xsl:otherwise>
           </xsl:choose>
         </xsl:when>
           <xsl:otherwise><xsl:value-of 
select="xs:date('1900-01-01')"/></xsl:otherwise>
       </xsl:choose>
   </xsl:function>


Le 05/12/2016 C  05:03, Bridger Dyson-Smith bdysonsmith@xxxxxxxxx a C)crit :
> Hi all --
>
> I'm wondering about best practices when dealing with potentially 
> malformed dates. I confess that I haven't worked with dates (xs:date, 
> xs:time, xs:dateTime) very much and so I'm not sure if I'm thinking 
> about things correctly.
>
> I have the following input:
>     <test>
>         <field name="publication_date" type="date">
>             <value>2011-12-01T00:00-08:00</value>
>         </field>
>         <field name="embargo_date" type="date">
>             <value>2011-12-01T00:00-08:00</value>
>         </field>
>         <submission-date>2011-11-17T08:11:17-08:00</submission-date>
>         <publication-date>2011-12-01T00:00-08:00</publication-date>
>     </test>
>
> And I'd like to do a comparison check on the 
> field[@name='embargo_date'] vs the output of current-dateTime(). I 
> have the following stylesheet:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform 
> <http://www.w3.org/1999/XSL/Transform>"
>     xmlns:xs="http://www.w3.org/2001/XMLSchema 
> <http://www.w3.org/2001/XMLSchema>"
>     exclude-result-prefixes="xs"
>     version="2.0">
>
>     <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
>
>
>     <xsl:variable name="c-dateTime"
>                   select="dateTime(xs:date(format-date(current-date(), 
> '[Y]-[M,2]-[D,2]')),
> xs:time(format-time(current-time(), '[H]:[m]:[s][Z]')))"/>
>
>   <xsl:template match="/">
>       <xsl:variable name="test-dateTime" as="xs:dateTime" 
> select="test/field[@name='embargo_date']/value"/>
>
>       <result><xsl:value-of select="if ($test-dateTime ge $c-dateTime 
> ) then 'embargo me!' else 'all clear!'"/></result>
>     </xsl:template>
> </xsl:stylesheet>
>
> I'm using Saxon HE-9.6.0.7 in oXygen 18.1, which gives me a compile 
> time error of "FORG0001: Invalid dateTime value 
> "2018-12-01T00:00-08:00" (Wrong delimiter after minute)".
>
> Is the right thing to do here to do some string parsing on the 
> substrings in the values that I need to check? Is there some other way 
> to convert the value in field[@name='embargo_date'] into an xs:dateTime?
>
> Thanks for your time and trouble.
> Best,
> Bridger
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <-list/2837134> 
> (by email <>)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.