Skip to content
Permalink
Browse files

Added i18n date methods. (format/hint/parse)

  • Loading branch information
jun66j5 committed Jul 2, 2010
1 parent bafb833 commit e5abd67dd35aa1aeb5631772ce78fd00d6442eb4
@@ -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
@@ -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'),
@@ -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))
@@ -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

@@ -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')
@@ -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))
@@ -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

@@ -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_
@@ -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
@@ -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
@@ -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
@@ -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')
@@ -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:
@@ -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': [],
}

@@ -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

@@ -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()
@@ -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
@@ -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'):

0 comments on commit e5abd67

Please sign in to comment.
You can’t perform that action at this time.