incrementing a time tuple by one day

David Stockwell winexpert at hotmail.com
Fri Sep 24 09:03:42 EDT 2004


Hi All,

Thanks for your help here.   In my case, I only needed to be approximately 
accurate to about 23 hours 59 minutes or roughly a day.

I did manage to figure it out.

Here is some source code that I used in debugging the problem (apologies in 
advance for the lack of comments/names,etc)
------- start
import time
import datetime
#   This is a Hack

month = 'm'
day   = 'd'
sYear = 'y'

_fHol  = [{month:1, day:1, sYear:2004}
                       ,{month:1,  day:19,  sYear:2004}
                       ,{month:2,  day:16,  sYear:2004}
                       ,{month:5,  day:31,  sYear:2004}
                       ,{month:7,  day:5,   sYear:2004}
                       ,{month:9,  day:6,   sYear:2004}
                       ,{month:10, day:11,  sYear:2004}
                       ,{month:11, day:11,  sYear:2004}
                       ,{month:11, day:25,  sYear:2004}
                       ,{month:12, day:24,  sYear:2004}
                       ,{month:12, day:31,  sYear:2004}
                       ,{month:1,  day:17,  sYear:2005}
                       ]

def _isAHoliday(pyear,pmonth,pday):
        """
               checks to see if a given date is a holiday
        """
        isAHoliday = 0

        for holiday in _fHol:
            if pyear == holiday['y']:
                if pmonth == holiday['m']:
                    if pday == holiday['d']:
                        isAHoliday = 1
        return isAHoliday

def _extendForHoliday(startDate,allowedMax):
        extendedDays = 0
        i = 0
        year  = 0
        month = 1
        day   = 2
        dayOfWeek = 6
        saturday = 5
        sunday = 6
        while i < allowedMax:
            d = datetime.datetime.fromtimestamp(time.mktime(startDate))
            testDate = d + datetime.timedelta(days=i)
            tt = time.strptime("%s%s%s" % (testDate.year, testDate.month, 
testDate.day), "%Y%m%d")
            if _isAHoliday(tt[year],tt[month],tt[day]):
                print "Found a holiday: %s/%s/%s Yeah!!" % (tt[year], 
tt[month], tt[day])
                extendedDays += 1
            elif tt[dayOfWeek] == saturday or tt[dayOfWeek] == sunday:
                print "Got a Weekend day (%s/%s/%s) Yeah!" % (tt[year], 
tt[month], tt[day])
                extendedDays += 1
            i += 1

        print "# days extended are %s" %  extendedDays

        return extendedDays
start = '2004/11/10'

test1 = time.strptime(start,"%Y/%m/%d")
max = 5

y = _extendForHoliday(test1, max)
print "For %s and range %s you get %s days off " % (start,max,y)

--------  fin


David
-------
Surf a wave to the future with a free tracfone 
http://cellphone.duneram.com/index.html




>From: claird at lairds.us (Cameron Laird)
>To: python-list at python.org
>Subject: Re: incrementing a time tuple by one day
>Date: Thu, 23 Sep 2004 20:08:05 GMT
>
>In article <donn-B27766.12063123092004 at gnus01.u.washington.edu>,
>Donn Cave  <donn at u.washington.edu> wrote:
> >In article <QcudnRWf360rm87cRVn-vg at powergate.ca>,
> > Peter Hansen <peter at engcorp.com> wrote:
> >> David Stockwell wrote:
>			.
>			.
>			.
> >Well, who knows, maybe datetime is the answer for him,
> >but if not, I would just use 24*60*60 instead of trying
> >to get one day in seconds out of mktime().  (I think if
> >you look at the date closer, it isn't decremented all!)
> >
> >   Donn Cave, donn at u.washington.edu
>
>While I quickly lost track of who said what, I've been around
>long enough to know that some people are touchy about there
>NOT being 24 * 60 * 60 seconds in a day near "daylight-savings"
>switches.  I have no idea whether that's the case for Mr.
>Stockwell.
>--
>http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list