2016年4月18日 星期一

[python] python 3.x , date itreating from start date to stop(today) date.


from datetime import datetime,timedelta


def daterange(begin, end, delta = timedelta(1)):
    """Form a range of dates and iterate over them.

    Arguments:
    begin -- a date (or datetime) object; the beginning of the range.
    end   -- a date (or datetime) object; the end of the range.
    delta -- (optional) a timedelta object; how much to step each iteration.
             Default step is 1 day.

    Usage:

    """
    if not isinstance(delta, timedelta):
        delta = timedelta(delta)

    ZERO = timedelta(0)

    if begin < end:
        if delta <= ZERO:
            raise StopIteration
        test = end.__gt__
    else:
        if delta >= ZERO:
            raise StopIteration
        test = end.__lt__

    while test(begin):
        yield begin
        begin += delta


def main():

    start = datetime(2010, 1, 1,19,00,00)
    stop = datetime.today()
    for x in daterange(start,stop):
        # only when today, it need to download after 7:00pm, so force the start date all is 19:00:00
        if(stop > x):
            #print(stop,x)
            year=str(x.year)
            month=str('%02d' % (x.month))
            day=str('%02d' % (x.day))

            download_xxxxx_from_web(year,month,day)

        else:
            print('For ',x,'Need to wait to 7:00')
    return


main()


沒有留言:

張貼留言