Skip to content

Commit

Permalink
#16, almost done.
Browse files Browse the repository at this point in the history
  • Loading branch information
pylover committed Oct 26, 2016
1 parent 08ad93b commit c05735e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
2 changes: 2 additions & 0 deletions khayyam/formatting/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@
PERSIAN_SECOND_REGEX = '([۰۱۲۳۴۵۶۷۸۹]|[۱۲۳۴۵][۰۱۲۳۴۵۶۷۸۹])'
PERSIAN_SECOND_ZERO_PADDED_REGEX = '(۰[۰۱۲۳۴۵۶۷۸۹]|[۱۲۳۴۵][۰۱۲۳۴۵۶۷۸۹])'

MILLISECOND_REGEX = '\d{1,3}' # FIXME: rename it to LATINE_

MICROSECOND_REGEX = '\d{1,6}' # FIXME: rename it to LATINE_
PERSIAN_MICROSECOND_REGEX = '[۰۱۲۳۴۵۶۷۸۹]{1,6}'

Expand Down
88 changes: 37 additions & 51 deletions khayyam/formatting/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,48 +486,6 @@ class JalaliDatetimeFormatter(JalaliDateFormatter):
),
]

"""
"""

# _post_parsers = [
# 'persianday',
# 'persiandayzeropadded',
# 'persiandayofyear',
# 'persiandayofyearzeropadded',
# 'persianmonth',
# 'persianmonthzeropadded',
# 'persianyear',
# 'persianyearzeropadded',
# 'persianshortyear',
# 'persianshortyearzeropadded',
# 'persianmicrosecond',
# 'persianhour12',
# 'persianhour12zeropadded',
# 'persianhour24',
# 'persianhour24zeropadded',
# 'persianminute',
# 'persianminutezeropadded',
# 'persiansecond',
# 'persiansecondzeropadded',
# 'persianutcoffset',
# 'localdateformat',
# 'localshortdatetimeformat',
# 'localshortdatetimeformatascii',
# 'localdatetimeformat',
# 'localdatetimeformatascii',
# 'localtimeformat',
# 'monthabbr',
# 'monthabbr_ascii',
# 'monthname',
# 'monthnameascii',
# 'ampm',
# 'ampmascii',
# 'shortyear',
# 'dayofyear',
# 'utcoffset'
# ]


class JalaliTimedeltaFormatter(JalaliDateFormatter):
"""
Expand Down Expand Up @@ -572,26 +530,54 @@ class JalaliTimedeltaFormatter(JalaliDateFormatter):

# Minutes
Directive(
'M',
'M', # Total minutes
consts.UNLIMITED_INT_REGEX,
type_=int,
formatter=lambda t: '%d' % t.total_minutes,
post_parser=lambda ctx, f: ctx.update(minutes=ctx['M'])
),
Directive(
'm',
'm', # Minutes zero padded
consts.MINUTE_REGEX,
name='minutes',
type_=int,
formatter=lambda t: '%.2d' % t.minutes
),

# Seconds
Directive(
'S', # Total seconds
consts.UNLIMITED_INT_REGEX,
type_=int,
formatter=lambda t: '%d' % t.total_seconds(),
post_parser=lambda ctx, f: ctx.update(seconds=ctx['S'])
),

Directive(
's', # Seconds
consts.SECOND_REGEX,
name='seconds',
type_=int,
formatter=lambda t: '%.2d' % t.seconds,
),

# Milliseconds
Directive(
't', # Milliseconds
consts.MILLISECOND_REGEX,
name='milliseconds',
type_=int,
formatter=lambda t: '%.3d' % (t.microseconds / 1000),
),

# Microseconds
Directive(
'f', # Microseconds
consts.MICROSECOND_REGEX,
name='microseconds',
type_=int,
formatter=lambda t: '%.6d' % t.microseconds,
),

]

# _post_parsers = [
#
# 'totalhours',
# 'persiantotalhours',
# 'totalminutes',
#
# ]
12 changes: 10 additions & 2 deletions khayyam/jalali_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def strptime(cls, date_string, fmt):

def _calculate_parts(self):
# days, seconds, microseconds
remaining_seconds = self.seconds
remaining_seconds = super(JalaliTimedelta, self).seconds
hours = remaining_seconds // 3600 # 1-23
remaining_seconds %= 3600
total_hours = self.days * 24 + hours + remaining_seconds / 3600
Expand Down Expand Up @@ -96,4 +96,12 @@ def minutes(self):

@property
def total_minutes(self):
return self.parts['total_minutes']
return self.parts['total_minutes']

@property
def seconds(self):
return self.parts['seconds']

@property
def milliseconds(self):
return int(self.microseconds / 1000)
19 changes: 18 additions & 1 deletion khayyam/tests/test_jalali_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@ def setUp(self):
pass

def test_strftime_strptime(self):
d1 = JalaliTimedelta(12, 23, 12, 3, 45, 34567)
d1 = JalaliTimedelta(
days=12,
seconds=23,
microseconds=12,
milliseconds=3,
minutes=45,
hours=34567
)
self.assertEqual(d1.total_hours, 34855.75638888889)
self.assertEqual(d1.strftime('%H'), '34855')
self.assertEqual(d1.strftime('%I'), '07')
self.assertEqual(d1.strftime('%k'), u'۳۴۸۵۵')
self.assertEqual(d1.strftime('%h'), u'۰۷')
self.assertEqual(d1.strftime('%M'), u'2091345')
self.assertEqual(d1.strftime('%m'), u'45')
self.assertEqual(d1.strftime('%S'), u'125480723')
self.assertEqual(d1.strftime('%s'), u'23')
self.assertEqual(d1.strftime('%f'), u'003012')
self.assertEqual(d1.strftime('%t'), u'003')

self.assertEqual(JalaliTimedelta.strptime('34855', '%H').total_seconds(), 125478000)
self.assertEqual(JalaliTimedelta.strptime(u'۳۴۸۵۵', '%k').total_seconds(), 125478000)
Expand All @@ -27,6 +40,10 @@ def test_strftime_strptime(self):
self.assertEqual(JalaliTimedelta.strptime('07', '%m').total_seconds(), 420)
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%M %m').total_minutes, 34343)
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%M %m').minutes, 23)
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%S %s').total_seconds(), 34343)
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%S %s').seconds, 23)
self.assertEqual(JalaliTimedelta.strptime('34343', '%f').microseconds, 34343)
self.assertEqual(JalaliTimedelta.strptime('100', '%t').milliseconds, 100)

############################################
# self.assertEqual(
Expand Down

0 comments on commit c05735e

Please sign in to comment.