Re: Question about printing month/day

[email protected] (Brian Hirt)
Newsgroups perl.datetime
Message-ID <[email protected]>
Dave,

Thanks for your excellent response.

It looks like the MMMMd entry is the one i'm interested in.   I was  
able to change the generate_from_cldr tool to extract this information  
and incorporate it into the locale modules.  Out of the 134 modules  
derived from DateTime::Locale::root.pm, 71 of them had MMMMd  
information.   The other locales will just inherit root which seems  
acceptable to me.  As the cldr database gets more complete, these may  
fill out.

I really don't know what to name the format string so i stuck with the  
current convention which is to derive the names from the xml  
entities.  That would make the format string MMMMd_datetime_format.   
It seems like an awkward name, but is easy enough to change.

The patch is pretty straight forward and included below.

If this is something that you are interested in incorporating into  
Date::Time::Locale, I would obviously love that.   If not, you have  
still provided me with a pretty easy way to do what I want without a  
lot of manual work.   If there is anything I can do to help (like  
update the documentation), please let me know.

Best Regards,

Brian Hirt

--- DateTime-Locale-0.35/tools/generate_from_cldr	2007-09-09  
13:13:56.000000000 -0600
+++ DateTime-Locale-patch/tools/generate_from_cldr	2008-05-12  
17:14:12.000000000 -0600
@@ -69,6 +69,7 @@
      );

  my @FormatLengths = qw( full long medium short  );
+my @dateTimeFormatIds = qw( MMMMd );

  my %opts;

@@ -373,6 +374,29 @@
  {
      my ( $hash, $cal ) = @_;

+    if ( my $dateTimes = $cal->{dateTimeFormats}{availableFormats} 
{dateFormatItem} )
+    {
+	# if there is only one availableFormats, a hash will be returned  
instead of an
+	# array, create array so it get's processed below
+	if ( ref $dateTimes eq 'HASH' )
+	{
+	    $dateTimes = [ $dateTimes ];
+	}
+
+	foreach my $dateFormatItem (@$dateTimes)
+	{
+	    foreach my $formatId (@dateTimeFormatIds)
+	    {
+		next unless $dateFormatItem->{id} eq $formatId;
+
+		$hash->{datetime_formats}{$formatId} = simple2strf($dateFormatItem- 
 >{content});
+
+		print "    datetime_formats{$formatId}: $hash->{datetime_formats} 
{$formatId}\n"
+		    if $opts{verbose};
+	    }
+	}
+    }
+
      if ( my $dates = $cal->{dateFormats} )
      {
          my %original;
@@ -690,6 +714,21 @@
          $subs .= sprintf "sub %-30s { \\\@%s }\n", $var, $var;
      }

+    foreach my $var ( qw( datetime_formats) )
+    {
+        next unless $hash->{$var};
+
+        for my $formatId (@dateTimeFormatIds)
+        {
+            next unless $hash->{$var}{$formatId};
+
+            my $sub_name = join '_', $formatId, $var;
+            $sub_name =~ s/s$//;
+
+            $subs .=
+                sprintf "sub %-30s { %s }\n", $sub_name, qq|"\Q$hash- 
 >{$var}{$formatId}\E"|;
+        }
+    }
      foreach my $var ( qw( date_formats
                            time_formats
                          ) )

On May 12, 2008, at 3:40 PM, Dave Rolsky wrote:

> On Mon, 12 May 2008, Brian Hirt wrote:
>
>> it includes the year (which i do not want).    Does anyone have any  
>> advice on how to do this other than manually defining and  
>> maintaining 200 different format strings outside of DateTime?    If  
>> it is not possible to print a "day month" string, is this something  
>> that might be added to the locale objects? It seems like this would  
>> be a common thing that's wanted.
>
> This is definitely something that should be part of the DT::Locale  
> api (if it exists).
>
> It looks like this data does exist in the CLDR XML data, which is  
> key, because the DT::Locale modules are just generated from this data.
>
> If you look at the CLDR data, there's a bit of XML like this:
>
>  <dateTimeFormats>
>          <dateTimeFormatLength>
>                  <dateTimeFormat>
>                          <pattern>{1} {0}</pattern>
>                  </dateTimeFormat>
>          </dateTimeFormatLength>
>          <availableFormats>
>                  <dateFormatItem id="HHmm">HH:mm</dateFormatItem>
>                  <dateFormatItem id="HHmmss">HH:mm:ss</dateFormatItem>
>                  <dateFormatItem id="MMMMd">MMMM d</dateFormatItem>
>                  <dateFormatItem id="MMMMdd" draft="unconfirmed">dd  
> MMMM</dateFormatItem>
>                  <dateFormatItem id="MMMd" draft="unconfirmed">d- 
> MMM</dateFormatItem>
>                  <dateFormatItem id="MMMdd" draft="unconfirmed">dd  
> MMM</dateFormatItem>
>                  <dateFormatItem id="MMd" draft="unconfirmed">d/MM</ 
> dateFormatItem>
>                  <dateFormatItem id="Md">M/d</dateFormatItem>
>                  <dateFormatItem id="hhmm" draft="unconfirmed">hh:mm  
> a</dateFormatItem>
>                  <dateFormatItem id="hhmmss"  
> draft="unconfirmed">hh:mm:ss a</dateFormatItem>
>                  <dateFormatItem id="mmss">mm:ss</dateFormatItem>
>                  <dateFormatItem id="yyMM">MM/yy</dateFormatItem>
>                  <dateFormatItem id="yyQ">Q yy</dateFormatItem>
>                  <dateFormatItem id="yyQQQQ">QQQQ yy</dateFormatItem>
>                  <dateFormatItem id="yyyyM" draft="unconfirmed">M/ 
> yyyy</dateFormatItem>
>                  <dateFormatItem id="yyyyMMM">MMM yyyy</ 
> dateFormatItem>
>          </availableFormats>
>  </dateTimeFormats>
>
> I'm pretty sure this is the type of thing you're looking for.
>
> This is definitely a case of patches welcome.
>
> It probably wouldn't be _too_ hard to add support for this.
>
> The starting point is the tools/generate_from_cldr script, which is  
> what generates the various locatel .pm files. It's not the most  
> beautiful code though ;)
>
> If you make the generator include this info I would certainly be  
> willing to add an appropriate API for retrieving it, probably  
> something like:
>
>  $locale->format_for('MMMMd')
>
> since there's so many formats making a mtehod for each seems like a  
> mess.
>
>
> -dave
>
> /*==========================
> VegGuide.Org
> Your guide to all that's veg
> ==========================*/
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.