Compare commits

...

27 Commits

Author SHA1 Message Date
Timo Furrer b896237cca release: 0.6.1 2019-01-06 11:22:35 +00:00
Timo Furrer 4e58e6455e Merge pull request #169 from ftobia/fix_intervals
Fix issue #168: intervals greater than one day were incorrect.
2019-01-04 15:13:52 +01:00
Frank Tobia 42cc61e7a8 Update authors. 2019-01-03 14:00:47 -05:00
Frank Tobia 9abd618083 Add basic tests for maya.intervals() 2019-01-03 13:58:06 -05:00
Frank Tobia 2c2a8d03c8 Fix issue #168: intervals greater than one day were incorrect. 2019-01-02 14:03:34 -05:00
Timo Furrer bdf2bb332b Merge pull request #166 from kennethreitz/feature/pendulum-2
Use pendulum >= 2.0.2
2018-12-14 13:30:22 +01:00
Timo Furrer 6522525d7c alpha release: 0.6.0a1 2018-12-02 13:37:48 +00:00
Timo Furrer 3a83f10988 Use pendulum >= 2.0.2. Closes #160 2018-12-02 13:31:22 +00:00
Timo Furrer 05eb0d0051 Merge pull request #164 from possnfiffer/patch-1
Update README.rst
2018-10-18 11:31:58 +02:00
__ROLLER__ 61eabc54e0 Update README.rst 2018-10-15 22:05:26 -06:00
kennethreitz 6bb380e7c5 Update README.rst 2018-09-17 08:12:21 -04:00
Timo Furrer cc698817b4 release: 0.5.0 2018-05-26 15:19:57 +02:00
Timo Furrer 9964e9305e Merge pull request #154 from alxwrd/support-locales
Allow slang methods to change locale
2018-05-26 15:16:17 +02:00
Alex Ward d87236a30d update authors 2018-05-26 11:19:57 +01:00
Alex Ward d44a1dd2f6 add tracer tests for slang_ locales 2018-05-26 11:19:36 +01:00
Timo Furrer ed5d72b34c Merge pull request #155 from DavidMuller/patch-1
Add Example of Creating a MayaDT Using a Unix Time
2018-05-22 21:38:21 +02:00
David Muller 9c1e94f956 Add example that instantiates MayaDT w/ unix time 2018-05-22 10:45:01 -07:00
Alex Ward 6755968f69 remove unused calendar import 2018-05-22 09:57:32 +01:00
Alex Ward 3760af2c41 use pendulum and dateparser for slang_date 2018-05-21 22:37:31 +01:00
Alex Ward df09625e74 fix failing test 2018-05-15 22:56:28 +01:00
Alex Ward 2d848bd62a typo fix 2018-05-15 22:55:07 +01:00
Alex Ward a99cddf76f allow changing the locale for slang_time 2018-05-15 22:27:16 +01:00
Timo Furrer 1b432d6626 release: 0.4.3 2018-05-15 09:13:21 +02:00
Timo Furrer f09d6eec63 Split RFC3339 test 2018-05-15 09:13:01 +02:00
Timo Furrer 62a6283200 Merge pull request #150 from marcelstoer/patch-1
Fix RFC3339 representation
2018-05-15 09:08:52 +02:00
Marcel Stör 9ef43a29d9 Add RFC3339 unit test 2018-05-14 21:49:47 +02:00
Marcel Stör a2bff42439 Fix RFC3339 representation
Must be at most 1-digit millisecond.
2018-05-14 20:48:28 +02:00
8 changed files with 142 additions and 17 deletions
+2
View File
@@ -24,3 +24,5 @@ In chronological order:
- Evan Mattiza <emattiza@gmail.com> (`@emattiza <https://github.com/emattiza>`_)
- Dima Spivak <dima@spivak.ch> (`@dimaspivak <https://github.com/dimaspivak>`_)
- Tom Barron <tusculum@gmail.com> (`@dtbarron <https://github.com/tbarron>`_)
- Alex Ward <alxwrd@gmail.com> (`@alxwrd <https://github.com/alxwrd>`_)
- Frank Tobia <frank.tobia@gmail.com> (`@ftobia <https://github.com/ftobia>`_)
+5
View File
@@ -1,3 +1,8 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
freezegun = "*"
pytest = "*"
+6 -1
View File
@@ -26,6 +26,7 @@ Art by `Sam Flores
<https://www.instagram.com/samagram12/>`_ (Photo by `Kenneth Reitz
<https://www.instagram.com/kennethreitz/>`_).
If you're interested in financially supporting Kenneth Reitz open source, consider `visiting this link <https://cash.me/$KennethReitz>`_. Your support helps tremendously with sustainability of motivation, as Open Source is no longer part of my day job.
☤ Basic Usage of Maya
---------------------
@@ -77,6 +78,10 @@ Behold, datetimes for humans!
>>> m = maya.MayaDT.from_struct(time.gmtime())
>>> print(m)
Wed, 20 Sep 2017 17:24:32 GMT
>>> m = maya.MayaDT(time.time())
>>> print(m)
Wed, 20 Sep 2017 17:24:32 GMT
>>> rand_day.day
7
@@ -133,7 +138,7 @@ From here, there are a number of methods available to you, which you can use to
☤ What about Delorean, Arrow, & Pendulum?
-----------------------------------------
All these project complement each other, and are friends. Pendulum, for example, helps power Maya's parsing.
All these projects complement each other, and are friends. Pendulum, for example, helps power Maya's parsing.
Arrow, for example, is a fantastic library, but isn't what I wanted in a datetime library. In many ways, it's better than Maya for certain things. In some ways, in my opinion, it's not.
+1 -1
View File
@@ -1 +1 @@
__version__ = '0.4.2'
__version__ = '0.6.1'
+51 -12
View File
@@ -14,6 +14,7 @@ import pendulum
import snaptime
from tzlocal import get_localzone
from dateutil.relativedelta import relativedelta
from dateparser.languages.loader import default_loader
from .compat import cmp, comparable
@@ -277,7 +278,7 @@ class MayaDT(object):
def rfc3339(self):
"""Returns an RFC 3339 representation of the MayaDT."""
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4] + "Z"
return self.datetime().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-5] + "Z"
# Properties
# ----------
@@ -331,15 +332,38 @@ class MayaDT(object):
# Human Slang Extras
# ------------------
def slang_date(self):
""""Returns human slang representation of date."""
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
return humanize.naturaldate(dt)
def slang_date(self, locale="en"):
""""Returns human slang representation of date.
def slang_time(self):
""""Returns human slang representation of time."""
dt = self.datetime(naive=True, to_timezone=self.local_timezone)
return humanize.naturaltime(dt)
Keyword Arguments:
locale -- locale to translate to, e.g. 'fr' for french.
(default: 'en' - English)
"""
dt = pendulum.instance(self.datetime())
try:
return _translate(dt, locale)
except KeyError:
pass
delta = humanize.time.abs_timedelta(
timedelta(seconds=(self.epoch - now().epoch)))
format_string = "DD MMM"
if delta.days >= 365:
format_string += " YYYY"
return dt.format(format_string, locale=locale).title()
def slang_time(self, locale="en"):
""""Returns human slang representation of time.
Keyword Arguments:
locale -- locale to translate to, e.g. 'fr' for french.
(default: 'en' - English)
"""
dt = self.datetime()
return pendulum.instance(dt).diff_for_humans(locale=locale)
def utc_offset(time_struct=None):
@@ -472,7 +496,7 @@ class MayaInterval(object):
try:
end = parse(end)
except pendulum.parsing.exceptions.ParserError as e:
except (pendulum.parsing.exceptions.ParserError, TypeError) as e:
end = cls.parse_iso8601_duration(end, start=start)
return cls(start=start, end=end)
@@ -715,7 +739,7 @@ def when(string, timezone='UTC', prefer_dates_from='current_period'):
return MayaDT.from_datetime(dt)
def parse(string, timezone='UTC', day_first=False, year_first=True):
def parse(string, timezone='UTC', day_first=False, year_first=True, strict=False):
""""Returns a MayaDT instance for the machine-produced moment specified.
Powered by pendulum.
@@ -730,11 +754,14 @@ def parse(string, timezone='UTC', day_first=False, year_first=True):
between YDM and YMD. (default: False)
year_first -- if true, the first value (e.g. 2016/05/01)
is parsed as year (default: True)
strict -- if False, allow pendulum to fall back on datetime parsing
if pendulum's own parsing fails
"""
options = {}
options['tz'] = timezone
options['day_first'] = day_first
options['year_first'] = year_first
options['strict'] = strict
dt = pendulum.parse(str(string), **options)
return MayaDT.from_datetime(dt)
@@ -759,6 +786,16 @@ def _seconds_or_timedelta(duration):
return dt_timedelta
def _translate(dt, target_locale):
en = default_loader.get_locale("en")
target = default_loader.get_locale(target_locale)
naturaldate = humanize.naturaldate(dt)
base = en.translate(naturaldate, settings=dateparser.conf.settings)
return target.info["relative-type"][base][-1]
def intervals(start, end, interval):
"""
Yields MayaDT objects between the start and end MayaDTs given,
@@ -769,4 +806,6 @@ def intervals(start, end, interval):
while current_timestamp.epoch < end.epoch:
yield current_timestamp
current_timestamp = current_timestamp.add(seconds=interval.seconds)
current_timestamp = current_timestamp.add(
seconds=interval.total_seconds()
)
+1 -1
View File
@@ -25,7 +25,7 @@ required = [
'pytz',
'dateparser>=0.7.0',
'tzlocal',
'pendulum>=1.0, <=1.5.1',
'pendulum>=2.0.2',
'snaptime'
]
+19 -2
View File
@@ -158,9 +158,19 @@ def test_slang_date():
assert d.slang_date() == 'tomorrow'
def test_slang_date_locale():
d = maya.when('tomorrow')
assert d.slang_date(locale='fr') == 'demain'
def test_slang_time():
d = maya.when('1 hour ago')
assert d.slang_time() == 'an hour ago'
assert d.slang_time() == '1 hour ago'
def test_slang_time_locale():
d = maya.when('1 hour ago')
assert d.slang_time(locale='de') == 'vor 1 Stunde'
@pytest.mark.parametrize("string,kwds,expected", [
@@ -239,13 +249,20 @@ def test_datetime_to_timezone():
assert dt.tzinfo.zone == 'US/Eastern'
def test_rfc3339():
def test_rfc3339_epoch():
mdt = maya.when('2016-01-01')
out = mdt.rfc3339()
mdt2 = maya.MayaDT.from_rfc3339(out)
assert mdt.epoch == mdt2.epoch
def test_rfc3339_format():
rfc3339 = maya.MayaDT.rfc3339(maya.when('2016-01-01T12:03:03Z'))
# it's important that the string has got a "max 1-digit millis" fragment
# as per https://tools.ietf.org/html/rfc3339#section-5.6
assert rfc3339 == '2016-01-01T12:03:03.0Z'
@pytest.mark.usefixtures("frozen_now")
def test_comparison_operations():
now = maya.now()
+57
View File
@@ -570,3 +570,60 @@ def test_interval_from_iso8601_duration():
assert interval.start == s
assert interval.end == e
@pytest.mark.parametrize(
"start_string,end_string,interval,expected_count",
[
("2019-01-03 11:40:00Z", "2019-01-03 11:40:20Z", 2, 10),
(
"2019-01-03 11:40:00Z",
"2019-01-03 11:40:30Z",
timedelta(seconds=2),
15,
),
("2019-01-03 11:40:00Z", "2019-01-03 11:45:00Z", 2 * 60, 3),
(
"2019-01-03 11:40:00Z",
"2019-01-03 11:51:00Z",
timedelta(minutes=1),
11,
),
("2019-01-03 11:40:00Z", "2019-01-03 21:40:00Z", 3 * 60 * 60, 4),
(
"2019-01-03 11:40:00Z",
"2019-01-03 13:41:00Z",
timedelta(hours=1),
3,
),
("2019-01-03 11:40:00Z", "2019-01-09 11:40:00Z", 3 * 60 * 60 * 24, 2),
("2019-01-03 11:40:00Z", "2019-01-05 12:00:00Z", timedelta(days=2), 2),
],
ids=(
"seconds",
"seconds-timedelta",
"minutes",
"minutes-timedelta",
"hours",
"hours-timedelta",
"days",
"days-timedelta",
),
)
def test_intervals(start_string, end_string, interval, expected_count):
start = maya.parse(start_string)
end = maya.parse(end_string)
assert len(list(maya.intervals(start, end, interval))) == expected_count
def test_issue_168_regression():
start = maya.now()
end = start.add(weeks=1)
gen = maya.intervals(start=start, end=end, interval=60 * 60 * 24)
# Since the bug causes the generator to never end, first sanity
# check that two results are not the same.
assert next(gen) != next(gen)
assert (
len(list(maya.intervals(start=start, end=end, interval=60 * 60 * 24)))
== 7
)