Docbook - Add Date and Time

By xngo on June 18, 2019

There are 2 ways to insert date and time in docbook.

Insert date and time using processing instruction

Here is an example in my Docbook file:

<pubdate><?dbtimestamp format="B d, Y at H:M:S"?></pubdate>
<!--Will generate: April 12, 2008 at 16:17:53-->
<!--More on format string can be found at http://www.sagehill.net/docbookxsl/Datetime.html -->

Insert date and time in the customization layer

  1. Add a marker inside your docbook. In my example, I assign my marker now to the role attribute:
    <pubdate role="now"></pubdate>
  2. In my customization layer, I'm using the EXSLT date-time() function supported by xsltprocprocessor. Here is what I put in my customization layer:
    <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:date="http://exslt.org/dates-and-times"> <!-- This declaration is needed in order to be able to use date and time function. -->
     
      <!-- Note the xmlns:date="..." above -->
      <xsl:template match="pubdate[@role='now']" mode="titlepage.mode">
        <xsl:variable name="now" select="date:date-time()"/>
        <fo:block>
          <xsl:text>Generated on </xsl:text>
          <!-- Month name -->
          <xsl:value-of select="date:month-name($now)"/>
          <xsl:text> </xsl:text>
     
          <!-- Day in month -->
          <xsl:call-template name="TimeDigits">
          <xsl:with-param name="iTime" select="date:day-in-month($now)"/>
          </xsl:call-template>
          <xsl:text>, </xsl:text>
     
          <!-- Year -->
          <xsl:value-of select="date:year($now)"/>
          <xsl:text> at </xsl:text>
     
          <!-- Hour -->
          <xsl:call-template name="TimeDigits">
          <xsl:with-param name="iTime" select="date:hour-in-day($now)"/>
          </xsl:call-template>
          <xsl:text>:</xsl:text>
     
          <!-- Minute -->
          <xsl:call-template name="TimeDigits">
          <xsl:with-param name="iTime" select="date:minute-in-hour($now)"/>
          </xsl:call-template>
          <xsl:text>:</xsl:text>
     
          <!-- Second -->
          <xsl:call-template name="TimeDigits">
          <xsl:with-param name="iTime" select="date:second-in-minute($now)"/>
          </xsl:call-template>
        </fo:block>
      </xsl:template>    
     
      <!-- Pad 0 to digit < 10 -->
      <xsl:template name="TimeDigits">
        <xsl:param name="iTime"/>
        <xsl:choose>
          <xsl:when test="$iTime &lt; 10"><xsl:value-of select="concat('0',$iTime)"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="$iTime"/></xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    </xsl:stylesheet>

Reference

  • http://www.sagehill.net/docbookxsl/Datetime.html

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.