Skip to content

Commit

Permalink
Merge pull request arrow-py#247 from sipp11/master
Browse files Browse the repository at this point in the history
Add Thai locale
  • Loading branch information
andrewelkins committed Aug 7, 2015
2 parents 8869524 + fbc12bf commit 75abd99
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions arrow/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def format(cls, dt, fmt):
def _format_token(self, dt, token):

if token == 'YYYY':
return '{0:04d}'.format(dt.year)
return self.locale.year_full(dt.year)
if token == 'YY':
return '{0:04d}'.format(dt.year)[2:]
return self.locale.year_abbreviation(dt.year)

if token == 'MMMM':
return self.locale.month_name(dt.month)
Expand Down
76 changes: 76 additions & 0 deletions arrow/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def month_number(self, name):

return self._month_name_to_ordinal.get(name)

def year_full(self, year):
''' Returns the year for specific locale if available
:param name: the ``int`` year (4-digit)
'''
return '{0:04d}'.format(year)

def year_abbreviation(self, year):
''' Returns the year for specific locale if available
:param name: the ``int`` year (4-digit)
'''
return '{0:04d}'.format(year)[2:]

def meridian(self, hour, token):
''' Returns the meridian indicator for a specified hour and format token.
Expand Down Expand Up @@ -1530,4 +1544,66 @@ def _format_timeframe(self, timeframe, delta):
return form.format(abs(delta))


class ThaiLocale(Locale):

names = ['th', 'th_th']

past = '{0}{1}ที่ผ่านมา'
future = 'ในอีก{1}{0}'

timeframes = {
'now': 'ขณะนี้',
'seconds': 'ไม่กี่วินาที',
'minute': '1 นาที',
'minutes': '{0} นาที',
'hour': '1 ชั่วโมง',
'hours': '{0} ชั่วโมง',
'day': '1 วัน',
'days': '{0} วัน',
'month': '1 เดือน',
'months': '{0} เดือน',
'year': '1 ปี',
'years': '{0} ปี',
}

month_names = ['', 'มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน',
'พฤษภาคม', 'มิถุนายน', 'กรกฏาคม', 'สิงหาคม',
'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม']
month_abbreviations = ['', 'ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.',
'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.',
'พ.ย.', 'ธ.ค.']

day_names = ['', 'จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์',
'เสาร์', 'อาทิตย์']
day_abbreviations = ['', 'จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส', 'อา']

meridians = {
'am': 'am',
'pm': 'pm',
'AM': 'AM',
'PM': 'PM',
}

BE_OFFSET = 543

def year_full(self, year):
'''Thai always use Buddhist Era (BE) which is CE + 543'''
year += self.BE_OFFSET
return '{0:04d}'.format(year)

def year_abbreviation(self, year):
'''Thai always use Buddhist Era (BE) which is CE + 543'''
year += self.BE_OFFSET
return '{0:04d}'.format(year)[2:]

def _format_relative(self, humanized, timeframe, delta):
'''Thai normally doesn't have any space between words'''
if timeframe == 'now':
return humanized
space = '' if timeframe == 'seconds' else ' '
direction = self.past if delta < 0 else self.future

return direction.format(humanized, space)


_locales = _map_locales()

0 comments on commit 75abd99

Please sign in to comment.