#!/usr/bin/env python #**************************************************************************** # units.py, provides classes for angles, times and time spans # # Copyright (C) 2002, Douglas W. Bell # # This is free software; you can redistribute it and/or modify it under the # terms of the GNU General Public License, Version 2. This program is # distributed in the hope that it will be useful, but WITTHOUT ANY WARRANTY. #***************************************************************************** import time, math, sys, os.path class Angle: """Stores & prints angle values and normalizes to 0-359.9999""" def __init__(self, num=0): self.num = num self.normalize() def normalize(self): """Restore angle to 0-359.9999...""" self.num = self.num % 360 def __coerce__(self, other): """Conversion for math operators""" return (self.num, other) def angleStr(self): """Return string rounded to no dec plcs""" if self.num >= 0.5: return '%03.0f%s' % (self.num, chr(176)) return '%02.0f%s E' % (360 + self.num, chr(176)) class Time: """Stores, prints, normalizes time values""" def __init__(self, hour=0): self.hour = hour self.normalize() def normalize(self): """Restore hour to 0-23.9999...""" self.hour = self.hour % 24 def __coerce__(self, other): """Conversion for math operators""" return (self.hour, other) def hourMinute(self, is24hr=1): """Return tuple of hour and minute integer values""" intHour = int(self.hour) minute = (self.hour % 1) * 60 if minute >= 59.5: intHour += 1 minute = 0.0 if not is24hr: if intHour > 12: intHour -= 12 elif intHour == 0: intHour = 12 return (intHour, int(round(minute))) def isAM(self): """Return 1 if AM""" return self.hour < 12.0 def timeStr(self, is24hr=1, addAMPM=0): """Return time string based on 12 or 24 hour clock""" if is24hr: return '%02d:%02d' % self.hourMinute(is24hr) if addAMPM: if self.hour < 12: return '%2d:%02d AM' % self.hourMinute(is24hr) return '%2d:%02d PM' % self.hourMinute(is24hr) return '%2d:%02d' % self.hourMinute(is24hr) def current(self, local=1): """Set time to local or GMT current, return self""" if local: tm = time.localtime(time.time()) else: tm = time.gmtime(time.time()) self.hour = tm[3] + tm[4] / 60.0 + tm[5] / 3600.0 return self def toLocal(self): """Convert self from GMT to local time""" if time.daylight: self.hour -= time.altzone / 3600.0 else: self.hour -= time.timezone / 3600.0 self.normalize() def toGMT(self): """Convert self from local to GMT time""" if time.daylight: self.hour += time.altzone / 3600.0 else: self.hour += time.timezone / 3600.0 self.normalize() class TimeSpan: """Stores and print values for spans of time""" def __init__(self, hour=0): self.hour = hour def __coerce__(self, other): """Conversion for math operators""" return (self.hour, other) def hourMinute(self): """Return tuple of hour and minute integer values""" intHour = int(self.hour) minute = (self.hour % 1) * 60 if minute >= 59.5: intHour += 1 minute = 0.0 return (intHour, int(round(minute))) def timeStr(self): """Return time string""" return '%2d:%02d' % self.hourMinute()