Re: Some more questions on Dates / Intervals

Ralf Schlatterbeck <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.user
Message-ID <[email protected]>
On Sat, Mar 27, 2021 at 11:34:28AM +0100, Nagy Gabor wrote:
> 
> How can I use this date/interval arithmetic on checking deadline
> property of an issue item? For example, how can I test "context/deadline
> < .+3d"? (I want to color these issues red in the issue class view.) 
> How can I create the Date object ".+3d" in the html templates? Exact
> tal:condition etc. would be appreciated. :) OK, I can do this by writing
> an extension (util) and import Roundup's date module, but I guess there
> is a simpler way I missed.

You can just compare dates, I think you can access the underlying
hyperdb date object with .value. I'm not sure it's a good idea to do
this in the templates directly, it's probably better to add a utility
method.

You can put a file in the extensions directory of your tracker and
register a function in it, something along the lines of

def my_utility_function (some, parameters):
    # do something

def init(instance):
    instance.registerUtil('my_utility_function', my_utility_function)

You can then use this in the html templates with

utils.my_utility_function(...)

Concerning date comparison: You can compare hyperdb date objects just
like you would expect, e.g.,

from roundup.date import Date, Interval

dateobj = Date('.-7d')

if dateobj < Date('.+3w'):
    ...

or do date arithmetics

day = Interval('1d')

if dateobj < Date('.') + day:

> I figured out the followings so far:
> 
> 1. [+ BUG?] The now() method of DateHTMLProperty can be used to create a
> Date() object of the form ".+3d":

OK, I didn't know there is a now method in the DateHTMLProperty.

> 
> But a strange thing happens here: In the same HTML template,
> 
> python:context.deadline.now().pretty('%Y-%m-%d.%H:%M')
> 
> gives 2021-03-27.11:05, but
> 
> python:context.deadline.now('+3d').pretty('%Y-%m-%d.%H:%M')
> 
> gives 2021-03-30.12:05.
> 
> I guess we have a local time issue here... (I could completely turn off
> that, if I could, both the servers and the users are in Hungary, the
> server's local time would be perfect.)

Tonight Europe switches to daylight savings time, that's where the
additional hour comes from. You chose a bad time to experiment with date
arithmetics :-)

You *can* have naive dates (without a timezone) in roundup by declaring
the Date properties in your Class definition in schema.py with the
parameter offset=0, e.g.

customclass = Class(db, 'customclass', prop=Date(offset=0) ...)

> 
> 2. But it seems
> 
> tal:condition="python: context.deadline < context.deadline.now('+3d')"
> 
> works as expected.

See above, tonight we switch to daylight savings time :-)

> 3. But how can I create custom Date object? In fact, I want to check that
> "deadline <= 2021-03-30.23:59:59" not "deadline <= 2021-03-30.11:05".
> And many other situations can occur when I want to define a custom date.
> (I observed that dates in standard form can be compared as a string. That
> is always an ultimate solution: Using .pretty(), I can easily create the 
> string "2021-03-30.23:59:59" from the output of now()).

I think, putting too much code into the template itself results at least
in hard-to-maintain templates, so as suggested above it's probably
better to produce some extension functions that you can use in the
template. And I'd not do date arithmetics with the HTML wrapper classes
but directly with hyperdb classes but that's just a personal preference,
in fact I don't know how the wrapper behaves with date arithmetics, I've
never done too much in the templating.

Ralf
-- 
Dr. Ralf Schlatterbeck                  Tel:   +43/2243/26465-16
Open Source Consulting                  www:   www.runtux.com
Reichergasse 131, A-3411 Weidling       email: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.