Skip to content

Instantly share code, notes, and snippets.

@EricsonWillians
Created June 15, 2015 06:34
Show Gist options
  • Save EricsonWillians/c882b896bb723b8f5137 to your computer and use it in GitHub Desktop.
Save EricsonWillians/c882b896bb723b8f5137 to your computer and use it in GitHub Desktop.
The Time object provides a simple and handy way to do basic operations with time in the hh:mm:ss format.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# time_object.py
#
# Copyright 2015 Ericson Willians (Rederick Deathwill) <EricsonWRP@ERICSONWRP-PC>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import datetime
class Time():
"""
The Time object provides a simple and handy way to do basic operations with time in the hh:mm:ss format.
"""
def __init__(self, h, m, s):
self.h = h
self.m = m
self.s = s
self.datetime = datetime.datetime(100, 1, 1, self.h, self.m, self.s)
def __add__(self, t):
if isinstance(t, tuple):
return str((self.datetime + datetime.timedelta(hours=t[0], minutes=t[1], seconds=t[2])).time())
elif isinstance(t, Time):
return str((self.datetime + datetime.timedelta(hours=t.h, minutes=t.m, seconds=t.s)).time())
def __sub__(self, t):
if isinstance(t, tuple):
return str((self.datetime - datetime.timedelta(hours=t[0], minutes=t[1], seconds=t[2])).time())
elif isinstance(t, Time):
return str((self.datetime - datetime.timedelta(hours=t.h, minutes=t.m, seconds=t.s)).time())
def __eq__(self, t):
if isinstance(t, tuple):
return self.datetime == datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime == t.datetime
def __ne__(self, t):
if isinstance(t, tuple):
return self.datetime != datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime != t.datetime
def __lt__(self, t):
if isinstance(t, tuple):
return self.datetime < datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime < t.datetime
def __gt__(self, t):
if isinstance(t, tuple):
return self.datetime > datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime > t.datetime
def __le__(self, t):
if isinstance(t, tuple):
return self.datetime <= datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime <= t.datetime
def __ge__(self, t):
if isinstance(t, tuple):
return self.datetime >= datetime.datetime(100, 1, 1, t[0], t[1], t[2])
elif isinstance(t, Time):
return self.datetime >= t.datetime
def __getitem__(self, key):
if key == 0 or key == 'h':
return self.h
elif key == 1 or key == 'm':
return self.m
elif key == 2 or key == 's':
return self.s
else:
raise Exception("The index must be 0, 1 or 2.")
def __str__(self):
return str(self.datetime.time())
def main():
# Examples:
x = Time(6, 30, 0)
y = Time(3, 43, 0)
# Arithmetic:
print(x + (1, 2, 0)) # <str> Adding a tuple in the (hh, mm, ss) format to a Time object.
print(x + y) # <str> Adding Time objects.
print(x - (1, 2, 0)) # <str> Subtracting a tuple in the (hh, mm, ss) format to a Time object.
print(x - y) # <str> Subtracting Time objects.
# Comparisons:
print(x == y) # <bool>
print(x == (6, 30, 0)) # <bool>
print(x != y) # <bool>
print(x > y) # <bool>
print(x < y) # <bool>
print(x >= y) # <bool>
print(x <= y) # <bool>
# Other:
print(x) # <str> Outputting the Time object.
print(x[0]) # <int> Hour access by index.
print(x[1]) # <int> Minute access by index.
print(x[2]) # <int> Second access by index.
return 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment