How Do I use the formats in DateTime::Locale with a DateTime object?

simbabque

DateTime::Locale has a very comprehensive list of date and time formats for various locales and countries. I would like to use it in emails to customers, depending on which country the customer is from.

Unfortunately, it is a little hard to understand from the documentation how to actually use the functions for a medium or a long date. For example, DateTime::Locale::de_DE lists these date formats (excerpt) in the doc:

Long
 2008-02-05T18:30:30 = 5. Februar 2008
 1995-12-22T09:05:02 = 22. Dezember 1995
-0010-09-15T04:44:23 = 15. September -10

Medium
 2008-02-05T18:30:30 = 05.02.2008
 1995-12-22T09:05:02 = 22.12.1995
 -0010-09-15T04:44:23 = 15.09.-010

This is great. According to DateTime::Locale::Base there are methods in the locale object to get these formats: $locale->date_format_long() and $locale->date_format_medium().

After some googling I came up with Sinan Ünür's blog, where he shows this code (excerpt):

for my $locale ( qw(ar da de en_GB es fr ru tr) ) {
    $dt->set_locale( $locale );
    print_date( $dt );
}

sub print_date {
    my ($dt) = @_;
    my $locale = $dt->locale;

    printf(
        "In %s: %s\n", $locale->name,
        $dt->format_cldr($locale->date_format_full)
    );
}

So the format that comes out of these methods is a cldr format. Cool. But what Sinan shows looks tedius. In short, it would be:

for (qw( ar da de en_GB es fr ru tr )) {
  my $dt2 = DateTime->now( locale => $_ );
  printf "%s: %s\n", $_, $dt2->format_cldr($dt2->locale->date_format_long);
}

In order to make that shorter, I could of course do something like this:

package DateTime;

sub stringify_long {
  return $_[0]->format_cldr($_[0]->locale->date_format_long);
}

package Main;
use strict; use warnings;    
use DateTime;

my $dt = DateTime->now( locale => 'de_DE' );
print $dt->stringify_long;

But I don't want to do that. So my question: Is there a way to stringify a DateTime object according to one of these formats from its locale with a build-in method that I am missing?

ThisSuitIsBlackNot

I'm not sure what your opposition is to Sinan Ünür's method, so I don't know if this will appeal to you or not, but you can specify a formatter object to control stringification of DateTime objects:

use DateTime;
use DateTime::Format::CLDR;
use DateTime::Locale;

my $locale = DateTime::Locale->load('de_DE');
my $formatter = DateTime::Format::CLDR->new(
    pattern => $locale->date_format_long,
    locale => $locale
);
my $dt = DateTime->now( locale => $locale, formatter => $formatter );
print $dt;

or

use DateTime;
use DateTime::Format::CLDR;
use DateTime::Locale;

my $locale = DateTime::Locale->load('de_DE');
my $dt = DateTime->now( locale => $locale );
my $formatter = DateTime::Format::CLDR->new(
    pattern => $locale->date_format_long,
    locale => $locale
);
$dt->set_formatter($formatter);
print $dt;

The nice thing about this approach is that once you've set a formatter, printing the date is easy-peasy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I add milliseconds to a DateTime object?

From Dev

How do I test if an object is a pandas datetime index?

From Dev

How do I construct a UTC `datetime` object in Python?

From Dev

How do I create a DateTime object given UTC time and date?

From Dev

How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe?

From Dev

How do I get the recent json object based on datetime value?

From Dev

How do I convert an object containing an Oracle Date to a DateTime?

From Dev

Given a DateTime object, how do I get DateTimeZone in Joda Time?

From Dev

How do I use LinqToSql to query on specific datetime

From Dev

How do I use LINQ to compare a date and a datetime?

From Java

How do I translate an ISO 8601 datetime string into a Python datetime object?

From Dev

How to use DateTime Object split Interval Object

From Dev

Python - given a timezone-aware datetime object how do I get a timezone-naive UTC datetime object?

From Dev

Why in PowerShell does embedding a DateTime object in a string lose the DateTime locale?

From Dev

How do I convert from an IronPython datetime to a .NET DateTime?

From Dev

How do I cast a Bloomberg DateTime to System.DateTime

From Dev

How do I convert a chrono `DateTime<UTC>` instance to `DateTime<Local>`?

From Dev

How do I get the closest DateTime from a List<DateTime>

From Dev

How do I cast a Bloomberg DateTime to System.DateTime

From Java

How do I convert a datetime to date?

From Dev

How do I pass DateTime as a parameter in PowerShell?

From Dev

How do I store timestamp or datetime in Orion?

From Dev

How do I format a column but keep it as datetime?

From Dev

How do I store timestamp or datetime in Orion?

From Dev

How do I convert a datetime into a epoch?

From Dev

How do i convert this date string to datetime?

From Dev

DateTime locale string not change even I changed the locale in app in android

From Dev

How do I vectorize a boolean operation on a numpy array of datetimes where the boolean operation compares datetime object attributes?

From Dev

How do I modify my `@SQLUpdate` to convert a `Joda` `DateTime` object into an `h2`-readable timestamp?

Related Related

  1. 1

    How do I add milliseconds to a DateTime object?

  2. 2

    How do I test if an object is a pandas datetime index?

  3. 3

    How do I construct a UTC `datetime` object in Python?

  4. 4

    How do I create a DateTime object given UTC time and date?

  5. 5

    How do I properly set the Datetimeindex for a Pandas datetime object in a dataframe?

  6. 6

    How do I get the recent json object based on datetime value?

  7. 7

    How do I convert an object containing an Oracle Date to a DateTime?

  8. 8

    Given a DateTime object, how do I get DateTimeZone in Joda Time?

  9. 9

    How do I use LinqToSql to query on specific datetime

  10. 10

    How do I use LINQ to compare a date and a datetime?

  11. 11

    How do I translate an ISO 8601 datetime string into a Python datetime object?

  12. 12

    How to use DateTime Object split Interval Object

  13. 13

    Python - given a timezone-aware datetime object how do I get a timezone-naive UTC datetime object?

  14. 14

    Why in PowerShell does embedding a DateTime object in a string lose the DateTime locale?

  15. 15

    How do I convert from an IronPython datetime to a .NET DateTime?

  16. 16

    How do I cast a Bloomberg DateTime to System.DateTime

  17. 17

    How do I convert a chrono `DateTime<UTC>` instance to `DateTime<Local>`?

  18. 18

    How do I get the closest DateTime from a List<DateTime>

  19. 19

    How do I cast a Bloomberg DateTime to System.DateTime

  20. 20

    How do I convert a datetime to date?

  21. 21

    How do I pass DateTime as a parameter in PowerShell?

  22. 22

    How do I store timestamp or datetime in Orion?

  23. 23

    How do I format a column but keep it as datetime?

  24. 24

    How do I store timestamp or datetime in Orion?

  25. 25

    How do I convert a datetime into a epoch?

  26. 26

    How do i convert this date string to datetime?

  27. 27

    DateTime locale string not change even I changed the locale in app in android

  28. 28

    How do I vectorize a boolean operation on a numpy array of datetimes where the boolean operation compares datetime object attributes?

  29. 29

    How do I modify my `@SQLUpdate` to convert a `Joda` `DateTime` object into an `h2`-readable timestamp?

HotTag

Archive