1 """ strptime version 1.3, Time-stamp: <96/09/23 21:22:24 flognat>
2 The reverse of strftime.
4 Copyright (C) 1996 Andy Eskilsson, flognat@fukt.hk-r.se
6 This is free software; unrestricted redistribution is allowed under the
7 terms of the GPL. For full details of the license conditions of this
8 software, see the GNU General Public License.
10 And here comes the documentation:
12 Throw a string and a format specification at strptime and if everything
13 is ok you will get a tuple containing 9 items that are compatible with
17 strptime(inputstring, formatstring)
19 Little errorchecking... so you'd better know what you are doing.
22 from strptime import *
23 mktime(strptime("26/6 1973", "%d/%m %Y"))
25 And voila you have the second when the author of this function was born.
27 The supported format identifiers are:
28 %a weekday in short text-form, e.g. Mon
29 %A weekday in long text-form, e.g. Monday
30 %b month in short text-form, e.g. Jul
31 %B month in long text-form e.g. July
32 %c the format specified by DateAndTimeRepresentation
33 %d the day in month in numeric form, e.g. 24
34 %H hour in 24 hour form
35 %j julian day (day of year)
36 %m month in numeric format
39 %T Time in '%H:%M:%S'-format
41 %x date in format represented by DateRepresentation
42 %X time in format represented by TimeRepresentation
47 I have done some thinking here (*REALLY*) and it is possible to configure
48 this module so it uses other languages by adding their names to the
49 dictionaries first in the file, and setting the variable LANGUAGE.
51 For your exercise I have inserted the swedish names ;-)
53 The lfind, name, complex, numbers and parse functions are for internal
54 use, called by strptime.
56 Uh.. oh yeah.. if you want to get in touch with me.. I am reachable
57 at flognat@fukt.hk-r.se, the newest version of this file can probably
58 be found somewhere close to http://www.fukt.hk-r.se/~flognat
60 If you like it, send a postcard to Andy Eskilsson
65 Uhm be gentle with the bug-reports, its the first time for me ;-)
71 LongDayNames
={ 'English' : [ 'Monday', 'Tuesday', 'Wednesday',
72 'Thursday', 'Friday', 'Saturday', 'Sunday'],
73 'Swedish' : [ 'Måndag', 'Tisdag', 'Onsdag', 'Torsdag',
74 'Fredag', 'Lördag', 'Söndag']}
75 ShortDayNames
={ 'English' : [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
76 'Swedish' : [ 'Mån', 'Tis', 'Ons', 'Tor', 'Fre', 'Lör', 'Sön']}
78 LongMonthNames
={ 'English' : ['none', 'January', 'February', 'March', 'April',
79 'May', 'June', 'July', 'August', 'September',
80 'October', 'November', 'December'],
81 'Swedish' : ['none', 'Januari', 'Februari', 'Mars', 'April',
82 'Maj', 'Juni', 'Juli', 'Augusti','September',
83 'Oktober', 'November', 'December'] }
84 ShortMonthNames
={ 'English' : ['none', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
85 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
86 'Swedish' : ['none', 'Jan', 'Feb', 'Mar', 'Apr', 'Maj', 'Jun',
87 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec']}
88 DateAndTimeRepresentation
={ 'English' : '%a %b %d %H:%m:%S %Y',
89 'Swedish' : '%a %d %b %Y %H:%m:%S' }
91 DateRepresentation
= { 'English' : '%m/%d/%y',
92 'Swedish' : '%d/%m/%y'}
94 TimeRepresentation
= { 'English' : '%H:%M:%S',
95 'Swedish' : '%H:%M:%S'}
99 BadFormatter
='An illegal formatter was given'
101 #Check if strinf begins with substr
102 def lfind(str, substr
):
103 return string
.lower(str[:len(substr
)])==string
.lower(substr
)
105 #atoms consisting of other atoms
106 def complex(str, format
, base
):
109 string
=DateAndTimeRepresentation
[LANGUAGE
]
113 string
=DateRepresentation
[LANGUAGE
]
115 string
=TimeRepresentation
[LANGUAGE
]
117 return parse(str, string
, base
)
120 def names(str, format
, base
):
123 selection
=ShortDayNames
[LANGUAGE
]
126 selection
=LongDayNames
[LANGUAGE
]
129 selection
=ShortMonthNames
[LANGUAGE
]
132 selection
=LongMonthNames
[LANGUAGE
]
141 base
[result
]=selection
.index(match
)
145 def numeric(str, format
, base
):
147 if code
=='d': result
='day'
148 elif code
=='H': result
='hour'
149 elif code
=='j': result
='juliand'
150 elif code
=='m': result
='month'
151 elif code
=='M': result
='min'
152 elif code
=='S': result
='sec'
153 elif code
=='w': result
='weekd'
154 elif code
=='y': result
='shortYear'
155 elif code
=='Y': result
='year'
158 while str[i
] in string
.whitespace
: i
=i
+1
161 while not str[j
] in string
.whitespace
and str[j
]!=format
[1]: j
=j
+1
164 while not str[j
] in string
.whitespace
: j
=j
+1
168 # hmm could check exception here, but what could I add?
169 base
[result
]=string
.atoi(str[i
:j
])
173 parseFuns
={ 'a':names
, 'A':names
, 'b':names
, 'B':names
, 'c':complex, 'd':numeric
,
174 'H':numeric
, 'j':numeric
, 'm':numeric
, 'M':numeric
, 'S':numeric
,
175 'T':complex, 'w':numeric
, 'x':complex, 'y':numeric
, 'Y':numeric
}
177 # Well split up in atoms, reason to why this is separated from atrptime
178 # is to be able to reparse complex atoms
179 def parse(str, format
, base
):
180 atoms
=string
.split(format
, '%')
184 # Hey I am laazy and think that the format is exactly what the string is!
185 charCounter
=charCounter
+len(atoms
[atomCounter
])
186 atomCounter
=atomCounter
+1
188 while atomCounter
< len(atoms
) and charCounter
< len(str):
189 atom
=atoms
[atomCounter
]
191 if atom
=='': # escaped
192 charCounter
=charCounter
+1
193 atomCounter
=atomCounter
+1
194 charCounter
=charCounter
+len(atoms
[atomCounter
])
197 parsefunction
=parseFuns
[atom
[:1]]
199 raise BadFormatter
, atom
[:1]
200 grabbed
=apply(parsefunction
, (str[charCounter
:], atom
, base
))
201 charCounter
=charCounter
+grabbed
+len(atom
)-1
203 atomCounter
=atomCounter
+1
207 # Ok here we go, tadaaa --> STRPTIME <-- at last..
208 def strptime(str, format
):
209 """Converts str specified by format to tuple useable by the time module"""
212 returnTime
['shortYear']=None
213 returnTime
['month']=0
218 returnTime
['weekd']=0
219 returnTime
['juliand']=0
222 parse(str, format
, returnTime
)
224 if returnTime
['shortYear']!=None:
225 returnTime
['year']=returnTime
['shortYear']+1900
227 return (returnTime
['year'], returnTime
['month'], returnTime
['day'],
228 returnTime
['hour'], returnTime
['min'], returnTime
['sec'],
229 returnTime
['weekd'], returnTime
['juliand'], returnTime
['dst'])
231 # just for my convenience
234 pdb
.run('strptime("% Tue 3 Feb", "%% %a %d %b")')
238 a
=asctime(localtime(time()))
240 b
=strptime(a
, '%a %b %d %H:%M:%S %Y')
242 print strptime("%% % Tue 3 Feb", "%%%% %% %a %d %b")
243 print strptime('Thu, 12 Sep 1996 19:42:06 GMT', '%a, %d %b %Y %T GMT')
244 print strptime('Thu, 12 Sep 1996 19:42:06 GMT', '%a, %d %b %Y %T')
245 print strptime('Thu, 12 Sep 1996 19:42:06', '%a, %d %b %Y %T')
247 if __name__
== '__main__':