Bug 75981 - [templates][preferences] Allow to specify format of date variable in templates
Summary: [templates][preferences] Allow to specify format of date variable in templates
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.0.1   Edit
Hardware: All All
: P5 enhancement with 27 votes (vote)
Target Milestone: 4.6 M5   Edit
Assignee: Jeremie Bresson CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted, noteworthy
: 105802 153445 169418 303336 347399 (view as bug list)
Depends on:
Blocks: 486532 486748
  Show dependency tree
 
Reported: 2004-10-09 06:48 EDT by Freddie Schwenke CLA
Modified: 2016-02-05 06:57 EST (History)
24 users (show)

See Also:
daniel_megert: review-


Attachments
Proposed patch to add ${day_of_month} and ${month} templates (10.71 KB, patch)
2008-05-02 10:49 EDT, Michael Pellaton CLA
daniel_megert: review-
Details | Diff
Patch as described in comment 31. (2.81 KB, patch)
2012-04-03 16:52 EDT, Thomas Reinhardt CLA
daniel_megert: review-
Details | Diff
Fix as per Dani's comment (3.13 KB, patch)
2013-09-26 23:51 EDT, Pramod Goyal CLA
no flags Details | Diff
Document update (975 bytes, patch)
2013-09-27 10:35 EDT, Pramod Goyal CLA
pramod.goyal: review?
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Freddie Schwenke CLA 2004-10-09 06:48:45 EDT
Currently the format of the $(date) variable in the templates is MMM dd, yyyy.  
I would like to be able to change this, since I am using it as part of a 
template when creating new source files.  From the news groups, it is suggested 
that one should be able to change the locale setting at startup time, but that 
seems to have no effect.

Previously (2.x) the format was yyyy/mm/dd.  Personally I liked this one 
better, however, I think it should be customizable.

Thanks.
Comment 1 Olivier Thomann CLA 2004-10-09 08:53:24 EDT
Move to JDT/UI
Comment 2 Dirk Baeumer CLA 2004-10-11 06:35:52 EDT
Moving to text.
Comment 3 Dani Megert CLA 2004-10-11 07:03:09 EDT
It takes the date format of your operating system (locale) settings. There are
currently no plans to offer locale customization preferences.
Comment 4 Marcio CLA 2006-05-01 16:19:41 EDT
The problem of using the developer's machine's formatter is that if you have a developer in the US commit code and have a developer in Canada commit code, they will be in different formats. So, if you do a build and have a custom processor to do any kind of processing on these date tags, you have no idea how to parse the date.

In my concrete case I have a custom doclet that we wrote which traverses the code looking at our own custom tag

mytag ${date}

    I want a uniform date format for the doclet processing, not a locale-dependent one. In other words, I'd like to specify that I want a ${date} in Eclipse, but I want to pass the pattern string to the formatter too. Like this:
       
    ${date:YYYY-MM-DD}
    
    (inspired by the .NET format syntax - see http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx )
    
    Unfortunately this does not work.

    Note that the heuristics used when DateFormat.getDateInstance().setLenient(true) is on don't seem to be that good (it fails even across en-ca and en-us).

    This makes the @date tag and any other that relies on dates absolutely useless for tools that want to traverse the code & do processing based on date values. Java can't parse them in any fuzzy way (infer the format from the value) and Eclipse can't be tweaked to do it in a given standard format for the tools either. We're are stuck.

   Note that even humans browsing Javadoc may have a problem because the code will have a mix of date formats.

   Can the support for passing the format pattern string be added, please? Thanks.

Comment 5 Dani Megert CLA 2006-05-02 10:02:03 EDT
.
Comment 6 Marcio CLA 2006-07-20 21:33:12 EDT
Unfortunately we can't even use a workaround like

${year}-${month}-${day}

because ${year} is a valid variable in the templates but ${month} and ${day} aren't.

Can we at least get ${month} and ${day} added since ${year} already is, anyway?
Comment 7 Dani Megert CLA 2006-07-21 04:23:44 EDT
>Can we at least get ${month} and ${day} added since ${year} already is, anyway?
No plans to work on this but if you send us a good quality patch we will add it.
Comment 8 Marcio CLA 2006-07-26 10:59:47 EDT
I don't know where to start in the wad, to try to add {month} and {day}. 

What are the classes that contribute {year}? Knowing that would help tremendously.
Comment 9 Tom Hofmann CLA 2006-07-26 11:14:23 EDT
@see org.eclipse.jface.text.templates.GlobalTemplateVariables.Year
Comment 10 Dani Megert CLA 2006-08-15 03:45:28 EDT
*** Bug 153445 has been marked as a duplicate of this bug. ***
Comment 11 Dani Megert CLA 2006-08-15 03:46:42 EDT
*** Bug 105802 has been marked as a duplicate of this bug. ***
Comment 12 Markus Keller CLA 2006-08-15 04:37:00 EDT
The ${date} variable could be enhanced to accept an argument (similar to other parameterizations added in 3.3M1), e.g. ${d:date(format)}, where format is a pattern for SimpleDateFormat.
Comment 13 Dani Megert CLA 2007-01-03 10:27:18 EST
*** Bug 169418 has been marked as a duplicate of this bug. ***
Comment 14 Michael Pellaton CLA 2008-05-02 10:49:48 EDT
Created attachment 98429 [details]
Proposed patch to add ${day_of_month} and ${month} templates

The proposed patch I attached adds two new global code templates (similar to ${year}):
${day_of_month} evaluates to the current day number
${month} evaluates to the current month number (January = 01)

Both numbers are printed using two digits

Example: 
Today, a template using ${year}${month}${day_of_moth} evaluates to: 20080502
Comment 15 Dani Megert CLA 2008-05-05 04:47:13 EDT
To be looked at during 3.5.
Comment 16 Dani Megert CLA 2009-01-26 03:25:36 EST
Patch to be assessed during M6.
Comment 17 Dani Megert CLA 2009-02-04 06:41:21 EST
I don't like the patch as it introduces the same problem that this bug wants to solve: it hard-codes the formatting by adding a '0' ;-)

The correct fix is outlined in comment 12.
Comment 18 Markus Keller CLA 2009-02-04 07:14:28 EST
The date variable should also take a locale string as optional second argument, i.e. the documentation should define it as

    ${id:date(format[,locale])}

The locale should be a java.util.Locale identifier, separated by '_', see Locale#toString().
Comment 19 Dani Megert CLA 2010-02-22 02:49:33 EST
*** Bug 303336 has been marked as a duplicate of this bug. ***
Comment 20 douglasmhurst CLA 2010-02-22 11:24:01 EST
(In reply to comment #19)
> *** Bug 303336 has been marked as a duplicate of this bug. ***
Comment 21 douglasmhurst CLA 2010-02-22 11:31:48 EST
Comments 15 and 16 indicate this will be looked at in 3.5 and M6 respectively.  This was back in 2008.  3.5 does not appear to addressed this issue.  Is there an update on when it may be addressed?
Comment 22 Dani Megert CLA 2010-02-22 11:39:39 EST
>Comments 15 and 16 indicate this will be looked at in 3.5 and M6 respectively.
Which we did, see comment 17.

No work planned until we get another patch.
Comment 23 Romain Loutrel CLA 2011-05-03 12:12:10 EDT
Hello together,

I wish I could support you on this bug fixing but I need some help to help us:

1/ I do not have the sources of the eclipse platform. Does an how to FAQ, etc exists? Since I am using Ubuntu 10.04, I will do my development based on the platform 3.5 (not 3.6). But if accepted I will submit it also for 3.6

2/ Please confirm the following specifications:

Use of the already existing variable ${date}
1/ Replaced by
${date(format)}

(format) is optional
ie:
use example 1 ${date} ==> actual value
use example 2 ${date(yyyy-mm-dd)} ==> 2011-04-04

if format does not exist, the current version will be used.

format will be buid on the use of the following keywords:
YEAR:
y
yy
yyy
yyyy : The expected numbers of characters (ie y=1; yy=11; yyy=011; yyyy=2011)
Y* : alternative use, not defined yet
==> I can also do y= 2 char; Y= 4 chars

MONTH (just for Dani MEGERT):
m  : Month number as short as possible  ( 1 if january ; 10 if october)
mm : Month number with 0 pre-completion (01 if january; 10 if october)

M  : Month short name in the locale used by eclipse (if this concept exists)
MM : Month full name in the locale used by eclipse 

DAY:
d  : Day number as short as possible           ( 4=4th day ; 10=10th day)
dd : Day number as short with 0 pre-completion (04=4th day ; 10=10th day)
==> if someone want 0024 for the 24th of january he will be able to do it using 00dd


Any other character would be printed out like found in the string
To print out 'm' 's' 'd' or '\' the use of '\' will be used to escape

For escape char, if '\' does not fit with the used standards, please tell me which char must be used.

I will probably need some support especially about the coding rules of the project (documentation, delivery process, bug management). Please do not tell me RTFM but tell me instead where TFM takes place.
Comment 24 Romain Loutrel CLA 2011-05-03 13:07:03 EDT
(In reply to comment #23)
> Hello together,
> 
> (...)
> 
> I will probably need some support especially about the coding rules of the
> project (documentation, delivery process, bug management). Please do not tell
> me RTFM but tell me instead where TFM takes place.

Finally I found the info:
http://wiki.eclipse.org/Platform_UI/How_to_Contribute
Comment 25 Markus Keller CLA 2011-05-03 13:44:10 EDT
The spec is in comment 12 and comment 18. We won't accept a complicated patch that invents date formatting again.

See comment 9 for an entry point to start coding. The code is in org.eclipse.text in :pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse
Comment 26 Romain Loutrel CLA 2011-05-03 18:01:29 EDT
(In reply to comment #25)
> The spec is in comment 12 and comment 18. We won't accept a complicated patch
> that invents date formatting again.
> 
> See comment 9 for an entry point to start coding. The code is in
> org.eclipse.text in :pserver:anonymous@dev.eclipse.org:/cvsroot/eclipse

Well actually, the date format of Class SimpleDateFormat is something I had in mind even if I did not know it.
Okay for that (it will even make the things easier since it is already available, just an interface to create between template and java ...)

What is still not clear for me is which one of the 2 must be used?
${d:date(format)} and ${id:date(format[,locale])}
Comment 27 Markus Keller CLA 2011-05-04 07:12:18 EDT
(In reply to comment #26)
> What is still not clear for me is which one of the 2 must be used?
> ${d:date(format)} and ${id:date(format[,locale])}

Why do you think the older comment would win over the newer one?
Comment 28 Dani Megert CLA 2011-05-27 05:07:24 EDT
*** Bug 347399 has been marked as a duplicate of this bug. ***
Comment 29 Thomas Reinhardt CLA 2012-03-30 11:10:53 EDT
(In reply to comment #26)
Romain, have you been able to work on this issue? If not I would fix it.
Comment 30 Romain Loutrel CLA 2012-03-31 12:54:21 EDT
(In reply to comment #29)
> (In reply to comment #26)
> Romain, have you been able to work on this issue? If not I would fix it.

Hi Thomas,

unfortunatly I do not have time to do this fix (new job,  etc...) and I do not plan to work on that (I do not know the Eclipse architecture and would require a lot of time for this fix).

I will thank you if you do it!

Good luck and I hope one day I will come back to help the team.
Comment 31 Thomas Reinhardt CLA 2012-04-03 16:51:41 EDT
I have created a patch that implements the proposal in comment 18.
Comment 32 Thomas Reinhardt CLA 2012-04-03 16:52:57 EDT
Created attachment 213533 [details]
Patch as described in comment 31.
Comment 33 Dani Megert CLA 2012-04-10 11:58:02 EDT
Thanks for the patch Thomas. It works, but needs some polish:

- It should not fail with exceptions when the format or locale is wrong.
  Instead, it should just delegate it to the super method.

- The existing default format must be kept if no arguments are given.

- You should not use Locale.getDefault(), if no locale is specified.

- The patch misses to set whether the variable is ambiguous or not.

- The description of the variable must be improved and explain what the valid
  formats are.

- The documentation (F1) needs to be updated. See
  org.eclipse.jdt.doc.user/concepts/concept-template-variables.htm

- Each changed file needs an entry in the copyright, e.g.
  Dani Megert <dani@eclipse.org> - this is a bug - https://bugs.eclipse.org/75981

- Please use the same formatting style as in the existing code.
Comment 34 Pramod Goyal CLA 2013-09-24 22:47:01 EDT
I would like to take this up and finish it. Can this be assigned to me. I am new to this so not sure if the bug needs to be assigned to me before i can work on it
Comment 35 Markus Keller CLA 2013-09-25 13:58:41 EDT
(In reply to Pramod Goyal from comment #34)
You can just attach a patch here. We usually don't assign a bug to an external contributor unless it's a bigger project and we've already seen some promising results.
Comment 36 Pramod Goyal CLA 2013-09-26 23:51:05 EDT
Created attachment 235875 [details]
Fix as per Dani's comment
Comment 37 Pramod Goyal CLA 2013-09-26 23:53:11 EDT
Can someone please let me know how to update the document for this ? As per Dani's comment we need to update the document:

- The documentation (F1) needs to be updated. See
  org.eclipse.jdt.doc.user/concepts/concept-template-variables.htm

However i couldn't find the concept-template-variable.htm anywhere
Comment 38 Philippe Faes CLA 2013-09-27 02:54:54 EDT
I guess he wanted the documentation in the JDT plugin to reflect the changes:

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm

Try looking in for the HTML file in the org.eclipse.jdt.doc.user plugin.

Note that the bug/feature itself relates not to org.eclipse.jdt.*, but to org.eclipse.text!
Comment 39 Markus Keller CLA 2013-09-27 06:16:13 EDT
Use File > Import... > Plug-ins and Fragments, and select "Projects from a repository" to import the org.eclipse.jdt.doc.user bundle.

Nag in bug 395143 if you don't like the wrong default location for the cloned repo.
Comment 40 Pramod Goyal CLA 2013-09-27 10:35:05 EDT
Created attachment 235894 [details]
Document update

Adding a patch for document update. Please review both this and previous patch and  let me know your comments.
Comment 41 Pramod Goyal CLA 2013-09-27 10:36:32 EDT
Thanks Keller/Phil. Submitted the updated document
Comment 42 Pramod Goyal CLA 2013-10-01 00:20:22 EDT
Sorry i am new to this ,can some one tell me what are the next steps to close this ticket.
Comment 43 Dani Megert CLA 2013-10-01 06:41:19 EDT
(In reply to Pramod Goyal from comment #42)
> Sorry i am new to this ,can some one tell me what are the next steps to
> close this ticket.

We will have to review the patches and comment on them, but currently we are very busy working on Java 8 support for Eclipse. Stay tuned!
Comment 44 Philippe Faes CLA 2014-01-09 03:56:12 EST
Any progress on this? 
I understand that everybody is busy, but this is just a small patch. Should be relatively easy to OK it.
Comment 45 Dani Megert CLA 2014-01-13 07:34:27 EST
(In reply to Pramod Goyal from comment #36)
> Created attachment 235875 [details] [diff]
> Fix as per Dani's comment

I see that you've added the following copyright info:

*     Pramod Goyal: pramod.goyal@gmail.com - this is a bug - https://bugs.eclipse.org/75981

How much is your code and how much is from Thomas, who contributed the initial patch? If it uses the code from Thomas, then he also needs to
- be added to the copyright header
- sign the CLA
- confirm in this bug that he wants to contribute that code by stating:
  "This contribution complies with http://www.eclipse.org/legal/CoO.php"

Also, please replace "this is a bug", with the bug summary from bugzilla.
Comment 46 Thomas Reinhardt CLA 2014-07-01 10:40:30 EDT
I managed to sign the CLA but I think the latest patch does not use my code. So this should be fine.
Comment 47 Endre Stølsvik CLA 2015-07-11 09:29:44 EDT
I was just looking for this, found this bug, and judging from the above comments, it seems like everything should be in order to just include the patch?
Comment 48 Ben Deeming CLA 2015-07-14 01:21:12 EDT
I haven't checked the source code recently, but the last few builds seem to have this functionality already... 
For example this is in my current C++ 'file' comment (C/C++ > Code Style > Code Templates > Comments > Files) and works just fine:
/// @date       Created on ${id:date('dd MMM YYYY')}
Comment 49 Eclipse Genie CLA 2015-10-14 15:58:26 EDT
New Gerrit change created: https://git.eclipse.org/r/58196
Comment 50 Jeremie Bresson CLA 2015-10-14 16:00:46 EDT
(In reply to Eclipse Genie from comment #49)
> New Gerrit change created: https://git.eclipse.org/r/58196

I have given this bug a try.

I have taken the existing patch (attachment 213533 [details]) with some modifications. I have added a Unit Test for the different cases. All authors are mentioned in the commit message.
I hope that putting it into a Gerrit patch will ease the code review process.

I will prepare a second change with the patch proposed in attachment 235894 [details] (for the documentation).
Comment 51 Jeremie Bresson CLA 2015-10-15 01:38:00 EDT
(In reply to Markus Keller from comment #18)
> The date variable should also take a locale string as optional second
> argument, i.e. the documentation should define it as
> 
>     ${id:date(format[,locale])}
> 
> The locale should be a java.util.Locale identifier, separated by '_', see
> Locale#toString().

I had a look at the Locale Javadoc.
 
I think that the locale should conform to the "IETF BCP 47" specification. It seems to be a standard: "Tags for Identifying Languages".

See: Locale#forLanguageTag(Sring)
http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#forLanguageTag%28java.lang.String%29

My understanding is that the main difference is that the separator is '-' and not '_' (example: 'fr-FR' and not 'fr_FR')
Comment 52 Eclipse Genie CLA 2015-10-15 15:22:15 EDT
New Gerrit change created: https://git.eclipse.org/r/58275
Comment 53 Jeremie Bresson CLA 2015-10-15 15:39:20 EDT
(In reply to Jeremie Bresson from comment #51) 
> See: Locale#forLanguageTag(Sring)

To use this method, Bug 478673 (Move platform text to Java 1.7 BREE) must be solved first.
Comment 54 Dani Megert CLA 2015-12-01 08:35:06 EST
(In reply to Jeremie Bresson from comment #53)
> (In reply to Jeremie Bresson from comment #51) 
> > See: Locale#forLanguageTag(Sring)
> 
> To use this method, Bug 478673 (Move platform text to Java 1.7 BREE) must be
> solved first.

Don't use that method. We should use what's known to Java developers and that's the usual Locale format, e.g. de_CH.
Comment 55 Jeremie Bresson CLA 2015-12-15 15:32:16 EST
As discussed here and on Gerrit the last version of this patch (Patch Set 5 on Gerrit) does not use the Java 7 Method:
> Locale#forLanguageTag(Sring)
As suggested, the replacement was to use:
> com.ibm.icu.util.ULocale

I remove the dependency to Bug 478673.
Comment 56 Dani Megert CLA 2016-01-21 09:34:41 EST
I've reviewed both changes. Only some minor issues need to get fixed before they can be merged.
Comment 59 Dani Megert CLA 2016-01-25 09:21:40 EST
I've added the following to the info for 'time' and 'year' variable:
Use the 'date' variable if you want to specify the format.

And added an entry to the 4.6 M5 N&N.
Comment 60 Lars Vogel CLA 2016-01-28 12:15:21 EST
Opened Bug 486748 for an improved description of the date variable.