Puzzled with Time Server conversion problems?

M.-A. Lemburg mal at lemburg.com
Sun Oct 10 08:41:18 EDT 1999


Malcolm Tredinnick wrote:
> 
> On Tue, Oct 05, 1999 at 01:37:02AM +0000, Benjamin Schollnick wrote:
> [ ... snip ...]
> > Client13 was written to take advantage of a daemon running under
> > Windows NT on
> > port 13, which broadcasts like this:
> >
> >               Mon Oct  4 21:27:45 1999 (In plain text.)
> >
> > Which is nice, but annoying because for some *STRANGE* reason I can't
> > get
> > time.strptime (datetime, '%A, %B, %d, %Y %X') to work.
> >
> > Everytime I try STRPTIME, I get a attribute error.  Can anyone point
> > out what
> > I'm goofing up?
> 
> I think you want the format string to be '%a %b %d %X %Y'. That is,
> some of the commas are not necessary and %A and %B expect the *full*
> day and month names, whereas %a and %b handle the abbreviated
> versions. Secondly, your example had the time and year the wrong way
> around.
> 
> >
> > BTW-> Anyone have suggestions for making the server a little bit
> > "friendlier" either
> >       code / execution wise?
> 
> Yes. :-)
> 
> [refer to original post for server code]
> 
> Since we can probably assume that this server will be used to serve
> times between 1900 and 2099 (you were intending to rewrite it sometime
> in the next 100 years, weren't you?), the leap calculation can be
> simplified.
> 
> 1900 is not a leap year and 2000 is a leap year, you can probably just
> lose the leapyear() function and rewirte calc_leap_years as
> 
> def calc_leap_years(year):
>         if (year > 1900):
>                 return ((year - 1900) - 1)/4
>         else:
>                 return 0
> 
> Of course, you can reduce it to a single line if you make the
> assumption that the year will always be greater than 1900. And it
> probably isn't wonderful programming practice to return 0 if say
> year==1897 anyway (raise an exception?), but that is another issue.
> This should be a fair bit faster than looping through the years.

Just a suggestion that will probably make writing such a server
easier: use mxDateTime for the job. It does string parsing (you
don't even have to worry about the format used -- it tries
several common formats before it gives up) and also handles
things like leap years, ticks (seconds since the epoch) and
conversion to many different time standards.

mxDateTime is available via my Python Pages.

The docs also have a link to the Calendar FAQ which has lots of
useful information about calendars, date and time:

	http://www.pip.dknet.dk/~pip10160/calendar.html

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                    82 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/






More information about the Python-list mailing list