1 #!/usr/bin/env python 2 3 """ 4 Increment the hour in a datetime string, wrapping round within any given range. 5 """ 6 7 import sys 8 9 try: 10 dt_str = sys.argv[1] 11 first, last = (sys.argv[2:4] + [None, None])[:2] 12 except (IndexError, ValueError): 13 sys.exit(1) 14 15 if not dt_str: 16 sys.exit(1) 17 18 try: 19 date_str = dt_str[:9] 20 hour = int(dt_str[9:11]) 21 min_sec_str = dt_str[11:] 22 except ValueError: 23 sys.exit(1) 24 25 if not first: 26 first = 0 27 else: 28 first = int(first) 29 30 if not last: 31 last = 23 32 else: 33 last = int(last) 34 35 hour += 1 36 if hour > last: 37 hour = first 38 39 print '%s%02d%s' % (date_str, hour, min_sec_str) 40 41 # vim: tabstop=4 expandtab shiftwidth=4