2010-12-20 15:34:58 +00:00
|
|
|
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
|
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#-- Macros for datetime formatting
|
2010-12-20 15:34:58 +00:00
|
|
|
|
2011-01-10 15:48:52 +00:00
|
|
|
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 -->
|
2011-01-10 03:10:08 +00:00
|
|
|
<#macro yearSpan dateTime>
|
|
|
|
<#if dateTime?has_content>
|
2011-01-10 15:48:52 +00:00
|
|
|
<@dateTimeSpan>${xsdDateTimeToYear(dateTime)}</@dateTimeSpan>
|
2011-01-10 03:10:08 +00:00
|
|
|
</#if>
|
|
|
|
</#macro>
|
2011-01-06 21:19:42 +00:00
|
|
|
|
2011-01-18 04:42:19 +00:00
|
|
|
<#macro yearIntervalSpan startDateTime="" endDateTime="" endYearAsRange=true>
|
2011-01-10 03:10:08 +00:00
|
|
|
<#local yearInterval = yearInterval(startDateTime, endDateTime, endYearAsRange)>
|
2011-01-06 21:19:42 +00:00
|
|
|
<#if yearInterval?has_content>
|
2011-01-10 15:48:52 +00:00
|
|
|
<@dateTimeSpan>${yearInterval}</@dateTimeSpan>
|
2011-01-06 21:19:42 +00:00
|
|
|
</#if>
|
|
|
|
</#macro>
|
|
|
|
|
2011-01-10 15:48:52 +00:00
|
|
|
<#-- Display the datetime value or interval in a classed span appropriate for
|
|
|
|
a property statement list -->
|
|
|
|
<#macro dateTimeSpan>
|
|
|
|
<span class="listDateTime"><#nested></span>
|
|
|
|
</#macro>
|
|
|
|
|
|
|
|
<#-- FUNCTIONS -->
|
2011-01-10 02:21:21 +00:00
|
|
|
|
2011-01-10 03:10:08 +00:00
|
|
|
<#-- Assign a year precision and generate the interval -->
|
2011-01-18 04:42:19 +00:00
|
|
|
<#function yearInterval dateTimeStart="" dateTimeEnd="" endYearAsRange=true>
|
2011-01-10 03:10:08 +00:00
|
|
|
<#local precision = "yearPrecision">
|
|
|
|
<#return dateTimeIntervalShort(dateTimeStart, precision, dateTimeEnd, precision, endYearAsRange)>
|
|
|
|
</#function>
|
2011-01-10 02:21:21 +00:00
|
|
|
|
2011-01-10 03:10:08 +00:00
|
|
|
<#-- Generate a datetime interval with dates displayed as "January 1, 2011" -->
|
2011-01-18 04:42:19 +00:00
|
|
|
<#function dateTimeIntervalLong dateTimeStart="" precisionStart="" dateTimeEnd="" precisionEnd="" endAsRange=true>
|
2011-01-10 03:10:08 +00:00
|
|
|
<#return dateTimeInterval(dateTimeStart, precisionStart, dateTimeEnd, precisionEnd, "long", endAsRange) >
|
|
|
|
</#function>
|
|
|
|
|
|
|
|
<#-- Generate a datetime interval with dates displayed as "1/1/2011" -->
|
2011-01-18 04:42:19 +00:00
|
|
|
<#function dateTimeIntervalShort dateTimeStart="" precisionStart="" dateTimeEnd="" precisionEnd="" endAsRange=true>
|
2011-01-10 03:10:08 +00:00
|
|
|
<#return dateTimeInterval(dateTimeStart, precisionStart, dateTimeEnd, precisionEnd, "short", endAsRange)>
|
|
|
|
</#function>
|
|
|
|
|
|
|
|
<#-- Generate a datetime interval -->
|
2011-01-18 04:42:19 +00:00
|
|
|
<#function dateTimeInterval dateTimeStart="" precisionStart="" dateTimeEnd="" precisionEnd="" formatType="short" endAsRange=true>
|
2011-01-08 00:11:24 +00:00
|
|
|
|
|
|
|
<#if dateTimeStart?has_content>
|
2011-01-10 15:48:52 +00:00
|
|
|
<#local start = formatXsdDateTime(dateTimeStart, precisionStart, formatType)>
|
2011-01-08 00:11:24 +00:00
|
|
|
</#if>
|
|
|
|
|
|
|
|
<#if dateTimeEnd?has_content>
|
2011-01-10 15:48:52 +00:00
|
|
|
<#local end = formatXsdDateTime(dateTimeEnd, precisionEnd, formatType)>
|
2011-01-08 00:11:24 +00:00
|
|
|
</#if>
|
|
|
|
|
|
|
|
<#local interval>
|
|
|
|
<#if start?? && end??>
|
2011-01-31 05:10:52 +00:00
|
|
|
<#if start == end>
|
|
|
|
${start}
|
|
|
|
<#else>
|
2011-02-09 15:00:35 +00:00
|
|
|
${start} - ${end}
|
2011-01-31 05:10:52 +00:00
|
|
|
</#if>
|
2011-01-08 00:11:24 +00:00
|
|
|
<#elseif start??>
|
|
|
|
${start} -
|
|
|
|
<#elseif end??>
|
2011-02-09 15:00:35 +00:00
|
|
|
<#if endAsRange>- </#if>${end}
|
2011-01-08 00:11:24 +00:00
|
|
|
</#if>
|
|
|
|
</#local>
|
|
|
|
|
2011-02-09 15:00:35 +00:00
|
|
|
<#return interval?trim>
|
2011-01-10 03:10:08 +00:00
|
|
|
</#function>
|
2011-01-08 00:11:24 +00:00
|
|
|
|
|
|
|
<#-- 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.
|
2011-01-10 03:10:08 +00:00
|
|
|
|
|
|
|
On the other hand, this is so easy that it may not be worth re-implementing to gain a bit more MVC compliance.
|
2010-12-20 15:34:58 +00:00
|
|
|
-->
|
|
|
|
|
2011-01-10 03:10:08 +00:00
|
|
|
<#-- Generate a datetime with date formatted as "January 1, 2011" -->
|
2011-01-10 02:21:21 +00:00
|
|
|
<#function formatXsdDateTimeLong dateTime precision>
|
|
|
|
<#return formatXsdDateTime(dateTime, precision, "long")>
|
|
|
|
</#function>
|
|
|
|
|
2011-01-10 03:10:08 +00:00
|
|
|
<#-- Generate a datetime with date formatted as "1/1/2011" -->
|
2011-01-10 02:21:21 +00:00
|
|
|
<#function formatXsdDateTimeShort dateTime precision>
|
2011-01-18 00:25:42 +00:00
|
|
|
<#return formatXsdDateTime(dateTime, precision, "short")>
|
2011-01-08 00:11:24 +00:00
|
|
|
</#function>
|
|
|
|
|
2011-01-10 03:10:08 +00:00
|
|
|
<#-- Generate a datetime as a year -->
|
|
|
|
<#function xsdDateTimeToYear dateTime>
|
|
|
|
<#local precision = "yearPrecision">
|
|
|
|
<#return formatXsdDateTime(dateTime, precision)>
|
|
|
|
</#function>
|
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#-- Apply a precision and format type to format a datetime -->
|
2011-01-18 00:46:30 +00:00
|
|
|
<#function formatXsdDateTime dateTimeStr precision="" formatType="short">
|
2011-01-18 04:42:19 +00:00
|
|
|
|
2011-01-18 01:35:28 +00:00
|
|
|
<#-- First convert the string to a format that Freemarker can interpret as a datetime.
|
2011-01-18 00:25:42 +00:00
|
|
|
For now, strip away time zone rather than displaying it. -->
|
2011-01-18 01:35:28 +00:00
|
|
|
<#local dateTimeStr = dateTimeStr?replace("T", " ")?replace("Z.*$", "", "r")?trim>
|
2011-01-18 00:25:42 +00:00
|
|
|
|
2011-01-18 00:46:30 +00:00
|
|
|
<#-- If a non-standard datetime format (e.g, "2000-04" from
|
|
|
|
"2000-04"^^<http://www.w3.org/2001/XMLSchema#gYearMonth>), 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. -->
|
2011-01-18 01:35:28 +00:00
|
|
|
<#if ! dateTimeStr?matches("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})")>
|
2011-01-18 00:46:30 +00:00
|
|
|
<#return dateTimeStr>
|
2011-01-18 00:25:42 +00:00
|
|
|
</#if>
|
|
|
|
|
|
|
|
<#-- Convert the string to a datetime object. -->
|
2011-01-18 01:35:28 +00:00
|
|
|
<#local dateTimeObj = dateTimeStr?datetime("yyyy-MM-dd HH:mm:ss")>
|
2011-01-18 00:46:30 +00:00
|
|
|
|
|
|
|
<#-- 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)>
|
|
|
|
</#if>
|
2011-01-18 00:25:42 +00:00
|
|
|
|
|
|
|
<#-- Get the format string for the datetime output -->
|
|
|
|
<#local format = getFormat(formatType, precision)>
|
|
|
|
|
2011-01-18 00:46:30 +00:00
|
|
|
<#return dateTimeObj?string(format)>
|
2011-01-10 03:10:08 +00:00
|
|
|
</#function>
|
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#function getPrecision dateTime>
|
2011-01-08 00:11:24 +00:00
|
|
|
|
2011-01-18 01:35:28 +00:00
|
|
|
<#-- We know this will match because the format has already been checked -->
|
2011-01-18 00:25:42 +00:00
|
|
|
<#local match = dateTime?matches("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})")>
|
2011-01-18 01:35:28 +00:00
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#list match as m>
|
|
|
|
<#local hours = m?groups[4]?number>
|
|
|
|
<#local minutes = m?groups[5]?number>
|
|
|
|
<#local seconds = m?groups[6]?number>
|
|
|
|
</#list>
|
2011-01-08 00:11:24 +00:00
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#local precision>
|
|
|
|
<#if hours == 0 && minutes == 0 && seconds == 0>yearMonthDayPrecision
|
|
|
|
<#else>yearMonthDayTimePrecision
|
|
|
|
</#if>
|
|
|
|
</#local>
|
|
|
|
|
|
|
|
<#return precision?trim>
|
|
|
|
</#function>
|
|
|
|
|
|
|
|
<#function getFormat formatType precision>
|
2011-01-08 00:11:24 +00:00
|
|
|
<#-- Use the precision to determine which portion to display,
|
2011-01-18 00:25:42 +00:00
|
|
|
and the format type to determine how to display it. -->
|
2011-01-08 00:11:24 +00:00
|
|
|
<#local format>
|
2011-01-10 15:48:52 +00:00
|
|
|
<#if formatType == "long">
|
2011-01-08 00:11:24 +00:00
|
|
|
<#if precision == "yearPrecision">yyyy
|
|
|
|
<#elseif precision == "yearMonthPrecision">MMMM yyyy
|
|
|
|
<#elseif precision == "yearMonthDayPrecision">MMMM d, yyyy
|
|
|
|
<#else>MMMM d, yyyy h:mm a
|
|
|
|
</#if>
|
2011-01-10 15:48:52 +00:00
|
|
|
<#else> <#-- formatType == "short" -->
|
2011-01-18 00:25:42 +00:00
|
|
|
<#if precision == "yearPrecision">yyyy
|
2011-01-10 02:21:21 +00:00
|
|
|
<#elseif precision == "yearMonthPrecision">M/yyyy
|
|
|
|
<#elseif precision == "yearMonthDayPrecision">M/d/yyyy
|
|
|
|
<#else>M/d/yyyy h:mm a
|
2011-01-08 00:11:24 +00:00
|
|
|
</#if>
|
|
|
|
</#if>
|
|
|
|
</#local>
|
|
|
|
|
2011-01-18 00:25:42 +00:00
|
|
|
<#return format?trim>
|
2011-01-06 21:19:42 +00:00
|
|
|
</#function>
|
2011-01-18 01:35:28 +00:00
|
|
|
|
2011-01-06 21:19:42 +00:00
|
|
|
|