Skip to content

Commit

Permalink
Added i18n date methods. (format/hint/parse)
Browse files Browse the repository at this point in the history
  • Loading branch information
jun66j5 committed Jul 2, 2010
1 parent bafb833 commit e5abd67
Show file tree
Hide file tree
Showing 9 changed files with 631 additions and 38 deletions.
33 changes: 20 additions & 13 deletions trac/ticket/admin.py
Expand Up @@ -19,9 +19,9 @@
from trac.resource import ResourceNotFound
from trac.ticket import model
from trac.util import getuser
from trac.util.datefmt import utc, parse_date, get_date_format_hint, \
get_datetime_format_hint, format_date, \
format_datetime
from trac.util.datefmt import utc, parse_date, format_date, format_datetime, \
i18n_get_datetime_format_hint, \
i18n_get_date_format_hint, i18n_parse_date
from trac.util.text import print_table, printout, exception_to_unicode
from trac.util.translation import _, N_, gettext
from trac.web.chrome import Chrome, add_notice, add_warning
Expand Down Expand Up @@ -262,10 +262,13 @@ def _render_admin_panel(self, req, cat, page, milestone):
mil.due = mil.completed = None
due = req.args.get('duedate', '')
if due:
mil.due = parse_date(due, req.tz)
mil.due = i18n_parse_date(due, tzinfo=req.tz,
locale=req.locale)
if req.args.get('completed', False):
completed = req.args.get('completeddate', '')
mil.completed = parse_date(completed, req.tz)
mil.completed = i18n_parse_date(completed,
tzinfo=req.tz,
locale=req.locale)
if mil.completed > datetime.now(utc):
raise TracError(_('Completion date may not be in '
'the future'),
Expand Down Expand Up @@ -293,8 +296,9 @@ def _render_admin_panel(self, req, cat, page, milestone):
mil = model.Milestone(self.env)
mil.name = name
if req.args.get('duedate'):
mil.due = parse_date(req.args.get('duedate'),
req.tz)
mil.due = i18n_parse_date(req.args.get('duedate'),
tzinfo=req.tz,
locale=req.locale)
mil.insert()
add_notice(req, _('The milestone "%(name)s" has been '
'added.', name=name))
Expand Down Expand Up @@ -345,8 +349,8 @@ def do_remove(db):
'default': default}

data.update({
'date_hint': get_date_format_hint(),
'datetime_hint': get_datetime_format_hint()
'date_hint': i18n_get_date_format_hint(locale=req.locale),
'datetime_hint': i18n_get_datetime_format_hint(locale=req.locale),
})
return 'admin_milestones.html', data

Expand Down Expand Up @@ -449,7 +453,9 @@ def _render_admin_panel(self, req, cat, page, version):
if req.args.get('save'):
ver.name = req.args.get('name')
if req.args.get('time'):
ver.time = parse_date(req.args.get('time'), req.tz)
ver.time = i18n_parse_date(req.args.get('time'),
tzinfo=req.tz,
locale=req.locale)
else:
ver.time = None # unset
ver.description = req.args.get('description')
Expand All @@ -474,8 +480,9 @@ def _render_admin_panel(self, req, cat, page, version):
ver = model.Version(self.env)
ver.name = name
if req.args.get('time'):
ver.time = parse_date(req.args.get('time'),
req.tz)
ver.time = i18n_parse_date(req.args.get('time'),
tzinfo=req.tz,
locale=req.locale)
ver.insert()
add_notice(req, _('The version "%(name)s" has been '
'added.', name=name))
Expand Down Expand Up @@ -516,7 +523,7 @@ def do_remove(db):
'default': default}

data.update({
'datetime_hint': get_datetime_format_hint()
'datetime_hint': i18n_get_datetime_format_hint(locale=req.locale),
})
return 'admin_versions.html', data

Expand Down
10 changes: 8 additions & 2 deletions trac/ticket/query.py
Expand Up @@ -32,7 +32,7 @@
from trac.ticket.api import TicketSystem
from trac.util import Ranges, as_bool
from trac.util.datefmt import format_datetime, from_utimestamp, parse_date, \
to_timestamp, to_utimestamp, utc
to_timestamp, to_utimestamp, utc, i18n_parse_date
from trac.util.presentation import Paginator
from trac.util.text import empty, shorten_line, unicode_unquote
from trac.util.translation import _, tag_
Expand Down Expand Up @@ -422,6 +422,11 @@ def get_sql(self, req=None, cached_ids=None):
"""Return a (sql, params) tuple for the query."""
self.get_columns()
db = self.env.get_db_cnx()
tz = None
locale = None
if req:
tz = req.tz
locale = req.locale

enum_columns = ('resolution', 'priority', 'severity')
# Build the list of actual columns to query
Expand Down Expand Up @@ -469,7 +474,8 @@ def add_cols(*args):
def get_timestamp(date):
if date:
try:
return to_utimestamp(parse_date(date, req.tz))
return to_utimestamp(i18n_parse_date(date, tzinfo=tz,
locale=locale))
except TracError, e:
errors.append(unicode(e))
return None
Expand Down
21 changes: 15 additions & 6 deletions trac/ticket/roadmap.py
Expand Up @@ -30,8 +30,9 @@
from trac.resource import *
from trac.search import ISearchSource, search_to_sql, shorten_result
from trac.util.datefmt import parse_date, utc, to_utimestamp, \
get_date_format_hint, get_datetime_format_hint, \
format_date, format_datetime, from_utimestamp
format_date, format_datetime, from_utimestamp, \
i18n_get_datetime_format_hint, \
i18n_get_date_format_hint, i18n_parse_date
from trac.util.text import CRLF
from trac.util.translation import _, tag_
from trac.ticket import Milestone, Ticket, TicketSystem, group_milestones
Expand Down Expand Up @@ -626,7 +627,11 @@ def _do_save(self, req, db, milestone):
milestone.description = req.args.get('description', '')

due = req.args.get('duedate', '')
milestone.due = due and parse_date(due, tzinfo=req.tz) or None
if due:
milestone.due = i18n_parse_date(due, tzinfo=req.tz,
locale=req.locale)
else:
milestone.due = None

completed = req.args.get('completeddate', '')
retarget_to = req.args.get('target')
Expand Down Expand Up @@ -658,7 +663,11 @@ def warn(msg):

# -- check completed date
if 'completed' in req.args:
completed = completed and parse_date(completed, req.tz) or None
if completed:
completed = i18n_parse_date(completed, tzinfo=req.tz,
locale=req.locale)
else:
completed = None
if completed and completed > datetime.now(utc):
warn(_('Completion date may not be in the future'))
else:
Expand Down Expand Up @@ -703,8 +712,8 @@ def _render_confirm(self, req, db, milestone):
def _render_editor(self, req, db, milestone):
data = {
'milestone': milestone,
'date_hint': get_date_format_hint(),
'datetime_hint': get_datetime_format_hint(),
'date_hint': i18n_get_date_format_hint(locale=req.locale),
'datetime_hint': i18n_get_datetime_format_hint(locale=req.locale),
'milestone_groups': [],
}

Expand Down
8 changes: 7 additions & 1 deletion trac/ticket/tests/query.py
Expand Up @@ -5,6 +5,11 @@
from trac.web.href import Href
from trac.wiki.formatter import LinkFormatter

try:
from babel import Locale
except ImportError:
Locale = None

import unittest
import difflib

Expand Down Expand Up @@ -33,7 +38,8 @@ def assertEqualSQL(self, sql, correct_sql):
def setUp(self):
self.env = EnvironmentStub(default_data=True)
self.db = self.env.get_db_cnx()
self.req = Mock(href=self.env.href, authname='anonymous', tz=utc)
self.req = Mock(href=self.env.href, authname='anonymous', tz=utc,
locale=Locale and Locale.parse('en_US') or None)

def tearDown(self):
self.env.reset_db()
Expand Down
5 changes: 3 additions & 2 deletions trac/timeline/web_ui.py
Expand Up @@ -30,7 +30,7 @@
from trac.timeline.api import ITimelineEventProvider
from trac.util import as_int
from trac.util.datefmt import format_date, format_datetime, parse_date, \
to_utimestamp, utc, pretty_timedelta
to_utimestamp, utc, pretty_timedelta, i18n_parse_date
from trac.util.text import exception_to_unicode, to_unicode
from trac.util.translation import _, tag_
from trac.web import IRequestHandler, IRequestFilter
Expand Down Expand Up @@ -99,7 +99,8 @@ def process_request(self, req):
# Acquire from date only from non-blank input
reqfromdate = req.args['from'].strip()
if reqfromdate:
precisedate = parse_date(reqfromdate, req.tz)
precisedate = i18n_parse_date(reqfromdate, tzinfo=req.tz,
locale=req.locale)
fromdate = precisedate
precision = req.args.get('precision', '')
if precision.startswith('second'):
Expand Down

0 comments on commit e5abd67

Please sign in to comment.