# HG changeset patch # User Paul Boddie # Date 1454884520 -3600 # Node ID ed312d199bb75ff53a37cbeb31b091ddd93e02e3 # Parent e55229d8df6d809be921b20c8a2d4a22110d0be3 Added support for duration formatting. diff -r e55229d8df6d -r ed312d199bb7 imiptools/dates.py --- a/imiptools/dates.py Sun Jan 31 17:18:54 2016 +0100 +++ b/imiptools/dates.py Sun Feb 07 23:35:20 2016 +0100 @@ -3,7 +3,7 @@ """ Date processing functions. -Copyright (C) 2014, 2015 Paul Boddie +Copyright (C) 2014, 2015, 2016 Paul Boddie 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 @@ -89,6 +89,19 @@ else: return None +def format_duration(td): + + "Format the timedelta 'td' as an iCalendar-compatible string." + + if not td: + return None + else: + day_portion = td.days and "%dD" % td.days or "" + time_portion = td.seconds and "T%dS" % td.seconds or "" + if not day_portion and not time_portion: + time_portion = "T0S" + return "P%s%s" % (day_portion, time_portion) + # Parsing of datetime and related information. def get_datetime(value, attr=None):