dgilland / zulu Goto Github PK
View Code? Open in Web Editor NEWA drop-in replacement for native Python datetimes that embraces UTC.
Home Page: https://zulu.readthedocs.io
License: MIT License
A drop-in replacement for native Python datetimes that embraces UTC.
Home Page: https://zulu.readthedocs.io
License: MIT License
Hey Derrick, the docs for zulu still refer to zulu.delta(), but it was deprecated in 0.11.0
Hi, thanks for a great time library.
I'm facing a weird issue. When parsing a datetime string with 31.12 or 30.12 I get the wrong year when formatting as "YYYY"
To reproduce:
`
import zulu
zulu.parse("2019-12-31 00:00:00.000Z").format("YYYY")
'2020'
zulu.parse("2019-12-30 00:00:00.000Z").format("YYYY")
'2020'
zulu.parse("2019-12-29 00:00:00.000Z").format("YYYY")
'2019'
`
Should this really happen?
Due to the way datetime.timedelta
is implemented, the zulu.Delta
subclass isn't used:
>>> delta = zulu.Delta(seconds=5)
>>> delta + delta
datetime.timedelta(0, 10)
>>> delta - delta
datetime.timedelta(0)
>>> delta / 1
datetime.timedelta(0, 5)
>>> delta // 1
datetime.timedelta(0, 5)
>>> delta * 1
datetime.timedelta(0, 5)
Arrow tokens: https://arrow.readthedocs.io/en/latest/#tokens
Unicode date patterns: http://www.unicode.org/reports/tr35/tr35-19.html#Date_Field_Symbol_Table
I think we can "cheat" a bit and simply map Arrow Unicode tokens to strptime-style tokens. Nothing fancy, perhaps just a simple look-ahead parser that translates things like YYYY-MM-DD
into %Y-%m-%d
or M/D/YY
into %-m/%-d/%y
(note that %-m
and %-d
require glibc installed so may need a more universal way to handle that or simply make note of it as a requirement).
It can lead to confusion between zulu.delta
and zulu.Delta
since these two have different parameters and handle positional arguments differently. So we'll rename zulu.delta
to zulu.parse_delta
to make it more explicit.
Allow user to do something like that following:
zulu.parse({'year': 2017, 'month': 2, 'day': 9})
zulu.parse(**{'year': 2017, 'month': 2, 'day': 9})
zulu.parse_delta({'hours': 1, 'minutes': 30, 'seconds': 15})
zulu.parse_delta(**{'hours': 1, 'minutes': 30, 'seconds': 15})
DateTime.days_in_year()
--> Should return total number of days in a particular year.
DateTime.days_in_years()
--> Should return total number of days in a given years range. For example, DateTime.days_in_years(2016, 2018)
should return the total number of days from January 1st 2016
to December 31st 2018
.
DateTime.days_in_months()
--> Should return total number of days in a given month range. For example, DateTime.days_in_months(2, 5)
should return the total number of days from February 1st
to May 1st
.
DateTime.weeks_in_year()
--> We can use isocalendar()
and return the number of weeks.
Zulu.add and Zulu.subtract already do this so makes sense for Zulu.shift too behave the same.
Zulu uses the same Unicode date patterns as Babel does but Babel has locale support. Should be a drop-in replacement with the added benefit of supporting a locale option in zulu.DateTime.format()
.
example from python 3.8
Python 3.8.1 (default, Jan 8 2020, 23:09:20)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.10.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from dateutil import tz
In [2]: import zulu
In [3]: n = zulu.now()
In [4]: n
Out[4]: <Zulu [2020-01-14T22:37:07.389997+00:00]>
In [5]: n.astimezone(tz.gettz("Europe/Paris"))
Out[5]: <Zulu [2020-01-14T22:37:07.389997+00:00]>
In [6]: n.datetime.astimezone(tz.gettz("Europe/Paris"))
Out[6]: datetime.datetime(2020, 1, 14, 23, 37, 7, 389997, tzinfo=tzfile('/usr/share/zoneinfo/Europe/Paris'))
Also tried to update tox in a zulu fork and test branch: all unittests are OK in python 3.7 and less, but not ok in python 3.8:
https://travis-ci.com/ThomasChiroux/zulu/builds/144422588
This may have been related to this python bug:
https://bugs.python.org/issue32417 which has been merged in python 3.8
still investigating...
(a work around could be as seen in the example to call datetime before astimezone and force a cast to datetime before calling astimezone, but it's less powerfull than standard zulu lib (no timezone parsing)
Usage:
dt1 = zulu.create(2017, 1, 1)
dt2 = zulu.create(2017, 1, 2)
dt3 = zulu.create(2017, 1, 1)
dt1.is_before(dt2) is True
dt1.is_after(dt2) is False
dt1.is_on_or_before(dt3) is True
dt1.is_on_or_after(dt3) is True
dt2.between(dt1, dt3) is True
dt1.between(dt3, dt2) is True
dt1.between(dt2, dt3) is False
Should basically call DateTime.shift
.
dt.add(hours=5) == dt.shift(hours=5)
dt.add(hours=-5) == dt.shift(hours=-5)
dt.sub(hours=5) == dt.shift(hours=-5)
dt.sub(hours=-5) == dt.shift(hours=5)
Should return the number of days in the current month of the DateTime object.
Can use calendar.monthrange
:
>>> from calendar import monthrange
>>> monthrange(2011, 2)
(1, 28)
>>> monthrange(2012, 2)
(2, 29)
DateTime.get_days()
--> Should return the total number of days between the given dates. For example, DateTime.get_days('05/23/2016', '06/23/2016')
should return you the total number of days between those two dates.
DateTime.get_weeks()
--> Same as get_days()
but, this returns total number of weeks between the given dates.
DateTime.get_weekday()
--> Should return a tuple something like (3, 'Wednesday')
when a date is given. For example, DateTime.get_weekday('8/3/2016')
should return (3, 'Wednesday')
where 3 represents third day in the week
.
Basically want to have something that works like this:
dt1 = DateTime(2015, 1, 1)
dt2 = DateTime(2017, 1, 1)
dt1.format_from_now()
# dt1 - now()
#1 year, 7 months ago
dt1.format_to_now()
# now() - dt1
# in 1 year, 7 months
dt1.format_from(dt2)
# dt1 - dt2
#2 years ago
dt1.format_to(dt2)
# dt2 - dt1
# in 2 years
dt2.format_from_now()
# dt2 - now()
# in 4 months
dt2.format_to_now()
# now() - dt2
#4 months ago
dt2.format_from(dt1)
# dt2 - dt1
# in 2 years
dt2.format_to(dt1)
# dt1 - dt2
#2 years ago
Can use babel.dates.format_timedelta.
We need to have similar methods like arrow.Arrow.span_range()
and arrow.Arrow.range()
. (http://crsmithdev.com/arrow/#ranges-spans)
We have DateTime.naive
but would be good to have DateTime.datetime
that returns a native datetime
object.
To allow zulu.parse
to be the universal way to get some kind of zulu object, we can add a flag to zulu.parse
that will allow it to parse the input into a zulu.Delta
object.
Similar to arrow.span
, arrow.floor
, and arrow.ceil
: http://crsmithdev.com/arrow/#ranges-spans
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.