Created
December 13, 2014 20:16
-
-
Save dselans/8909e743947fb93ca066 to your computer and use it in GitHub Desktop.
This file contains 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 characters
#!/usr/bin/env python | |
# | |
# Simple timeshift script | |
# | |
import re | |
import sys | |
import time | |
import operator | |
def shift_time(op, value, measure): | |
# Create operator map, to avoid some if/else's | |
op_map = {'+' : operator.add, '-' : operator.sub} | |
multiplier = 1 | |
if measure == 'm': | |
multiplier = 60 | |
elif measure == 'h': | |
multiplier = 60*60 | |
elif measure == 'd': | |
multiplier = (60*60)*24 | |
elif measure == 'w': | |
multiplier = ((60*60)*24)*7 | |
elif measure == 'M': | |
multiplier = ((60*60)*24)*30 # egh.. | |
elif measure == 'y': | |
multiplier = ((60*60)*24)*365 | |
# Convert our value + measure to seconds | |
seconds = multiplier * int(value) | |
current_ts = int(time.time()) | |
return op_map[op](current_ts, seconds) | |
def usage(): | |
print "Usage: ./ts.py +|-VALUE[mhdwMy]" | |
sys.exit(1) | |
def main(): | |
if len(sys.argv) < 2: | |
usage() | |
time_input = sys.argv[1] | |
pattern = '(\+|\-)(\d+)([smhdMy])|now' | |
match = re.match(pattern, time_input) | |
if match is not None: | |
if match.group(0) == 'now': | |
d = int(time.time()) | |
else: | |
d = shift_time(match.group(1), match.group(2), match.group(3)) | |
print "Date: %s" % d | |
else: | |
usage() | |
sys.exit(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment