-
-
Save jonlabelle/7d306575cbbd34b154f87b1853d532cc to your computer and use it in GitHub Desktop.
Revisions
-
jonlabelle revised this gist
Nov 23, 2019 . 1 changed file with 3 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ def __init__(self, dt): def format(self): for period in ['year', 'month', 'day', 'hour', 'minute', 'second']: n = getattr(self, period) if n >= 1: return '{0} ago'.format(formatn(n, period)) return "just now" @@ -52,8 +52,8 @@ def format(self): # EXAMPLES # just_now = relative_time(datetime.now()) # >>> just now ten_years_ago = relative_time(datetime(2008, 9, 1)) # >>> 11 years ago six_years_ago = relative_time(datetime.now() - timedelta(days=6)) # >>> 6 days ago twenty_minutes = relative_time(datetime.now() - timedelta(minutes=20)) # >>> 20 minutes ago @@ -62,6 +62,3 @@ def format(self): print('ten years ago.......: {0}'.format(ten_years_ago)) print('six years ago.......: {0}'.format(six_years_ago)) print('twenty minutes ago..: {0}'.format(twenty_minutes)) -
jonlabelle revised this gist
Oct 5, 2018 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,8 @@ def relative_time(date): be returned. Make sure date is not in the future, or else it won't work. Original Gist by 'zhangsen' @ https://gist.github.com/zhangsen/1199964 """ def formatn(n, s): -
jonlabelle revised this gist
Oct 5, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,7 @@ def format(self): # just_now = relative_time(datetime.now()) # >>> just now ago ten_years_ago = relative_time(datetime(2008, 9, 1)) # >>> 10 years ago six_years_ago = relative_time(datetime.now() - timedelta(days=6)) # >>> 6 days ago twenty_minutes = relative_time(datetime.now() - timedelta(minutes=20)) # >>> 20 minutes ago -
jonlabelle revised this gist
Oct 5, 2018 . 1 changed file with 35 additions and 24 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,54 +1,65 @@ from datetime import datetime, timedelta def relative_time(date): """Take a datetime and return its "age" as a string. The age can be in second, minute, hour, day, month or year. Only the biggest unit is considered, e.g. if it's 2 days and 3 hours, "2 days" will be returned. Make sure date is not in the future, or else it won't work. """ def formatn(n, s): """Add "s" if it's plural""" if n == 1: return "1 %s" % s elif n > 1: return "%d %ss" % (n, s) def qnr(a, b): """Return quotient and remaining""" return a / b, a % b class FormatDelta: def __init__(self, dt): now = datetime.now() delta = now - dt self.day = delta.days self.second = delta.seconds self.year, self.day = qnr(self.day, 365) self.month, self.day = qnr(self.day, 30) self.hour, self.second = qnr(self.second, 3600) self.minute, self.second = qnr(self.second, 60) def format(self): for period in ['year', 'month', 'day', 'hour', 'minute', 'second']: n = getattr(self, period) if n > 0: return '{0} ago'.format(formatn(n, period)) return "just now" return FormatDelta(date).format() # # EXAMPLES # just_now = relative_time(datetime.now()) # >>> just now ago ten_years_ago = relative_time(datetime(2008, 9, 1)) # >>> 10 years agp six_years_ago = relative_time(datetime.now() - timedelta(days=6)) # >>> 6 days ago twenty_minutes = relative_time(datetime.now() - timedelta(minutes=20)) # >>> 20 minutes ago print('') print('just now............: {0}'.format(just_now)) print('ten years ago.......: {0}'.format(ten_years_ago)) print('six years ago.......: {0}'.format(six_years_ago)) print('twenty minutes ago..: {0}'.format(twenty_minutes)) # import time # print(time.time()) # unix epoch time -
zhangsen revised this gist
Sep 7, 2011 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ # # This piece of code is in the public domain. # <[email protected]> # from datetime import datetime def get_age(date): -
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ from datetime import datetime def get_age(date): '''Take a datetime and return its "age" as a string. The age can be in second, minute, hour, day, month or year. Only the biggest unit is considered, e.g. if it's 2 days and 3 hours, "2 days" will be returned. Make sure date is not in the future, or else it won't work. ''' def formatn(n, s): '''Add "s" if it's plural''' if n == 1: return "1 %s" % s elif n > 1: return "%d %ss" % (n, s) def q_n_r(a, b): '''Return quotient and remaining''' return a / b, a % b class PrettyDelta: def __init__(self, dt): now = datetime.now() delta = now - dt self.day = delta.days self.second = delta.seconds self.year, self.day = q_n_r(self.day, 365) self.month, self.day = q_n_r(self.day, 30) self.hour, self.second = q_n_r(self.second, 3600) self.minute, self.second = q_n_r(self.second, 60) def format(self): for period in ['year', 'month', 'day', 'hour', 'minute', 'second']: n = getattr(self, period) if n > 0: return formatn(n, period) return "0 second" return PrettyDelta(date).format() # examples # get_age(datetime.now()) -> "0 second" # get_age(datetime(2001, 9, 1)) -> "10 years" # get_age(datetime(2011, 9, 1)) -> "6 days"