#!/usr/bin/python

"""
Time test and change script.

Many people are not able to set their pc clock correctly.
This script corrects the date tolerrantly.

For procmail use:

DATE=`formail -xDate:`
NDATE=`path/to/bin/testdate.py $DATE`

:0fwhi
* ! DATE ?? NDATE
| formail -i"Date: ${NDATE}"


Changelog:

 0.1 Inital release
 0.2 rewrite :)
"""

MAXFUTURE=60 # 1 hour
MAXPAST=60*24*2 # 2 days

# END

from string import *
import sys
import email.Utils
import time

arg = rstrip(join(sys.argv[1:]))
try:
	o =	email.Utils.mktime_tz(email.Utils.parsedate_tz(arg))
	now = int(time.time())
	if (o + (MAXPAST*60) < now) or (o - (MAXFUTURE*60) > now):
		print email.Utils.formatdate()	
	else:
		print arg
except:
	# generate a new date
	print email.Utils.formatdate()
