You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Townson, Chris" <C....@nature.com> on 2006/04/27 17:57:13 UTC

DateTool and human-friendly date formats

Hi,

I'm need to write a method which outputs a supplied Date object in a "human-friendly" format (e.g. "5 minutes ago", "3 hours ago", "4 days ago" etc)

My initial impulse was to sit down and sub-class DateTool, adding a method that compares the supplied date to the current date and so on ... 

... In the interests of DRY: has anyone here already done anything like this or got tips/tricks?

Chris

********************************************************************************   
DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
not the original intended recipient. If you have received this e-mail in error
please inform the sender and delete it from your mailbox or any other storage
mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
liability for any statements made which are clearly the sender's own and not
expressly made on behalf of Macmillan Publishers Limited or one of its agents.
Please note that neither Macmillan Publishers Limited nor any of its agents
accept any responsibility for viruses that may be contained in this e-mail or
its attachments and it is your responsibility to scan the e-mail and 
attachments (if any). No contracts may be concluded on behalf of Macmillan 
Publishers Limited or its agents by means of e-mail communication. Macmillan 
Publishers Limited Registered in England and Wales with registered number 785998 
Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS   
********************************************************************************

Re: DateTool and human-friendly date formats

Posted by Nathan Bubna <nb...@gmail.com>.
Christoph,  that is one serious macro you've got! ;-)  Just so i never
have to see one of those again, i am going to make sure and get
"standalone" toolbox management into VelocityTools asap. :)

Chris, if you come across a way to do this in a DateTool subclass or
write the code yourself, i would happily test it out and add it to
DateTool.  this seems like a very useful feature.

On 4/28/06, apache@recks.org <ap...@recks.org> wrote:
> Hi Chris,
>
> this seems to be a classical "view" task. You can put
> the code into a tool (possibly a subclass of DateTool);
> but you can also accommodate such a thing in a macro.
>
> I have done something similar (OK, not the cleanest approach,
> but it works. Part of this could have been done with the
> DateTool and NumberTool, but I don't have the ToolboxManager
> in my commandline velocity utility...):
>
> ## ------------------------------------------------------------------------
> ## convenience helper tools:
> ## ------------------------------------------------------------------------
> #set( $Integer = 1 )
> #set( $Long = $Integer.longValue() )
> #set( $cdLimit = $Long.valueOf("640000000") )## example use of Long.class
> ##
> ## ------------------------------------------------------------------------
> ## convenience directive to invoke a method and ignore the return value
> ## ------------------------------------------------------------------------
> #macro( call $foo )#if($foo)#**##end#end
> ##
> ## ------------------------------------------------------------------------
> ## Number stringify helpers
> ## ------------------------------------------------------------------------
> #macro( digits4 $number )#*
>    *##set( $id = "$number" )#*         -- access the macro paramter, ensure its a String
>    *##set( $id = "0000$id.trim()" )#*  -- prepend the padding, clipping whitespace artefacts
>    *##set( $len = $id.length() - 4 )#* -- compute the offset for the rest length
>    *#$id.substring($len)##             -- emit the resulting padded string
> #end
> #macro( digits2 $number )#*
>    *##set( $id = "$number" )#*
>    *##set( $id = "00$id.trim()" )#*
>    *##set( $len = $id.length() - 2 )#*
>    *#$id.substring($len)##
> #end
> ##
> ## ------------------------------------------------------------------------
> ## Date operations and formatting
> ## ------------------------------------------------------------------------
> #macro( dateStr $d )#*
>    *##set( $y = $d.year + 1900 )#*
>    *##digits4($y)#digits2($d.month)#digits2($d.date)#digits2($d.hours)#digits2($d.minutes)#digits2($d.seconds)##
> #end
> ##
> #macro( addDateDuration $d $duration )##
> #set( $d = $Integer.parseInt($duration) )
> ##duration = $duration = $d
> #set( $Y = $Integer.parseInt($startDay.substring(0,4)) - 1900 )
> #set( $M = $Integer.parseInt($startDay.substring(4,6)) )
> #set( $D = $Integer.parseInt($startDay.substring(6,8)) )
> #set( $h = $Integer.parseInt($startTime.substring(0,2)) )
> #set( $m = $Integer.parseInt($startTime.substring(2,4)) )
> #set( $s = $Integer.parseInt($startTime.substring(4,6)) )
> ##uncomment for debugging: #digits2($Y)-#digits2($M)-#digits2($D) #digits2($s):#digits2($m):#digits2($s)#**#
> #set( $D = $D + ($d / 86400) )
> #set( $d = $d - (($d / 86400) * 86400) )
> #set( $h = $h + ($d / 3600) )
> #set( $d = $d - (($d / 3600) * 3600) )
> #set( $m = $m + ($d / 60) )
> #set( $d = $d - (($d / 60) * 60) )
> #set( $s = $s + $d )
> ##uncomment for debugging: #digits2($Y)-#digits2($M)-#digits2($D) #digits2($s):#digits2($m):#digits2($s)#**#
> #set( $d = $date.clone() )
> #call( $d.setTime( $Integer.longValue() ) )
> #call( $d.setYear( $Y ) )
> #call( $d.setMonth( $M ) )
> #call( $d.setDate( $D ) )
> #call( $d.setHours( $h ) )
> #call( $d.setMinutes( $m ) )
> #call( $d.setSeconds( $s ) )
> #dateStr( $d )##
> #end
> ##
> ## ------------------------------------------------------------------------
> ## Macro to write a text to a file.
> ## ------------------------------------------------------------------------
> #macro( fileWrite $filename $text )
>    #set( $out = $Class.newInstance("java.io.FileOutputStream", $filename) )
>    #set( $data = $text.getBytes() )
>    #call( $out.write($data) )
>    #call( $out.close() )
> #end
> ##
> ## example of a "switch" statement:
> #macro( SensorName $productId )#*
>    *##if( $productId.startsWith("ASA") )ASAR#*
>    *##elseif( $productId.startsWith("ATS") )ATSR#*
>    *##elseif( $productId.startsWith("GOM") )GOMOS#*
>    *##elseif( $productId.startsWith("MER") )MERIS#*
>    *##elseif( $productId.startsWith("MIP") )MIPAS#*
>    *##elseif( $productId.startsWith("RA2") )ALTIMETER#*
>    *##elseif( $productId.startsWith("SCI") )SCIAMACHY#*
>    *##elseif( $productId.startsWith("DOR") )DORIS#*
>    *##elseif( $productId.startsWith("MWR") )MICROWAVE#*
>    *##else#**#UNKNOWN#*
>    *##end##
> #end
> ...
> <SensorName>    #SensorName( $productId )#*this comment is needed to avoid the macro gobbling the EOL*#
> ...
> <TrackNo>       $Integer.parseInt($relativeOrbit)
> ...
> <AcqEndDateTime>        #addDateDuration( "$startDay$startTime" $duration ).000
>
>
> It's your freedom to setup either a tool or a macro to
> assist generating the "view" output you need.
>
> Cheers,
> Christoph
>
> Townson, Chris wrote:
> > Hi,
> >
> > I'm need to write a method which outputs a supplied Date object in a "human-friendly" format (e.g. "5 minutes ago", "3 hours ago", "4 days ago" etc)
> >
> > My initial impulse was to sit down and sub-class DateTool, adding a method that compares the supplied date to the current date and so on ...
> >
> > ... In the interests of DRY: has anyone here already done anything like this or got tips/tricks?
> >
> > Chris
> >
> > ********************************************************************************
> > DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
> > not the original intended recipient. If you have received this e-mail in error
> > please inform the sender and delete it from your mailbox or any other storage
> > mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
> > liability for any statements made which are clearly the sender's own and not
> > expressly made on behalf of Macmillan Publishers Limited or one of its agents.
> > Please note that neither Macmillan Publishers Limited nor any of its agents
> > accept any responsibility for viruses that may be contained in this e-mail or
> > its attachments and it is your responsibility to scan the e-mail and
> > attachments (if any). No contracts may be concluded on behalf of Macmillan
> > Publishers Limited or its agents by means of e-mail communication. Macmillan
> > Publishers Limited Registered in England and Wales with registered number 785998
> > Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS
> > ********************************************************************************
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: DateTool and human-friendly date formats

Posted by ap...@recks.org.
Hi Chris,

this seems to be a classical "view" task. You can put
the code into a tool (possibly a subclass of DateTool);
but you can also accommodate such a thing in a macro.

I have done something similar (OK, not the cleanest approach,
but it works. Part of this could have been done with the
DateTool and NumberTool, but I don't have the ToolboxManager
in my commandline velocity utility...):

## ------------------------------------------------------------------------
## convenience helper tools:
## ------------------------------------------------------------------------
#set( $Integer = 1 )
#set( $Long = $Integer.longValue() )
#set( $cdLimit = $Long.valueOf("640000000") )## example use of Long.class
##
## ------------------------------------------------------------------------
## convenience directive to invoke a method and ignore the return value
## ------------------------------------------------------------------------
#macro( call $foo )#if($foo)#**##end#end
##
## ------------------------------------------------------------------------
## Number stringify helpers
## ------------------------------------------------------------------------
#macro( digits4 $number )#*
   *##set( $id = "$number" )#*         -- access the macro paramter, ensure its a String
   *##set( $id = "0000$id.trim()" )#*  -- prepend the padding, clipping whitespace artefacts
   *##set( $len = $id.length() - 4 )#* -- compute the offset for the rest length
   *#$id.substring($len)##             -- emit the resulting padded string
#end
#macro( digits2 $number )#*
   *##set( $id = "$number" )#*
   *##set( $id = "00$id.trim()" )#*
   *##set( $len = $id.length() - 2 )#*
   *#$id.substring($len)##
#end
##
## ------------------------------------------------------------------------
## Date operations and formatting
## ------------------------------------------------------------------------
#macro( dateStr $d )#*
   *##set( $y = $d.year + 1900 )#*
   *##digits4($y)#digits2($d.month)#digits2($d.date)#digits2($d.hours)#digits2($d.minutes)#digits2($d.seconds)##
#end
##
#macro( addDateDuration $d $duration )##
#set( $d = $Integer.parseInt($duration) )
##duration = $duration = $d
#set( $Y = $Integer.parseInt($startDay.substring(0,4)) - 1900 )
#set( $M = $Integer.parseInt($startDay.substring(4,6)) )
#set( $D = $Integer.parseInt($startDay.substring(6,8)) )
#set( $h = $Integer.parseInt($startTime.substring(0,2)) )
#set( $m = $Integer.parseInt($startTime.substring(2,4)) )
#set( $s = $Integer.parseInt($startTime.substring(4,6)) )
##uncomment for debugging: #digits2($Y)-#digits2($M)-#digits2($D) #digits2($s):#digits2($m):#digits2($s)#**#
#set( $D = $D + ($d / 86400) )
#set( $d = $d - (($d / 86400) * 86400) )
#set( $h = $h + ($d / 3600) )
#set( $d = $d - (($d / 3600) * 3600) )
#set( $m = $m + ($d / 60) )
#set( $d = $d - (($d / 60) * 60) )
#set( $s = $s + $d )
##uncomment for debugging: #digits2($Y)-#digits2($M)-#digits2($D) #digits2($s):#digits2($m):#digits2($s)#**#
#set( $d = $date.clone() )
#call( $d.setTime( $Integer.longValue() ) )
#call( $d.setYear( $Y ) )
#call( $d.setMonth( $M ) )
#call( $d.setDate( $D ) )
#call( $d.setHours( $h ) )
#call( $d.setMinutes( $m ) )
#call( $d.setSeconds( $s ) )
#dateStr( $d )##
#end
##
## ------------------------------------------------------------------------
## Macro to write a text to a file.
## ------------------------------------------------------------------------
#macro( fileWrite $filename $text )
   #set( $out = $Class.newInstance("java.io.FileOutputStream", $filename) )
   #set( $data = $text.getBytes() )
   #call( $out.write($data) )
   #call( $out.close() )
#end
##
## example of a "switch" statement:
#macro( SensorName $productId )#*
   *##if( $productId.startsWith("ASA") )ASAR#*
   *##elseif( $productId.startsWith("ATS") )ATSR#*
   *##elseif( $productId.startsWith("GOM") )GOMOS#*
   *##elseif( $productId.startsWith("MER") )MERIS#*
   *##elseif( $productId.startsWith("MIP") )MIPAS#*
   *##elseif( $productId.startsWith("RA2") )ALTIMETER#*
   *##elseif( $productId.startsWith("SCI") )SCIAMACHY#*
   *##elseif( $productId.startsWith("DOR") )DORIS#*
   *##elseif( $productId.startsWith("MWR") )MICROWAVE#*
   *##else#**#UNKNOWN#*
   *##end##
#end
...
<SensorName>	#SensorName( $productId )#*this comment is needed to avoid the macro gobbling the EOL*#
...
<TrackNo>	$Integer.parseInt($relativeOrbit)
...
<AcqEndDateTime>	#addDateDuration( "$startDay$startTime" $duration ).000


It's your freedom to setup either a tool or a macro to
assist generating the "view" output you need.

Cheers,
Christoph

Townson, Chris wrote:
> Hi,
> 
> I'm need to write a method which outputs a supplied Date object in a "human-friendly" format (e.g. "5 minutes ago", "3 hours ago", "4 days ago" etc)
> 
> My initial impulse was to sit down and sub-class DateTool, adding a method that compares the supplied date to the current date and so on ... 
> 
> ... In the interests of DRY: has anyone here already done anything like this or got tips/tricks?
> 
> Chris
> 
> ********************************************************************************   
> DISCLAIMER: This e-mail is confidential and should not be used by anyone who is
> not the original intended recipient. If you have received this e-mail in error
> please inform the sender and delete it from your mailbox or any other storage
> mechanism. Neither Macmillan Publishers Limited nor any of its agents accept
> liability for any statements made which are clearly the sender's own and not
> expressly made on behalf of Macmillan Publishers Limited or one of its agents.
> Please note that neither Macmillan Publishers Limited nor any of its agents
> accept any responsibility for viruses that may be contained in this e-mail or
> its attachments and it is your responsibility to scan the e-mail and 
> attachments (if any). No contracts may be concluded on behalf of Macmillan 
> Publishers Limited or its agents by means of e-mail communication. Macmillan 
> Publishers Limited Registered in England and Wales with registered number 785998 
> Registered Office Brunel Road, Houndmills, Basingstoke RG21 6XS   
> ********************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org