Edgewall Software

MacroBazaar: DateOffset.py

File DateOffset.py, 1.7 KB (added by Muness Alrubaie <muness@…>, 7 years ago)

DateOffset? Macro

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