Time constants module - standard-library fodder?

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Fri May 30 02:51:51 EDT 2003


Howdy,

Many programmers in many languages break the "use named constants" rule,
but one of the most egregious examples seems to be constants for number
of seconds in an hour, day, week, etc.

Using the numeric literal '604800' instead of 'SECS_PER_WEEK' is as bad
as using '3.14159265' instead of 'math.pi', for all the same reasons:
it's prone to hard-to-find typing errors, it's not obvious to anyone
unfamiliar with the value of the constant, it gives no context as to why
this number is being used, etc.

Yet, though the 'math' module gives us 'pi' and 'e' constants as
exports, I can't find any standard library location for the (arguably
much more commonly used) time-period constants.

So, I'm currently using this home-grown module when I need to use
number-of-seconds constants.  I've written this file so many times in so
many languages I can't remember, but it's not until I encountered
Python, which has so much common-sense stuff in the standard library,
that I thought to wonder why these constants aren't there.

Is there a better way?  A standard module I've missed?  Any improvements
to offer?  Should this go into the standard library, in some form?  Why
or why not?

=====
#!/usr/bin/env python
# timeconst.py - Define time period constants
#
# 2003-05-30 by Ben Finney <bignose at zip.com.au>
# No copyright -- dedicated to the public domain.
# You are free to use this for anything you like.
# (An email about it would be appreciated but not required.)

SECS_PER_MINUTE = 60
MINUTES_PER_HOUR = 60
HOURS_PER_DAY = 24
DAYS_PER_WEEK = 7
MONTHS_PER_YEAR = 12

DAYS_PER_YEAR_AVG = 365.25
DAYS_PER_MONTH_AVG = DAYS_PER_YEAR_AVG / MONTHS_PER_YEAR
WEEKS_PER_YEAR_AVG = DAYS_PER_YEAR_AVG / DAYS_PER_WEEK

SECS_PER_HOUR = MINUTES_PER_HOUR * SECS_PER_MINUTE
SECS_PER_DAY = HOURS_PER_DAY * SECS_PER_HOUR
SECS_PER_WEEK = DAYS_PER_WEEK * SECS_PER_DAY
SECS_PER_MONTH_AVG = DAYS_PER_MONTH_AVG * SECS_PER_DAY
SECS_PER_YEAR_AVG = DAYS_PER_YEAR_AVG * SECS_PER_DAY

#
# End of timeconst.py
=====

-- 
 \        "The Bermuda Triangle got tired of warm weather. It moved to |
  `\            Alaska. Now Santa Claus is missing."  -- Steven Wright |
_o__)                                                                  |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list