Comments (5)
You are right, this is (well was 😃 ) an undesired behavior.
I just pushed a fix (660c6b9) on the master
branch.
Basically, pendulum will now treat years, months, weeks and days as whole units and will not apply the transition but only set the proper timezone. Even if you combine it with other units (hours, minutes, seconds and microseconds).
Let's take an example:
import pendulum
dt = pendulum.create(2016, 2, 20, 22, tz='America/Sao_Paulo')
# <Pendulum [2016-02-20T22:00:00-02:00]>
# Let's add 3 hours (we will stumble across the DST transition)
dt.add(hours=3)
# <Pendulum [2016-02-21T00:00:00-03:00]>
# Let's add 1 day
# (the transition has occurred but we won't apply it (i.e. remove one hour))
dt.add(days=1)
# <Pendulum [2016-02-21T22:00:00-03:00]>
# But the difference will actually be 25 hours
dt.add(days=1).diff(dt).in_hours()
# 25
As for your initial example:
>>> pendulum.parse("2016-01-12", tz="America/Sao_Paulo").add(months=4)
# <Pendulum [2016-05-12T00:00:00-03:00]>
>>> pendulum.parse("2016-01-30", tz="America/Sao_Paulo").add(months=4)
# <Pendulum [2016-05-30T00:00:00-03:00]>
from pendulum.
Blazingly fast! =)
I installed the git tip to try that commit, and I've found something perhaps related to the date adding/subtracting (I'm using IPython):
In [1]: import pendulum
In [2]: # Change the string format just to avoid some undesired coloring on GitHub
In [3]: pendulum.set_to_string_format("'%Y-%m-%d %H:%M:%S %_z'")
In [4]: dt = pendulum.parse("2016-01-30", tz="America/Sao_Paulo")
In [5]: p = dt.add(months=4) - dt
In [6]: p.range("months")
Out[6]:
[<Pendulum ['2016-01-30 00:00:00 -02:00']>,
<Pendulum ['2016-02-29 00:00:00 -03:00']>,
<Pendulum ['2016-03-29 00:00:00 -03:00']>,
<Pendulum ['2016-04-29 00:00:00 -03:00']>,
<Pendulum ['2016-05-29 00:00:00 -03:00']>]
The period range seem to get the previous output to add, instead of using its p.start
value. I was expecting the result above to be like this:
In [7]: [dt.add(months=m) for m in range(5)]
Out[7]:
[<Pendulum ['2016-01-30 00:00:00 -02:00']>,
<Pendulum ['2016-02-29 00:00:00 -03:00']>,
<Pendulum ['2016-03-30 00:00:00 -03:00']>,
<Pendulum ['2016-04-30 00:00:00 -03:00']>,
<Pendulum ['2016-05-30 00:00:00 -03:00']>]
That shouldn't make any difference when adding time (hours, minutes, seconds, microseconds), but when we're adding dates (days, weeks, months, years) that matters a lot as some_dt_obj.add(days=1).add(days=1)
and some_dt_obj.add(days=2)
might give us different results. The aforementioned range example isn't about DST, but here's an example that propagates something specific to a DST transition:
In [8]: my_birthday = pendulum.parse("2016-10-14", tz="America/Sao_Paulo")
In [9]: (my_birthday.add(weeks=1) - my_birthday).range("days")
Out[9]:
[<Pendulum ['2016-10-14 00:00:00 -03:00']>,
<Pendulum ['2016-10-15 00:00:00 -03:00']>,
<Pendulum ['2016-10-16 01:00:00 -02:00']>,
<Pendulum ['2016-10-17 01:00:00 -02:00']>,
<Pendulum ['2016-10-18 01:00:00 -02:00']>,
<Pendulum ['2016-10-19 01:00:00 -02:00']>,
<Pendulum ['2016-10-20 01:00:00 -02:00']>]
In [10]: [my_birthday.add(days=d) for d in range(7)]
Out[10]:
[<Pendulum ['2016-10-14 00:00:00 -03:00']>,
<Pendulum ['2016-10-15 00:00:00 -03:00']>,
<Pendulum ['2016-10-16 01:00:00 -02:00']>,
<Pendulum ['2016-10-17 00:00:00 -02:00']>,
<Pendulum ['2016-10-18 00:00:00 -02:00']>,
<Pendulum ['2016-10-19 00:00:00 -02:00']>,
<Pendulum ['2016-10-20 00:00:00 -02:00']>]
from pendulum.
For your first example, I agree that the behavior you expect should be the correct one since if you just do dt.add(months=4)
you will get <Pendulum ['2016-05-30 00:00:00 -03:00']>
but this is not the last instance returned by range()
.
As for you second example, I think range()
should return the second batch of results too.
Anyway, the two are related anyway and it should be an easy fix. I will keep you posted about the resolution of this issue.
from pendulum.
It should be fixed on the master
branch.
from pendulum.
Wow! Thanks! Now my birthday is in your test_range_with_dst
code. ^^
As some_dt.start_of("week")
calls self.previous
, which happens to call the subtract
method, the other places where I had seen a similar behavior (namely, the next
, previous
, start_of
and end_of
methods) seem to be fixed together as they call the same add
and subtract
methods.
I'll be using the git tip until a version bump with this fix gets available. =)
from pendulum.
Related Issues (20)
- dst_rule not implemented in 3.x? Has it in documentation but examples don't work. HOT 3
- in_months() returns wrong values in some cases in January to February HOT 1
- pendulum PanicException on FreeBSD 14 HOT 4
- [Feature Request] Add strict mode for from_format()
- backports-zoneinfo does not compile on 3.10, and hard to package (pyinstaller, zipapp,...) HOT 3
- Bug / regression: Unable to parse string '031' with 'DDDD' format using Pendulum 3.0.0
- Regression - Inconsistent DBAPI serialization of pendulum datetimes
- Duration division, multiplication and mod broken for month lengths. HOT 1
- Default datetime string format changed in 3.0 release HOT 2
- Security issue or bug with pipenv ? HOT 1
- to_iso8601_string
- instance() fails for datetimes with pytz.FixedOffsets
- "import pendulum" causes unwanted import of pytest (presence of pytest in sys.modules often used to control Python behaviour) HOT 1
- MMM is not case-insensitive like Java
- If you have the /etc/timezone file set to a timezone string, pendulum.UTC has that timezone's utcoffset instead of None HOT 1
- in_words() exclude week
- timedelta additions to pendulum.DateTime HOT 1
- extreme slow down in `in_tz` after upgrading from 2.1.2 to 3.0.0 HOT 3
- Day of week constants
- pendulum.instance & dateutil.tz compatibility
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pendulum.