dateutils
dateutils copied to clipboard
Can't seem to round correctly (to move months/years)
Hi
Ubuntu 18.04 user here.
dateutils.dround -n 2019-01-13 1y 2019-01-13
I would expect output woult 2020-01-01
I also tried 2y without a difference.
I also tried
dateutils.dround now 1y
Trying to round up to the next new year start date. What am I doing wrong? Thanks!
Hi there,
I see where you're coming from. Well, first of all the solution to your problem as it stands:
$ dateutils.dround -n 2019-01-13 +1d +Jan
2020-01-01
Now to the explanation: date rounding in dround is more like an advanced setting of some of the date (and time) components than rounding as you'd expect from decimals. The +1d sets the day of month component to 1, leaving everything to the right as is and adapting everything to the left accordingly (according with -n means that the result value is strictly greater than the input value).
In the same vein, +Jan sets the month component to January while keeping everything to the right (which is 01 as day-of-month now) and adapting everything to the left such that the new result is (strictly == -n) greater than the interim result.
Using this logic, +1y would set the year component to the "next year 1", which isn't supported because the year component doesn't wrap around. Arguably, I could imagine you want an error message here that points out this peculiarity?
There is another operating mode of rounding that is implemented with time and date/time values: co-class rounding. Something that resembles the rounding of decimals a lot. Like you can round to the next full minute (/+1m) or the next full hour (/+1h) or the next quarter of an hour (/+15m). Example:
$ dateround.dround now /+15m
2019-01-14T08:30:00
These rounding specifiers change everything to the right of the specified component to 0 and everything to the left gets "adapted accordingly". I didn't implement this variant of rounding for the date components because the "setting to zero" bit would have to be redefined. But seeing as you're not the first to ask for such a feature I should maybe get around to defining and implementing things in the date category as well.
/+1dwould be equivalent to/+24h, i.e. next day's midnight/+1mowould be the (next) month's first,/+1qwould be equivalent to/+3mo, the beginning of the next quarter- and finally
/+1ywould be equivalent to/+12mo, the beginning of the next year - in the same spirit,
/+10ythe next decade,/+100ythe next century.
There aren't many values that actually make sense, like what is /+5d or /+5mo supposed to be? The next day/month divisible by 5 implies that the month or year could be subdivided in multiples of 5. Ah well, it's syntactic sugar for the explicit form anyway.
Hi again, you can try the latest master (0d504438). I finally got around to implementing the co-classes for days, months, quarters and years.
$ dateround -n 2019-01-13 /1y
2020-01-01
This works because years are now considered "subdivisions" of millenia. So to get the first day of the next decade:
$ dateround -n today /10y
2020-01-01
And the beginning of the current decade:
$ dateround -n today /-10y
2010-01-01
Hope this helps, I will roll a release soon.
thanks for the followup!
may today be the best day of your life,
david parkinson founder - aroonilabs.com blog - davidparkinson.com https://www.davidparkinson.com/ twitter - @followdavid https://twitter.com/followdavid
On Tue, Feb 5, 2019 at 8:12 AM Sebastian Freundt [email protected] wrote:
Hi again, you can try the latest master (0d50443 https://github.com/hroptatyr/dateutils/commit/0d504438f89d7efb19875f8c81d8ed4fb9f9537c). I finally got around to implementing the co-classes for days, months, quarters and years.
$ dateround -n 2019-01-13 /1y 2020-01-01
This works because years are now considered "subdivisions" of millenia. So to get the first day of the next decade:
$ dateround -n today /10y 2020-01-01
And the beginning of the current decade:
$ dateround -n today /-10y 2010-01-01
Hope this helps, I will roll a release soon.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hroptatyr/dateutils/issues/92#issuecomment-460651412, or mute the thread https://github.com/notifications/unsubscribe-auth/AAh-eyKKj17Ptvz9arYqDUoTwzE5-jPDks5vKZEzgaJpZM4Z9Woa .
Somewhat unrelated; is there a way to round to a the next instance of a specific date?
Say I want to create a recurring reminder to file taxes on april 15th. is there a way to use your utility to give me the next future anniversary date of april 15th?
Thanks in advance!
Yes, that's possible:
$ dateutils.dround today 15 Apr
2019-04-15
This will round to the next 15th, then to next April. The rounding bits (15 and Apr) are applied left to right, and this is not the same as:
$ dateutils.dround 2019-03-20 Apr 15
2019-05-15
as 2019-03-20 will first be rounded to April (gives 2019-04-20), then to the next 15th, which happens to be 2019-05-15.
thanks so much for your reply and your utility!
may today be the best day of your life,
david parkinson founder - aroonilabs.com blog - davidparkinson.com https://www.davidparkinson.com/ twitter - @followdavid https://twitter.com/followdavid
On Mon, Feb 11, 2019 at 2:08 AM Sebastian Freundt [email protected] wrote:
Yes, that's possible:
$ dateutils.dround today 15 Apr 2019-04-15
This will round to the next 15th, then to next April. The rounding bits ( 15 and Apr) are applied left to right, and this is not the same as:
$ dateutils.dround 2019-03-20 Apr 15 2019-05-15
as 2019-03-20 will first be rounded to April (gives 2019-04-20), then to the next 15th, which happens to be 2019-05-15.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/hroptatyr/dateutils/issues/92#issuecomment-462244906, or mute the thread https://github.com/notifications/unsubscribe-auth/AAh-e5L1zmjUDBuh13J8k37jLHaTzbThks5vMSUNgaJpZM4Z9Woa .