Re: formatting calendars for display

Tim Berners-Lee wrote:

> The difficulty which phpicalendar has and any other solution would have
> is the function which takes a local time and a timezone name like 
> America/New_York and
> generates from it the time in Z.
> 
> Do you have that in python?
> 

Well, almost! At least in the newest release of python (2.3). It 
contains a new module called 'datetime', which also contains, among 
others, a skeleton for timezone module. Skeleton in the sense that an 
abstract superclass is defined, and there is also an example of how to 
define specific timezone classes for utc, local timezone, and the us 
timezones. If one knows the daytime transition rules (which I don't...) 
it should not be difficult to implement the European and Asian ones, too 
(I wonder why this has not been done...). I attach this example to this 
mail.

If one has this, than the following works easily (just copied from my 
screen):


(535) Desktop> python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import tzinfoEx
 >>> from datetime import datetime
 >>> from tzinfoEx import Eastern, utc
 >>> t1 = datetime.now() # returns in local timezone
 >>> t2 = datetime.now(utc) # returns in UTC
 >>> t3 = datetime.now(Eastern) # returns in US Eastern
 >>> print t1
2003-07-31 11:01:08.803000
 >>> print t2
2003-07-31 09:01:21.361000+00:00
 >>> print t3
2003-07-31 05:01:34.320000-04:00
 >>>




> There also seemed to be a difference between iCalendar from evolution 
> and from iCal,
> in that evolution writes the timezone definition in the calendar file, 
> the latter just uses a name.
> 
> timbl
> 
> On Tuesday, Jul 29, 2003, at 17:38 US/Eastern, Dan Connolly wrote:
> 
>> I've seen various bits of work on formatting
>> calendars for display...
>>
>> I wrote something to take data from the palm
>> datebook, projected into RDF, and display
>> it as HTML...
>>   http://dev.w3.org/cvsweb/2001/palmagent/datebook2html.xsl
>>
>> David Dorward reported a problem with that recently;
>> the fix is pretty easy, but I haven't gotten around
>> to releasing it and I'm not sure when I will...
>>
>> I see Hugo has been doing some related work...
>>
>>   "weekly.pl is a Perl script generating an XHTML weekly view of an RDF
>> calendar document."
>>   http://dev.w3.org/cvsweb/2003/weekly-view/
>>
>> And I've seen a few people struggling with bugs
>> in phpicalendar... I thought it was a read/write
>> database thing, but it's actually just a
>> read-only .ics->.html formatter. php is great
>> for database applications, but as a general-purpose
>> programming language, I prefer python (or even perl).
>> And for XML->XML transformations, XSLT is usually
>> worth trying.
>>
>> I don't have any particular suggestion or request;
>> but I'd like to move the discussion from my
>> private mailbox, where it competes with lots of
>> other stuff, to this forum where other folks have
>> a chance to help.
>>
>>
>> -- 
>> Dan Connolly, W3C http://www.w3.org/People/Connolly/
>>

-- 

Ivan Herman
W3C Head of Offices
C/o W3C Benelux Office at CWI, Kruislaan 413
1098SJ Amsterdam, The Netherlands
tel: +31-20-5924163; mobile: +31-641044153;
URL: http://www.w3.org/People/all?pictures=yes#ivan
from datetime import tzinfo, timedelta, datetime

ZERO = timedelta(0)
HOUR = timedelta(hours=1)

# A UTC class.

class UTC(tzinfo):
    """UTC"""

    def utcoffset(self, dt):
        return ZERO

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return ZERO

utc = UTC()

# A class building tzinfo objects for fixed-offset time zones.
# Note that FixedOffset(0, "UTC") is a different way to build a
# UTC tzinfo object.

class FixedOffset(tzinfo):
    """Fixed offset in minutes east from UTC."""

    def __init__(self, offset, name):
        self.__offset = timedelta(minutes = offset)
        self.__name = name

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return self.__name

    def dst(self, dt):
        return ZERO

# A class capturing the platform's idea of local time.

import time as _time

STDOFFSET = timedelta(seconds = -_time.timezone)
if _time.daylight:
    DSTOFFSET = timedelta(seconds = -_time.altzone)
else:
    DSTOFFSET = STDOFFSET

DSTDIFF = DSTOFFSET - STDOFFSET

class LocalTimezone(tzinfo):

    def utcoffset(self, dt):
        if self._isdst(dt):
            return DSTOFFSET
        else:
            return STDOFFSET

    def dst(self, dt):
        if self._isdst(dt):
            return DSTDIFF
        else:
            return ZERO

    def tzname(self, dt):
        return _time.tzname[self._isdst(dt)]

    def _isdst(self, dt):
        tt = (dt.year, dt.month, dt.day,
              dt.hour, dt.minute, dt.second,
              dt.weekday(), 0, -1)
        stamp = _time.mktime(tt)
        tt = _time.localtime(stamp)
        return tt.tm_isdst > 0

Local = LocalTimezone()


# A complete implementation of current DST rules for major US time zones.

def first_sunday_on_or_after(dt):
    days_to_go = 6 - dt.weekday()
    if days_to_go:
        dt += timedelta(days_to_go)
    return dt

# In the US, DST starts at 2am (standard time) on the first Sunday in April.
DSTSTART = datetime(1, 4, 1, 2)
# and ends at 2am (DST time; 1am standard time) on the last Sunday of Oct.
# which is the first Sunday on or after Oct 25.
DSTEND = datetime(1, 10, 25, 1)

class USTimeZone(tzinfo):

    def __init__(self, hours, reprname, stdname, dstname):
        self.stdoffset = timedelta(hours=hours)
        self.reprname = reprname
        self.stdname = stdname
        self.dstname = dstname

    def __repr__(self):
        return self.reprname

    def tzname(self, dt):
        if self.dst(dt):
            return self.dstname
        else:
            return self.stdname

    def utcoffset(self, dt):
        return self.stdoffset + self.dst(dt)

    def dst(self, dt):
        if dt is None or dt.tzinfo is None:
            # An exception may be sensible here, in one or both cases.
            # It depends on how you want to treat them.  The default
            # fromutc() implementation (called by the default astimezone()
            # implementation) passes a datetime with dt.tzinfo is self.
            return ZERO
        assert dt.tzinfo is self

        # Find first Sunday in April & the last in October.
        start = first_sunday_on_or_after(DSTSTART.replace(year=dt.year))
        end = first_sunday_on_or_after(DSTEND.replace(year=dt.year))

        # Can't compare naive to aware objects, so strip the timezone from
        # dt first.
        if start <= dt.replace(tzinfo=None) < end:
            return HOUR
        else:
            return ZERO

Eastern  = USTimeZone(-5, "Eastern",  "EST", "EDT")
Central  = USTimeZone(-6, "Central",  "CST", "CDT")
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
Pacific  = USTimeZone(-8, "Pacific",  "PST", "PDT")

Received on Thursday, 31 July 2003 05:00:57 UTC