Puzzled with Time Server conversion problems?

Malcolm Tredinnick malcolmt at smart.net.au
Tue Oct 5 07:52:26 EDT 1999


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.

Cheers,
Malcolm Tredinnick





More information about the Python-list mailing list