Tags:
create new tag
view all tags

Question

I need to use something like %DATE% in a topic template but instead of the entire date I only want "year". The docs do not indicate if %DATE% can return a hard text "year" - trying it did not result in anything expected. Right now I am trying to munge something together with %CALC%.

Thanks

Environment

TWiki version: TWikiRelease01Feb2003
TWiki plugins: DefaultPlugin, ActionTrackerPlugin, BeautifierPlugin, EditTablePlugin, ExplicitNumberingPlugin, FindElsewherePlugin, InterwikiPlugin, PowerEditPlugin, QuickCalendarPlugin, SessionPlugin, SmiliesPlugin, SpreadSheetPlugin, TWikiDrawPlugin, TablePlugin, TopicVarsPlugin, TreePlugin
Server OS: Unix (solaris)
Web server: Apache/1.3.26 Server at twiki.sps.mot.com Port 80
Perl version: 5.6.0
Client OS: WindowsXPPro
Web Browser: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007

-- SteveRJones - 09 Feb 2004

Answer

I guess I kind of answered my own question but it took leveraging the SpreadSheetPlugin features. In my topic template I have:

<!-- Calculate only the year based on when this topic was created
   %CALC{"$SET(YEAR, $REPLACE(%DATE%, 1, $EVAL($LENGTH(%DATE%)-4),) )"}%
-->

<!-- Check it -->
CURRENTDATE = %DATE% %BR%
CURRENTYEAR = %CALC{"$GET(YEAR)"}%

The template hardcodes the entire date, vis-a-vis when the topic is created, via %DATE%. I then used %CALC% to extract just the year. Does anyone have a more elegant way of doing this?


Will not have %GMTIME{"$year"}% the same effect?
CURRENTYEAR = 2026

-- FranzJosefSilli - 11 Feb 2004

The problem is that putting %GMTIME{"$year"}% into a topic template results in %GMTIME{"$year"}% being put into the new topic. Using %DATE% in a topic template results in a hard date string in the new topic. So my example above, if the topic were created today the new topic would render as:

<!-- Calculate only the year based on when this topic was created
   %CALC{"$SET(YEAR, $REPLACE(11 Feb 2004, 1, $EVAL($LENGTH(11 Feb 2004)-4),) )"}%
-->

-- SteveRJones - 11 Feb 2004

Well, now I understand what you're trying. Can't the same thing you do be done by using

  %CALC{"$FORMATGMTIME( $TIME( %DATE% ), $year )"}%
%DATE% will be expanded to the full date string when creating the new topic based on the template and CALC formats that string into the desired year. --> no need to store it temporarily, just read the whole manual. wink

-- FranzJosefSilli - 11 Feb 2004

Yes, I suppose that would work too, and so might:

<!-- Calculate only the year based on when this topic was created
   %CALC{"$REPLACE(%DATE%, 1, $EVAL($LENGTH(%DATE%)-4),) )"}%
-->

I guess the real issue then becomes, which way is more expensive? Having 25 %CALC{"$FORMATGMTIME( $TIME( 2026-05-23 ), $year )"}% statements in a topic

OR

having 1 %CALC{"$SET(YEAR, $FORMATGMTIME( $TIME( 2026-05-23 ), $year ) )"}% statement

and

25 %CALC{"$GET(YEAR)"}% statements? It may not make a difference. I typically use variables just to assist in readability and debugging. But in essence I was looking for something like %DATE{$year}% that would generate the hard text year.

Thanks for the feedback, I think I'll file this as ANSWERED.

-- SteveRJones - 11 Feb 2004

Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2004-02-11 - SteveRJones
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2026 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.