#!/usr/bin/env python
# -*- mode:Python; tab-width: 3 -*-
"""
This is a little script to help outputting universal coordinated time
dates from files in different languages and different formatting.
Usually the output will be used in a sed s/... command or from
another python script using regular expressions. Possible
'public' calls from other scripts are localize_time and
localize_time_from_file.
This script is gift-ware, it's given to you freely as a gift.
You may use, modify, redistribute, and generally hack it about
in any way you like, and you do not have to give me anything in
return. However, if you like this script you are encouraged to thank
me emailing me your opinion about it, requests for more features,
or directly diffs which implement them. If you redistribute parts
of this script or use it somewhere successfully, it would be nice if
you mentioned me somewhere in the credits, but you are not required
to do this.
Of course, I do not accept responsibility for any effects, adverse
or otherwise, that this code may have on you, your computer, your
sanity, your dog and anything else that you can think of.
Use it at your own risk.
Written by Grzegorz Adam Hankiewicz <gradha@users.sourceforge.net>.
Download this and other things from http://gradha.sdf-eu.org/.
$Id: localize_time.py 7614 2006-12-02 18:13:42Z gradha $
"""
import time, os, stat, sys, string
default_format_string = "%(hour)02d:%(minute)02d, %(daynumber)d %(monthname)s %(year)d UTC"
def get_format_string (line):
"""
get_format_string (line) -> date format string
Pass a text line containing a format string and it will be returned.
If the line is empty or bad, default_format_string will be returned.
"""
if not line: return default_format_string
if string.find (line, "%(") < 0: return default_format_string
return line
def load_translation_text (localization_file):
"""
load_translation_text (localization_file) -> twelve item list
Internal helper for localize_time, opens a file and splits it's first line:
if twelve words are found, they are returned. If there's any problem, the
classical english month names are returned, all exceptions are catched.
"""
try:
file = open (localization_file, "rt")
test = string.split (file.readline(), None, 11)
format = get_format_string (string.rstrip (file.readline()))
file.close()
return test, format
except IOError:
return ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"], \
default_format_string
def localize_time (time_in_seconds = 0, format = default_format_string, localization_file = ""):
"""
localize_time (time_in_seconds, format, localization_file) -> string
Returns the format string filled with time information according to
time_in_seconds, which is ``time since the epoch'' (see python's
time module documentation. localization_file and format can be blank,
see load_translation_text's function documentation for information.
"""
months, new_format = load_translation_text (localization_file)
if new_format: format = new_format
assert len (months) == 12
assert format
tuple = time.gmtime (time_in_seconds)
dic = {"hour": tuple[3], "minute": tuple[4], "daynumber":tuple[2],
"monthname":months[tuple[1]-1], "year": tuple[0]}
return "%s" % (format % (dic))
def localize_time_from_file (filename, format = default_format_string, localization_file = ""):
"""
localize_time_from_file (filename, format, localization_file) -> string
Wrapper around localize_time: time_in_seconds is extracted from the
file's last modification time. localization_file can be blank, see
load_translation_text's function documentation for information.
"""
return localize_time (os.stat (filename)[stat.ST_MTIME], format, localization_file)
if __name__ == "__main__":
print "This doesn't do anything alone. Please see example.py."