/[theodore]/bunnyblog/modules/dateutils.pyc


UCC Code Repository

Contents of /bunnyblog/modules/dateutils.pyc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Tue Jan 29 14:32:01 2008 UTC (12 years ago) by svn-admin
File MIME type: application/octet-stream
File size: 14300 byte(s)
Re-import of repository after repository database corruption.

1 m
2 ,Gc @sdklZddddddddd d
3 d d g Zd ddddddgZhdd<dd<dd<dd<dd<dd<dd<dd<d d<d
4 d<d d<d d<Zhdd<dd<dd<dd<dd<dd<dd<dd<d d<d
5 d<d d<d d<Zddddddddddddg Zddddddddddddg ZdddddddgZdd d!d"d#d$d%d&d'd(d)d*g Z d+Z
6 d,Z d-Z d.Z d/Zd0Zd1Zd2d3d4Zd5Zd6Zd7Zd8Zd9Zd:Zd;Zd<Zhd=Zhd>d?<d@d3<dAd?<dBd?<dCd3<ZedDjoeGHe\ZZZeeeeZ e GHee GHxve!one"dEZ#e#dFjoPne"dGZ$e"dHZ%ee&e#e&e$e&e%Z e GHee GHqWe
7 dId?dJGHxEe!o=e"dKZ#e#dLjoPneddMdJe&e# GHqzWxDe!o<e"dNZ#e#dOjoPnedPd?dJe&e#GHqWxe!oe"dQZ#e#dFjoPne"dRZ$e"dSZ%edPd?dJe&e%e&e$e&e#GHedPd?dJe&e%e&e$e&e#\ZZZdTGeeeeGHq WndUS(V(s localtimetjanuarytfebruarytmarchtapriltmaytjunetjulytaugustt septembertoctobertnovembertdecembertsundaytmondayttuesdayt wednesdaytthursdaytfridaytsaturdayiiiitSundaytMondaytTuesdayt WednesdaytThursdaytFridaytSaturdaytJanuarytFebruarytMarchtApriltMaytJunetJulytAugustt SeptembertOctobertNovembertDecembercCs|djp'|djp|djp |djotSn=|djo!t|o
8 d}qxd}nt|d}||jotSntSdS(sReturns true if the supplied date is a possible date
9 and False if it isn't :-) (Note - it *only* tests that the year is greater than zero).i iiiiN(tmonthtyeartdaytFalset
10 isleapyeartnumdayst monthlisttTrue(R(R&R'R+((t./modules/dateutils.pytrealdates4  
11 
12  cCsS|ddjotSn7|ddjotSn|ddjotSntSdS(smGiven a year as an integer (e.g. 2004) it returns True if the year is a leap year,
13 and False if it isn't.iiidiN(R'R)R-(R'((R.R*scCs@|djo!t|o
14 d}q<d}nt|d}|S(sDGiven a year and a month it returns how many days are in that month.iiiiN(R&R*R'R+R,(R'R&R+((R.t daysinmonths  
15 
16 cCs|}|}|}|djo|d}n|}d|d|d||d|d|d}|djo|d8}n|d }|S( sPassed in a date, in integers, it returns the day of the week.
17 Output is expressed as an integer from 0-6.
18 0 is Sunday, 1 is Monday....... iiii iidiiiN(R(tdR&tmR'tytzt dayofweek(R(R&R'R5R1R2R3R4((R.t datetodays 2 
19 cCsLt|djodSnt|d t|dd!t|dd!fS(sPassed in a datestring - in the form 'yyyymmdd'
20 (e.g. 20040122 being 22nd January 2004) -
21 it returns an integer tuple ( yyyy, mm, dd ).
22 If the datestring is of the wrong length it returns None.
23 (It assumes a four figure year).iiiN(tlent
24 datestringtNonetint(R8((R.tdatestringtointsscCst|}x"t|djod|}qWt|}t|}t|djod|}nt|djod|}n|||S(sgGiven three integers for day, month and year
25 it returns a datestring 'yyyymmdd' (for easy storage).it0iN(tstrR'R3R7R&R2R(R1(R(R&R'R1R2R3((R.tintstodatestring s   cCs4y t}Wn dSnX|d|d|dfS(svReturns the local date using the localtime function
26 from the time module.
27 Returns integers - ( yyyy, mm, dd ).iiiiiN(iii(t localtimet datetuple(R@((R.t
28 returndates  iic Cst|||}||jo|||fSn||jo||}d|}n||}d|}|p ||jo
29 |}n| }t
30 ||||S(sPGiven a date as three integers (year, month and day) it returns the nearest
31 date that is 'dayofweek'. (dayofweek should be an integer from 0 - 6. 0 is Sunday, 1 Monday etc..)
32 If afteronly is set to 1 then it finds the nearest date of that day, on or *after* the specified.
33 Returns integers - ( yyyy, mm, dd ).
34 dayofweek defaults to Tuesday (2) and afteronly defaults to 0 as they are the defaults I'm using for the Victory Day program this is written for.
35 This is used for : e.g find the nearest Tuesday to a given date, or find the nearest Tuesday *after* a given date !iN( R6R(R&R'tthisdayR5tforwardtbackwardt afteronlyt
36 differencet
37 addnumdays( R(R&R'R5RERDRFRBRC((R.t
38 nearestday"s  
39 
40 
41 
42 cCs.|djo|djo4t|o'|djo|d8}d}d}nx|djor|djo t|p|djot|do
43 d}nd}||jo|d7}||8}qQPqQWx|djo |djo!t|o
44 d}qd}nt|d}||jo9||8}|d jo|d7}qdd}|d7}nP|djo!t|o
45 d}qd}nt|d}||jo9|d jo|d7}nd}|d7}||}qqWx3|djo)t|||\}}}|d8}qWn|djo| }|djo.t|o!|djo|d8}d}nx|djor|djo t|p|djot|do
46 d}nd}||jo|d8}||8}q{Pq{Wx|djo|djo!t|o
47 d}qGd}nt|d}||}||jo||8}|djo|d8}nd }|d8}|djo!t|o
48 d}qd}nt|d}||}qPqWx;|djo)t |||\}}}|d8}qWn|||fS( sGiven a date as three integers (year, month and day) and a number of days to add or subtract
49 to that date (the integer modifier, positive or negative value) - it returns the correct date
50 as a tuple of integers - ( yyyy, mm, dd ).iiiiiiminii N(
51 tmodifierR&R*R'R(R+R,tincdatetadjustertdecdate(R(R&R'RIRKR+((R.RG;s '
52 
53  8
54  
55    
56 
57  
58    
59 
60   
61   '
62 
63  8
64  
65    
66 
67 
68  
69  
70   
71 
72  cCs|djo!t|o
73 d}q<d}nt|d}||jo|d7}n8|djod}|d7}d}n|d7}d}|||fS(sBGiven a date it adds one day to the date and returns the new date.iiiii N(R&R*R'R+R,R((R(R&R'R+((R.RJs  
74 
75   
76 
77 
78 cCs|djo|d8}nt|djo|d8}d}d}nM|djo't|o
79 d}nd}d}nt|d}|d8}|||fS( sIGiven a date it subtracts one day from the date and returns the new date.iii iiiiN(R(R&R'R*R,(R(R&R'((R.RLs  
80 
81   
82 
83 
84 c
85 Cs||}||}x&|djo|d8}|d7}qWx&|djo|d7}|d8}q@Wt||} || jo
86 | }n|djo| }t
87 }nt }x7|djo)||||\}}}|d8}qW|||fS(sAGiven a date as three integers (year1, month1 and day1) and another number of days (day2), months (month2)
88 and years (year2) to add to that date (or subtract from it) - it returns the new date as a tuple of integers - ( yyyy, mm, dd ).
89 Note :
90 Feb 28th + 1 month = March 31st
91 Feb 29th + 1 month = March 31st
92 January 29th to 31st + 1 month = feb 28th/29th
93 August 31st + 1 month = September 30th
94 We add the years together, then the months, then correct for the 'end of month' (e.g. we change Sep 31st to Sep 30th)
95 Finally we add any extra days on.ii iN( tyear1tyear2R'tmonth1tmonth2R&R0R+tday1tday2RLtthisfuncRJ(
96 RQRORMRRRPRNRSR'R&R+((R.tadddates. 
97 
98  
99  
100  
101  
102  cCs|djo|d}|d}n |d}t|d}d|t|d}td|td|||d S( s"This is an implementation of the Julian Day system. This
103 is a continuous count of days from January 1, 4713 B.C.
104 Given a date in in integers it returns an integer value for the date
105 This represents it's Julian Day number as above.
106 This only works for dates represented using the the Gregorian
107 calendar which was adopted in the US/UK on Oct. 15, 1582 - but
108 at different times elsewhere (so historical dates may not be in this system....).iii idiif365.25f30.600100000000001iBN(R&R'R:tAtBR((R'R&R(RURV((R.tdaycounts 
109 
110 cCs|d}tddd|S(svGiven the number for a date using the Julian Day System,
111 it returns that date as integer tuple (year, month, day).i&n%iiiN(RWRG(RW((R.t counttodates
112 cCs t|||t|||S(sGiven two dates it returns the number of days between them.
113 If date1 is earlier than date2 then the result will be positive.N(RWRNRPRRRMRORQ(RQRORMRRRPRN((R.t daysbetweenscCs|djo|djodSnt|}t|djo|d}n|djodSn/|djod Sn|d
114 jod SndSd S( spTakes an integer day and returns the correct finish for it
115 1 = 'st', 2 = 'nd', 3 = 'rd', 4-10 = 'th' etc....iitthiit1tstt2tndt3trdN(R(R=tdaystrR7(R(Ra((R.t dayfinish
116 s    c Ksdddddg}|hjot|to
117 |}nx,|D]$}|i|pd||<qCqCWd}|do%|ot t
118 |||d}n|d jo|dod
119 t |}n t |}|dp(||t|dt|d 7}n%|t|d d|t|7}|do|o|d 7}n|o|dt |7}n|do|d 7}n|S(sGiven a date in in integers, it returns the date as a nicely formatted string :
120 e.g. 24th January 1997 or 2nd February 1948
121 configs accepts the following keywords :
122 dayofweek, addzero, addcom, fullstop, monthfirst
123 e.g. print formatteddate(12, 8, 1974, dayofweek=1, addzero=0, addcom=1, fullstop=1, monthfirst=0)
124 Monday 12th August, 1974.
125 If dayofweek is set to 1 then the day of the week will also be printed :
126 e.g. Monday 24th January 1997
127 If addzero is set to 1 then days 1-9 will have an additional zero :
128 e.g. 02nd February 1948
129 If addcom is set to 1 then there will be a comma between the month and the year :
130 e.g. 24th January, 1997
131 If fullstop is set to 1 then there will be a fullstop after the year :
132 e.g. 24th January 1997.
133 If monthfirst is set to 1 then then the month will be put before the day :
134 e.g. January 24th 1997
135 If the year is set to zero then it will be missed off.
136 (and the dayofweek will be treated as 0 in this case as well).
137 There is a dictionary called dateformcon defined in the dateutils module with all the config values
138 defined and some good standard settings :-)
139 This dictionary can be passed in instead of the individual settings.
140 R5taddzerotaddcomtfullstopt
141 monthfirstitt i
142 R<it,t.N(t keywordlistt
143 configdictt
144 isinstancetdicttconfigstmemberthas_keyt outstringR'tdaysR6R(R&R=RaRbtmonths( R(R&R'RlRoRkRrRpRa((R.t formatteddates0
145   ($ R5iRcRdReRft__main__s&Enter Year of date (Enter to quit) >> RgsEnter Month >> s Enter Day >> i isEnter Modifier (0 to quit) >> R<is%Enter Day of Week 0-6 (7 to quit) >> t7is&Enter Years to Add (Enter to quit) >> sEnter Months to Add >> sEnter Days To Add >> s'The nearest Tuesday after that date is N('ttimeR?t monthslowert dayslowert monthdictt monthdictleapR,t monthlistleapRsRtR/R*R0R6R;R>RARHRGRJRLRTRWRXRYRbRut dateformcont__name__R'R&R(ttestR-t raw_inputtxR3R4R:("RzRTR'R&RJR6RXRWRuRRHRbR?RGRR*RYRAR;RyR/RLR,R0R(R{R}RtRsR>R~R3R|R4((R.t?%s *rr***}  
146 
147  k        23      $         ,6

Properties

Name Value
svn:mime-type application/octet-stream

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26