# Author: Muness Alrubaie
# Last Modified: 2004-12-21
#
# Copyright 2004 Muness Alrubaie
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may 
# not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
#    http://www.apache.org/licenses/LICENSE-2.0 
#
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License.
#
#
# Returns the date referred to as follows:
# First arg is the offset, i.e. the date to start from
# Second arg is the multiplier to multiply the count by (e.g. 7 for a week)
# Third arg is the count (i.e. the number of additions to make).
#
# So, for example
# [[DateOffset(2005/01/05,7,0)]] retuns Wed Jan Wed Jan 05
# [[DateOffset(2005/01/05,7,4)]] returns Wed Feb 02
#
# You can also configure the input and output date formats.  
# (See http://python.org/doc/2.3.4/lib/module-time.html)
# e.g. [[DateOffset(2005/01/05,7,0,%Y/%m/%d,%A %B %d)]] returns Wednesday January 05

import time

def execute(hdf, txt, env):
    args=txt.split(',',4)
    offset=args[0]
    multiplier=args[1]
    count=args[2]
    if len(args) > 3:
        infmt=args[3]
        outfmt=args[4]
    else:
        infmt = "%Y/%m/%d"
        outfmt = "%a %b %d"
    
    startdate = time.mktime(time.strptime(offset,infmt))
    
    sec_per_day = 24 * 60 * 60
    days = int(multiplier) * int(count)
    secs = sec_per_day * days
    
    return time.strftime(outfmt, time.localtime(startdate + secs))
