Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
308 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,8 +43,8 @@ nosetests.xml | |
|
||
sphinx/_build | ||
sphinx/_inv-cache | ||
sphinx/_site | ||
docs/doctest | ||
docs/doctrees | ||
docs/html | ||
htmlcov | ||
_site |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
from .constants import * | ||
from .formatters import BaseFormatter, JalaliDateFormatter, JalaliDatetimeFormatter | ||
__author__ = 'vahid' | ||
from .formatters import BaseFormatter, JalaliDateFormatter, JalaliDatetimeFormatter, JalaliTimedeltaFormatter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
from .base import Directive | ||
from .persian import PersianNumberDirective | ||
from khayyam.formatting import constants as consts | ||
|
||
|
||
class TotalHoursDirective(Directive): | ||
""" | ||
Representing total hours in a timedelta. | ||
""" | ||
|
||
def __init__(self, key, name): | ||
super(TotalHoursDirective, self).__init__(key, name, consts.UNLIMITED_INT_REGEX, int) | ||
|
||
def format(self, d): | ||
return str(int(d.total_hours)) | ||
|
||
def post_parser(self, ctx, formatter): | ||
super(TotalHoursDirective, self).post_parser(ctx, formatter) | ||
if self.name in ctx and ctx[self.name]: | ||
ctx['hours'] = ctx[self.name] | ||
|
||
|
||
class PersianTotalHoursDirective(PersianNumberDirective): | ||
""" | ||
Representing total hours in persian form. | ||
""" | ||
|
||
def __init__(self, key, name): | ||
super(PersianTotalHoursDirective, self).__init__(key, name, consts.PERSIAN_UNLIMITED_INT_REGEX, int) | ||
|
||
def format(self, d): | ||
return super(PersianTotalHoursDirective, self).format(int(d.total_hours)) | ||
|
||
def post_parser(self, ctx, formatter): | ||
super(PersianTotalHoursDirective, self).post_parser(ctx, formatter) | ||
if self.name in ctx and ctx[self.name]: | ||
ctx['hours'] = ctx[self.name] | ||
|
||
|
||
class PersianHoursDirective(PersianNumberDirective): | ||
""" | ||
Representing Hour24 format in persian form. | ||
""" | ||
|
||
def __init__(self, key, name): | ||
super(PersianHoursDirective, self).__init__(key, name, consts.PERSIAN_HOUR24_ZERO_PADDED_REGEX, int) | ||
|
||
def format(self, d): | ||
return super(PersianHoursDirective, self).format(int(d.hours)) | ||
|
||
def post_parser(self, ctx, formatter): | ||
super(PersianHoursDirective, self).post_parser(ctx, formatter) | ||
if self.name in ctx and ctx[self.name]: | ||
ctx['hours'] = ctx[self.name] | ||
|
||
|
||
class TotalMinutesDirective(Directive): | ||
""" | ||
Representing total minutes in a timedelta. | ||
""" | ||
|
||
def format(self, d): | ||
return super(TotalMinutesDirective, self).format(d.total_minutes) | ||
|
||
def post_parser(self, ctx, formatter): | ||
super(TotalMinutesDirective, self).post_parser(ctx, formatter) | ||
if self.name in ctx and ctx[self.name]: | ||
ctx['minutes'] = ctx[self.name] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import division | ||
from datetime import timedelta | ||
|
||
from khayyam.formatting import JalaliTimedeltaFormatter | ||
|
||
|
||
class JalaliTimedelta(timedelta): | ||
_parts = None | ||
|
||
@classmethod | ||
def formatterfactory(cls, fmt): | ||
return JalaliTimedeltaFormatter(fmt) | ||
|
||
def strftime(self, format_string): | ||
""" | ||
Format codes referring to hours, minutes or seconds will see 0 values. | ||
For a complete list of formatting directives, see :doc:`/directives`. | ||
:param format_string: The format string. | ||
:return: A string representing the date, controlled by an explicit format string | ||
:rtype: unicode | ||
""" | ||
return self.formatterfactory(format_string).format(self) | ||
|
||
@classmethod | ||
def strptime(cls, date_string, fmt): | ||
""" | ||
Return a :py:class:`khayyam.JalaliTimedelta` corresponding to *date_string*, parsed according to format. | ||
:py:class:`ValueError` is raised if the *date_string* and format can't be parsed with | ||
:py:class:`khayyam.formatting.JalaliTimedeltaFormatter` instance returned by | ||
:py:meth:`khayyam.JalaliTimedelta.formatterfactory` method. | ||
:param date_string: str The representing date & time in specified format. | ||
:param fmt: str The format string. | ||
:return: Jalali timedelta object. | ||
:rtype: :py:class:`khayyam.JalaliTimedelta` | ||
""" | ||
|
||
result = cls.formatterfactory(fmt).parse(date_string) | ||
# | ||
# def assert_a_xor_b(a, b): | ||
# if a in result: | ||
# if b in result: | ||
# raise ValueError('Cannot use %s and %s, together.' % (a, b)) | ||
# result[b] = result[a] | ||
# del result[a] | ||
# | ||
# assert_a_xor_b('total_hours', 'hours') | ||
# assert_a_xor_b('total_minutes', 'minutes') | ||
|
||
result = {k: v for k, v in result.items() if k in ( | ||
'days', 'hours', 'minutes', 'seconds', 'milliseconds', 'microseconds' | ||
)} | ||
|
||
return cls(**result) | ||
|
||
def _calculate_parts(self): | ||
# days, seconds, microseconds | ||
remaining_seconds = self.seconds | ||
hours = remaining_seconds // 3600 # 1-23 | ||
remaining_seconds %= 3600 | ||
total_hours = self.days * 24 + hours + remaining_seconds / 3600 | ||
|
||
minutes = remaining_seconds / 60 # 1-59 | ||
remaining_seconds %= 60 | ||
total_minutes = total_hours * 60 + remaining_seconds / 60 | ||
seconds = remaining_seconds # 1-59 | ||
|
||
self._parts = { | ||
'hours': hours, | ||
'minutes': minutes, | ||
'seconds': seconds, | ||
'total_hours': total_hours, | ||
'total_minutes': total_minutes | ||
} | ||
|
||
@property | ||
def parts(self): | ||
if self._parts is None: | ||
self._calculate_parts() | ||
return self._parts | ||
|
||
@property | ||
def hours(self): | ||
return self.parts['hours'] | ||
|
||
@property | ||
def total_hours(self): | ||
return self.parts['total_hours'] | ||
|
||
@property | ||
def minutes(self): | ||
return self.parts['minutes'] | ||
|
||
@property | ||
def total_minutes(self): | ||
return self.parts['total_minutes'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
|
||
from khayyam import JalaliTimedelta | ||
|
||
|
||
class TestJalaliTimedelta(unittest.TestCase): | ||
def setUp(self): | ||
pass | ||
|
||
def test_strftime_strptime(self): | ||
d1 = JalaliTimedelta(12, 23, 12, 3, 45, 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(JalaliTimedelta.strptime('34855', '%H').total_seconds(), 125478000) | ||
self.assertEqual(JalaliTimedelta.strptime(u'۳۴۸۵۵', '%k').total_seconds(), 125478000) | ||
|
||
self.assertEqual(JalaliTimedelta.strptime('07', '%I').total_seconds(), 25200) | ||
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%H %I').total_hours, 34343) | ||
self.assertEqual(JalaliTimedelta.strptime('34343 08', '%H %I').hours, 23) | ||
|
||
self.assertEqual(JalaliTimedelta.strptime('34855', '%M').total_seconds(), 2091300) | ||
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( | ||
# d1.strptime('Panjshanbeh 23 Esfand 1375 12:03:45 PM', '%Q'), | ||
# d1 - timedelta(microseconds=34567)) | ||
# | ||
# self.assertEqual(d1.isoformat(), '%s-12-23T12:03:45.034567' % self.leap_year) | ||
# tz_datetime = d1.astimezone(teh_tz) | ||
# self.assertEqual(tz_datetime.strftime('%Z'), 'Iran/Tehran') | ||
|
||
|
||
if __name__ == '__main__': # pragma: no cover | ||
unittest.main() |
Oops, something went wrong.