summaryrefslogtreecommitdiff
path: root/django/utils/formats.py
blob: 8ad0afdd34677137b5476f678d7307a2897a62ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import decimal
import datetime

from django.conf import settings
from django.utils.translation import get_language
from django.utils.importlib import import_module
from django.utils import dateformat
from django.utils import numberformat 

def project_formats_module():
    """
    Returns the formats module for the current locale, defined
    on the project
    """
    if settings.FORMAT_MODULE_PATH:
        try:
            return import_module('.formats', '%s.%s' % (settings.FORMAT_MODULE_PATH, get_language()))
        except ImportError:
            pass
    return None

def django_formats_module():
    """
    Returns the formats module for the current locale, defined
    on Django
    """
    try:
        return import_module('.formats', 'django.conf.locale.%s' % get_language())
    except ImportError:
        return None

def getformat(format_type):
    """
    For a specific format type, returns the format for the
    current language (locale) defaulting to the format on settings.
    format_type is the name of the format, for example 'DATE_FORMAT'
    """
    if settings.USE_I18N and settings.USE_FORMAT_I18N:
        for module in (project_formats_module(), django_formats_module()):
            if module:
                try:
                    return getattr(module, format_type)
                except AttributeError:
                    pass
    return getattr(settings, format_type)

def date_format(value, format=None):
    """
    Formats a datetime.date or datetime.datetime object using a
    localizable format
    """
    return dateformat.format(value, getformat(format or 'DATE_FORMAT'))

def number_format(value, decimal_pos=None):
    """
    Formats a numeric value using localization settings
    """
    return numberformat.format(
        value,
        getformat('DECIMAL_SEPARATOR'),
        decimal_pos,
        getformat('NUMBER_GROUPING'),
        getformat('THOUSAND_SEPARATOR'),
    )

def localize(value, is_input=False):
    """
    Checks value, and if it has a localizable type (date,
    number...) it returns the value as a string using
    current locale format
    """
    if settings.USE_I18N and settings.USE_FORMAT_I18N:
        if isinstance(value, decimal.Decimal):
            return number_format(value)
        elif isinstance(value, float):
            return number_format(value)
        elif isinstance(value, int):
            return number_format(value)
        elif isinstance(value, datetime.datetime):
            if not is_input:
                return date_format(value, 'DATETIME_FORMAT')
            else:
                return value.strftime(getformat('DATETIME_INPUT_FORMATS')[0])
        elif isinstance(value, datetime.date):
            if not is_input:
                return date_format(value)
            else:
                return value.strftime(getformat('DATE_INPUT_FORMATS')[0])
        elif isinstance(value, datetime.time):
            if not is_input:
                return date_format(value, 'TIME_FORMAT')
            else:
                return value.strftime(getformat('TIME_INPUT_FORMATS')[0])
    return value