[PATCH 1/2] A function for resolving date format and order

Brian C. Lane bcl at redhat.com
Tue Oct 7 20:40:18 UTC 2014


On Tue, Oct 07, 2014 at 06:32:36PM +0200, Vratislav Podzimek wrote:
> It's not that easy to get right order of date fields (year, month, day) and
> their formats so we should better have it in a standalone function with tests.
> 
> Related: rhbz#1044233
> Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
> ---
>  pyanaconda/localization.py                  | 64 +++++++++++++++++++++++++++++
>  tests/pyanaconda_tests/localization_test.py | 21 ++++++++++
>  2 files changed, 85 insertions(+)
> 
> diff --git a/pyanaconda/localization.py b/pyanaconda/localization.py
> index c95fa1d..efcb66f 100644
> --- a/pyanaconda/localization.py
> +++ b/pyanaconda/localization.py
> @@ -26,6 +26,7 @@ import re
>  import langtable
>  import locale as locale_mod
>  import glob
> +from collections import namedtuple
>  
>  from pyanaconda import constants
>  from pyanaconda.iutil import upcase_first_letter
> @@ -469,3 +470,66 @@ def load_firmware_language(lang):
>  
>      log.debug("Using UEFI PlatformLang '%s' ('%s') as our language.", d, locales[0])
>      setup_locale(locales[0], lang)
> +
> +_DateFieldSpec = namedtuple("DateFieldSpec", ["format", "suffix"])
> +
> +def resolve_date_format(year, month, day):
> +    """
> +    Puts the year, month and day objects in the right order according to the
> +    currently set locale and provides format specification for each of the
> +    fields.
> +
> +    :param year: any object or value representing year
> +    :type year: any
> +    :param month: any object or value representing month
> +    :type month: any
> +    :param day: any object or value representing day
> +    :type day: any
> +    :returns: a pair where the first field contains a tuple with the year, month
> +              and day objects/values put in the right order and where the second
> +              field contains a tuple with three :class:`_DateFieldSpec` objects
> +              specifying formats respectively to the first (year, month, day)
> +              field
> +    :rtype: tuple
> +    :raise ValueError: in case currently set locale has unsupported date format
> +                       (likely a bug in this function)
> +
> +    """
> +
> +    # see date (1), 'O' (not '0') is a mystery, 'E' is Buddhist calendar, '(.*)'
> +    # is an arbitrary suffix
> +    field_spec_re = re.compile(r'([-_0OE^#]*)([yYmbBde])(.*)')
> +
> +    order = []
> +    formats = []
> +    fmt_str = locale_mod.nl_langinfo(locale_mod.D_FMT)

Is it possible for this to return None or something without %? If so
then we should catch that and default to something 'normal'.

Other than that these look good to me.

-- 
Brian C. Lane | Anaconda Team | IRC: bcl #anaconda | Port Orchard, WA (PST8PDT)


More information about the anaconda-patches mailing list