goebelmasse

Differenz in Tagen zwischen zwei Datumsangaben ermitteln.

Jun 10th, 2018
274
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # $Id: datediff,v 1.3 2013/09/14 14:37:31 elias Exp $
  3.  
  4. from datetime import date
  5. from re import match
  6. from sys import argv
  7.  
  8. opt_year_real = False
  9. opt_short_output = False
  10.  
  11. def twodigityear(y):
  12.     if y > 100 or opt_year_real:
  13.         return y
  14.     if y > 70:
  15.         y += 1900
  16.     else:
  17.         y += 2000
  18.     return y
  19.  
  20. def s2date(s):
  21.     mo = match(r'(\d{,2})\.(\d{,2})\.(\d+)$', s)
  22.     if mo:
  23.         year = twodigityear(int(mo.group(3)))
  24.         return date(year, int(mo.group(2)), int(mo.group(1)))
  25.     mo = match(r'(\d+)\-(\d{,2})\-(\d{,2})$', s)
  26.     if mo:
  27.         year = twodigityear(int(mo.group(1)))
  28.         return date(year, int(mo.group(2)), int(mo.group(3)))
  29.     raise RuntimeError, 'Unsupported date format'
  30.  
  31. argc = len(argv)
  32. if argc == 2:
  33.     d1 = date.today()
  34.     d2 = s2date(argv[1])
  35. elif argc == 3:
  36.     d1 = s2date(argv[1])
  37.     d2 = s2date(argv[2])
  38. else:
  39.     raise RuntimeError, 'Improper parameters'
  40.  
  41. dd = d2 - d1
  42. days = dd.days
  43. if opt_short_output:
  44.     print(days)
  45. else:
  46.     direction = 'after'
  47.     ds1 = d1.isoformat()
  48.     ds2 = d2.isoformat()
  49.     if days < 0:
  50.         direction = 'before'
  51.         days = abs(days)
  52.     if days > 1:
  53.         plural = 'days'
  54.     else:
  55.         plural = 'day'
  56.     print('%s is %d %s %s %s' % (ds2, days, plural, direction, ds1))
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×