Module pyfdate
[hide private]
[frames] | no frames]

Module pyfdate

source code


@version: 0.7
@date:    2008-08-28
@status:  beta
@author:  Stephen Ferg
@contact: http://www.ferg.org/contact_info
@license: Creative Commons Attribution License 2.0 http://creativecommons.org/licenses/by/2.0/
@see:     http://www.ferg.org/pyfdate
@note:

ABOUT PYFDATE

The purpose of pyfdate is to provide a variety of user-friendly features
for working with datetimes, doing date arithmetic, and formatting dates,
times, and periods of time.

Pyfdate doesn't attempt to be all things to all people: rather, its goal
is to make it easy to do 95% if the things that people want to do with
dates and times.

Pyfdate provides two classes:

------------------------------------------------------------------------

Time:
        - A specific point in time, identified by
          year, month, day, hour, minute, second.
        (Python's datetime module calls this a "datetime".)

Period:
        - An amount of time, measured in days, hours, minutes, and seconds.

------------------------------------------------------------------------

In order to keep pyfdate simple, there are a number of things that it does not attempt to do.
        - Pyfdate doesn't know anything about timezones.
        - Pyfdate doesn't know anything about daylight savings time.
        - Pyfdate doesn't provide any facilities for parsing strings into date/times.
          (It does, however, provide a useful numsplit() function which performs a kind of parsing.)
        - The smallest unit of time that Pyfdate can handle is a second.
           It cannot be used to work with hundredths or thousandths of seconds.

INTERNATIONAL LANGUAGE SUPPORT

By default, pyfdate's language for displaying dates and times
(e.g. weekday names and month names) is American English.

If pyfdate can successfully import pyfdate_local,
the language for displaying dates and times
is taken from the pyfdate_local.py file.

Localization files are available for a number of languages.
For example: pyfdate_local_francais.py for French.

To use the file for language foobar,
simply copy pyfdate_local_foobar.py to pyfdate_local.py

And of course it is possible to customize pyfdate_local.py for any particular
language of your choice.  If you create (or correct) a localization file
for pyfdate, please share it with others by submitting it for inclusion
in the pyfdate distribution.


BACKGROUND

Many ideas for pyfdate functionality grew out of Steve Ferg's experiences with
an earlier (non-Python) program called "fdate", which provided datetime
arithmetic functionality for MS-DOS batch files. The name "pyfdate" is derived
from "Python" and "fdate".

Classes [hide private]
  Period
A pyfdate.Period is an amount of time.
  Time
A specific point in time, identified by year, month, day, hour, minute, second.
Functions [hide private]
string
argsToString(*args)
A utility routine for showing arguments in error messages.
source code
string
kwargsToString(**kwargs)
A utility routine for showing keyword arguments in error messages.
source code
int
CalendarMonthToNumericMonth(year, month)
Convert a calendar month (year,month) to a numeric representation:
source code
tuple
NumericMonthToCalendarMonth(months)
Convert a numeric representation of a month to a calendar month (year, month).
source code
boolean
isLeapYear(year)
Return True if year is a leapyear; otherwise return False.
source code
int
to24hour(hour, ampm)
Convert an hour expressed as am/pm into one expressed in 24 hour time.
source code
 
numsplit(s)
split a string into its numeric parts and return a list containing the numeric parts converted to ints.
source code
Variables [hide private]
  LANG = 'American English'
  TimeExpressedIn24Hours = False
  CivilTimeSeparator = ':'
  CivilDateFormat = 'm d, y'
  DAY = 'day'
  DAYS = 'days'
  HOUR = 'hour'
  HOURS = 'hours'
  MINUTE = 'minute'
  MINUTES = 'minutes'
  SECOND = 'second'
  SECONDS = 'seconds'
  WEEK = 'week'
  WEEKS = 'weeks'
  MONTH = 'month'
  MONTHS = 'months'
  YEAR = 'year'
  YEARS = 'years'
  MONTH_NAMES = {1: 'January', 2: 'February', 3: 'March', 4: 'Ap...
  WEEKDAY_NAMES = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4:...
  NEXT = 'NEXT'
  NEAREST = 'NEAREST'
  PREVIOUS = 'PREVIOUS'
  SECONDS_IN_MINUTE = 60
  MINUTES_IN_HOUR = 60
  HOURS_IN_DAY = 24
  SECONDS_IN_HOUR = 3600
  SECONDS_IN_DAY = 86400
  MINUTES_IN_DAY = 1440
  NORMAL_DAYS_IN_MONTH = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: ...
  APRIL = 4
  AUGUST = 8
  DECEMBER = 12
  FEBRUARY = 2
  FRIDAY = 5
  JANUARY = 1
  JULY = 7
  JUNE = 6
  MARCH = 3
  MAY = 5
  MONDAY = 1
  NOVEMBER = 11
  OCTOBER = 10
  SATURDAY = 6
  SEPTEMBER = 9
  SUNDAY = 7
  THURSDAY = 4
  TUESDAY = 2
  WEDNESDAY = 3
  __monthName = 'DECEMBER'
  __monthNumber = 12
  __package__ = None
  __weekdayName = 'SUNDAY'
  __weekdayNumber = 7
Function Details [hide private]

argsToString(*args)

source code 

A utility routine for showing arguments in error messages. Useful for debugging.

>>> from pyfdate import *
>>> s = argsToString("test",2,Time())
>>> print s
arg     1: "test" <type 'str'>
arg     2: 2 <type 'int'>
arg     3: 2008-01-01 14:40:18 <type 'instance'>
Returns: string

kwargsToString(**kwargs)

source code 

A utility routine for showing keyword arguments in error messages. Useful for debugging.

>>> from pyfdate import *
>>> s = kwargsToString(first="test", second=2, third=Time())
>>> print s
first     : "test" <type 'str'>
second    : 2 <type 'int'>
third     : 2008-01-01 14:36:38 <type 'instance'>
Returns: string

CalendarMonthToNumericMonth(year, month)

source code 

Convert a calendar month (year,month) to a numeric representation:

>>> CalendarMonthToNumericMonth(2007,4)
24075
Returns: int

NumericMonthToCalendarMonth(months)

source code 

Convert a numeric representation of a month to a calendar month (year, month).

>>> NumericMonthToCalendarMonth(24075)
(2007, 4)
Returns: tuple

isLeapYear(year)

source code 

Return True if year is a leapyear; otherwise return False.

>>> isLeapYear(2004)
True
>>> isLeapYear(2000)
True
>>> isLeapYear(2005)
False
Returns: boolean
True if year is a leapyear; otherwise return False.

to24hour(hour, ampm)

source code 

Convert an hour expressed as am/pm into one expressed in 24 hour time.

>>> to24hour(12,"am")
0
>>> to24hour(1,"am")
1
>>> to24hour(12,"PM")
12
>>> to24hour(1,"PM")
13
Parameters:
  • hour (int) - must be an integer (or a string that can be converted to an integer) in the range of 1 to 12
  • ampm (string) - must be a string containing either "am" or "pm" (in upper or lower case)
Returns: int
the number of of an hour in 24-hour (aka "military") time.

numsplit(s)

source code 

split a string into its numeric parts and return a list containing the numeric parts converted to ints.

This function can be used to parse a string containing an ISO datetime.

>>> from pyfdate import *
>>> numsplit("2007_10_09")
[2007, 10, 9]
>>> numsplit("2007-10-09T23:45:59")
[2007, 10, 9, 23, 45, 59]
>>> numsplit("2007/10/09 23.45.59")
[2007, 10, 9, 23, 45, 59]

Variables Details [hide private]

MONTH_NAMES

Value:
{1: 'January',
 2: 'February',
 3: 'March',
 4: 'April',
 5: 'May',
 6: 'June',
 7: 'July',
 8: 'August',
...

WEEKDAY_NAMES

Value:
{1: 'Monday',
 2: 'Tuesday',
 3: 'Wednesday',
 4: 'Thursday',
 5: 'Friday',
 6: 'Saturday',
 7: 'Sunday'}

NORMAL_DAYS_IN_MONTH

Value:
{1: 31,
 2: 28,
 3: 31,
 4: 30,
 5: 31,
 6: 30,
 7: 31,
 8: 31,
...