diff options
author | Hartmut Goebel <h.goebel@crazy-compilers.com> | 2019-01-19 21:38:50 +0100 |
---|---|---|
committer | Hartmut Goebel <h.goebel@crazy-compilers.com> | 2019-01-19 21:38:50 +0100 |
commit | 5759c1f7ceff3d1ff6da06cf86d129623ce0512e (patch) | |
tree | eefc3c7a3113a9cdeedd357034898cc216c3ef74 | |
parent | a755de67aece9b1d1e4564fb5fd3eeed5cc542fa (diff) |
Replace spaghetti code by loops.
-rw-r--r-- | gnunet/strings.py | 56 |
1 files changed, 15 insertions, 41 deletions
diff --git a/gnunet/strings.py b/gnunet/strings.py index c84e32c..1484495 100644 --- a/gnunet/strings.py +++ b/gnunet/strings.py @@ -53,48 +53,22 @@ def string_to_data(s): def absolute_time_to_string(t): return t.strftime("%a %b %d %H:%M:%S %Y") - def string_to_absolute_time(s): if s == "end of time": return None - try: - return datetime.datetime.strptime(s, "%a %b %d %H:%M:%S %Y") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%c") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Ec") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Y-%m-%d %H:%M") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%x") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Ex") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Y-%m-%d") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Y-%m") - except ValueError: - pass - try: - return datetime.datetime.strptime(s, "%Y") - except ValueError: - pass + for fmt in ( + "%a %b %d %H:%M:%S %Y", + "%c", + "%Ec", + "%Y-%m-%d %H:%M:%S", + "%Y-%m-%d %H:%M", + "%x", + "%Ex", + "%Y-%m-%d", + "%Y-%m", + "%Y"): + try: + return datetime.datetime.strptime(s, fmt) + except ValueError: + pass raise ValueError("%s is not a properly formatted time string" % s) |