grails-core icon indicating copy to clipboard operation
grails-core copied to clipboard

Change g:format to accept date, datetime and time

Open jamesfredley opened this issue 2 months ago • 11 comments

Issue description

Change g:format to take date, datetime and time, instead of single date format

jamesfredley avatar Oct 22 '25 15:10 jamesfredley

@jamesfredley it already does that but the default behavior is to render:

https://github.com/apache/grails-core/blob/c27742f9ad64b361ad39dabe9e090e938dc256ca/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormatTagLib.groovy#L187

otherwise there is a type parameter for date, time, and anything else

https://github.com/apache/grails-core/blob/c27742f9ad64b361ad39dabe9e090e938dc256ca/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormatTagLib.groovy#L193-L201

codeconsole avatar Oct 22 '25 22:10 codeconsole

https://github.com/apache/grails-core/pull/15146

https://github.com/apache/grails-core/blob/c27742f9ad64b361ad39dabe9e090e938dc256ca/grails-fields/grails-app/taglib/grails/plugin/formfields/FormFieldsTagLib.groovy#L949-L973

https://github.com/apache/grails-core/pull/15118 - introduces second precision on date picker and time-stamping

Date picker should also be updated to support time only as well as optional millisecond and nanosecond precision for lossless date updating.

codeconsole avatar Oct 22 '25 22:10 codeconsole

@codeconsole @jdaugherty I created this issue during the discussion in the weekly meting today, but it's sounds like we might already have what we need.

jamesfredley avatar Oct 22 '25 22:10 jamesfredley

@jamesfredley yeah, I know, I am just adding context because we need to discuss it more. I still don't think we have a final solution and I am looking for everyones input. I want this issue to start the discussion.

This PR shows what everything looks like using DATETIME https://github.com/apache/grails-core/pull/15165

codeconsole avatar Oct 22 '25 22:10 codeconsole

grails:
 databinding:
   dateFormats: # used when parsing posted form fields
     - "yyyy-MM-dd'T'HH:mm:ss.SSSX"
     - "yyyy-MM-dd'T'hh:mm:ss'Z'"
     - "yyyy-MM-dd HH:mm:ss.S z"
     - "yyyy-MM-dd HH:mm:ss.S"
     - "yyyy-MM-dd'T'HH:mm:ssZ"
     - "yyyy-MM-dd'T'HH:mm:ss"
     - "yyyy-MM-dd HH:mm:ss"
     - "yyyy-MM-dd HH:mm"
     - "yyyy-MM-dd"
     - "HH:mm:ssZ"
     - "HH:mm:ss"

and the following from messages.properties

default.date.format=EEE, MMM d, yyyy h:mm:ss a, z
default.date.day.format=EEE, MMM d
default.date.time.format=h:mm a

@jamesfredley can you please describe what you were referring to here https://github.com/apache/grails-core/pull/15146#issuecomment-3433028816 Are these existing configuration values or a proposal?

codeconsole avatar Oct 23 '25 16:10 codeconsole

I thought we wanted to create this ticket to only have 3 different default date/datetime/time formats. @codeconsole are you saying we already have defaults for these that can be overridden?

jdaugherty avatar Oct 23 '25 18:10 jdaugherty

@codeconsole I only wanted to make sure we continued to support overriding the default format(s) using the same historical config locations.

default.date.format varies by local in apps generated by shell and forge and I do not have an opinion on changing the default.

  • https://github.com/apache/grails-core/tree/7.0.x/grails-forge/grails-forge-core/src/main/resources/i18n
  • https://github.com/apache/grails-core/tree/7.0.x/grails-profiles/base/skeleton/grails-app/i18n

I had not had time to look at #15146 in depth, so the databinding config was not relevant.

In general I have found the default list for databinding dateFormats to be missing a few common formats, but I will keep overriding it and would not want to risk breaking other apps to change the default list.

https://github.com/apache/grails-core/blob/c27742f9ad64b361ad39dabe9e090e938dc256ca/grails-databinding/src/main/groovy/org/grails/plugins/databinding/AbstractDataBindingGrailsPlugin.groovy#L50-L56

jamesfredley avatar Oct 23 '25 18:10 jamesfredley

@jdaugherty I am saying g:dateFormat already supports 3 format types. I haven't dug deep enough to see if they are overridable. At first glance it looked to me like if you don't specify a type, you only get 1 default format which is everything.

I haven't traced this code yet, but it would make sense to be that the fields plugin should use type DATETIME for displaying dates. In 7.1.x, if they are not overridable, we can make them.

https://github.com/apache/grails-core/blob/c27742f9ad64b361ad39dabe9e090e938dc256ca/grails-gsp/plugin/src/main/groovy/org/grails/plugins/web/taglib/FormatTagLib.groovy#L193-L201

codeconsole avatar Oct 23 '25 22:10 codeconsole

@jamesfredley where did you get this yml code from?

grails:
 databinding:
   dateFormats: # used when parsing posted form fields
     - "yyyy-MM-dd'T'HH:mm:ss.SSSX"
     - "yyyy-MM-dd'T'hh:mm:ss'Z'"
     - "yyyy-MM-dd HH:mm:ss.S z"
     - "yyyy-MM-dd HH:mm:ss.S"
     - "yyyy-MM-dd'T'HH:mm:ssZ"
     - "yyyy-MM-dd'T'HH:mm:ss"
     - "yyyy-MM-dd HH:mm:ss"
     - "yyyy-MM-dd HH:mm"
     - "yyyy-MM-dd"
     - "HH:mm:ssZ"
     - "HH:mm:ss"

codeconsole avatar Oct 23 '25 22:10 codeconsole

That yml was copied from one of my apps.

https://grails.apache.org/docs/snapshot/guide/theWebLayer.html#_date_formats_for_data_binding pointed me in that direction years and years ago. It needs to be updated with the full list and a yml example.

jamesfredley avatar Oct 23 '25 22:10 jamesfredley

That yml was copied from one of my apps.

https://grails.apache.org/docs/snapshot/guide/theWebLayer.html#_date_formats_for_data_binding pointed me in that direction years and years ago. It needs to be updated with the full list and a yml example.

@jamesfredley ahh, yeah, those are for parsing, not rendering.

codeconsole avatar Oct 24 '25 02:10 codeconsole