<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> <#-- Macros for datetime formatting In this library, functions are used to format the datetime or interval according to a format string and precision, returning a raw string. Macros are used to generate the string with appropriate markup. --> <#-- MACROS --> <#-- Convenience display macros --> <#macro yearSpan dateTime> <#if dateTime?has_content> <@dateTimeSpan>${xsdDateTimeToYear(dateTime)} <#macro yearIntervalSpan startDateTime endDateTime endYearAsRange=true> <#local yearInterval = yearInterval(startDateTime, endDateTime, endYearAsRange)> <#if yearInterval?has_content> <@dateTimeSpan>${yearInterval} <#-- Display the datetime value or interval in a classed span appropriate for a property statement list --> <#macro dateTimeSpan> <#nested> <#-- FUNCTIONS --> <#-- Assign a year precision and generate the interval --> <#function yearInterval dateTimeStart dateTimeEnd endYearAsRange=true> <#local precision = "yearPrecision"> <#return dateTimeIntervalShort(dateTimeStart, precision, dateTimeEnd, precision, endYearAsRange)> <#-- Generate a datetime interval with dates displayed as "January 1, 2011" --> <#function dateTimeIntervalLong dateTimeStart precisionStart dateTimeEnd precisionEnd endAsRange=true> <#return dateTimeInterval(dateTimeStart, precisionStart, dateTimeEnd, precisionEnd, "long", endAsRange) > <#-- Generate a datetime interval with dates displayed as "1/1/2011" --> <#function dateTimeIntervalShort dateTimeStart precisionStart dateTimeEnd precisionEnd endAsRange=true> <#return dateTimeInterval(dateTimeStart, precisionStart, dateTimeEnd, precisionEnd, "short", endAsRange)> <#-- Generate a datetime interval --> <#function dateTimeInterval dateTimeStart precisionStart dateTimeEnd precisionEnd formatType="short" endAsRange=true> <#if dateTimeStart?has_content> <#local start = formatXsdDateTime(dateTimeStart, precisionStart, formatType)> <#if dateTimeEnd?has_content> <#local end = formatXsdDateTime(dateTimeEnd, precisionEnd, formatType)> <#local interval> <#if start?? && end??> ${start} - ${end} <#elseif start??> ${start} - <#elseif end??> <#if endAsRange>- ${end} <#return interval> <#-- Functions for formatting and applying precision to a datetime Currently these do more than format the datetime string, they select the precision as well. This should change in a future implementation; see NIHVIVO-1567. We want the Java code to apply the precision to the datetime string to pass only the meaningful data to the templates. The templates can format as they like, so these functions/macros would do display formatting but not data extraction. On the other hand, this is so easy that it may not be worth re-implementing to gain a bit more MVC compliance. --> <#-- Generate a datetime with date formatted as "January 1, 2011" --> <#function formatXsdDateTimeLong dateTime precision> <#return formatXsdDateTime(dateTime, precision, "long")> <#-- Generate a datetime with date formatted as "1/1/2011" --> <#function formatXsdDateTimeShort dateTime precision> <#return formatXsdDateTime(dateTime, precision, "short")> <#-- Generate a datetime as a year --> <#function xsdDateTimeToYear dateTime> <#local precision = "yearPrecision"> <#return formatXsdDateTime(dateTime, precision)> <#-- Apply a precision and format type to format a datetime --> <#function formatXsdDateTime dateTimeStr precision="" formatType="short"> <#-- First convert the string to a datetime object. For now, strip away time zone rather than displaying it. --> <#local dateTimeStr = dateTimeStr?replace("T", " ")?replace("Z.*$", "", "r")> <#local dateTimeStringFormat = "yyyy-MM-dd HH:mm:ss"> <#-- If a non-standard datetime format (e.g, "2000-04" from "2000-04"^^), just return the string without attempting to format. Possibly this should be handled in Java by examining the xsd type and making an appropriate conversion. --> <#if ! dateTimeStr?matches(dateTimeStringFormat)> <#return dateTimeStr> <#-- Convert the string to a datetime object. --> <#local dateTimeObj = dateTimeStr?datetime(dateTimeStringFormat)> <#if dateTimeObj == null> <#return "non-standard datetime string"> <#-- If no precision is specified, assign it from the datetime value. Pass dateTimeStr rather than dateTimeObj, because dateTimeObj replaces zeroes with default values, whereas we want to set precision based on whether the times values are all 0. --> <#if ! precision?has_content> <#local precision = getPrecision(dateTimeStr)> <#-- Get the format string for the datetime output --> <#local format = getFormat(formatType, precision)> <#return dateTimeObj?string(format)> <#function getPrecision dateTime> <#local match = dateTime?matches("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})")> <#list match as m> <#local hours = m?groups[4]?number> <#local minutes = m?groups[5]?number> <#local seconds = m?groups[6]?number> <#local precision> <#if hours == 0 && minutes == 0 && seconds == 0>yearMonthDayPrecision <#else>yearMonthDayTimePrecision <#return precision?trim> <#function getFormat formatType precision> <#-- Use the precision to determine which portion to display, and the format type to determine how to display it. --> <#local format> <#if formatType == "long"> <#if precision == "yearPrecision">yyyy <#elseif precision == "yearMonthPrecision">MMMM yyyy <#elseif precision == "yearMonthDayPrecision">MMMM d, yyyy <#else>MMMM d, yyyy h:mm a <#else> <#-- formatType == "short" --> <#if precision == "yearPrecision">yyyy <#elseif precision == "yearMonthPrecision">M/yyyy <#elseif precision == "yearMonthDayPrecision">M/d/yyyy <#else>M/d/yyyy h:mm a <#return format?trim>