Subtract 1 hour from the current time

While working on a project I had a need to subtract 1 hour from the current time. We are running Python 2 on linux and I could not quite find a function that did what I wanted.

#!/usr/bin/python
#----------------------------------------------------------------------
#
# Name       : subtract_hour.py
# Purpose    : Subtracts 1 hour from the current time on the remote server
#
# Jul 16, 2016 Dean Capps     Created new
#
#----------------------------------------------------------------------
#
import sys
import datetime

remote_time = sys.argv[1]

yy = int(remote_time[0:4])
mm = int(remote_time[4:6])
dd = int(remote_time[6:8])
hh = int(remote_time[8:10])
mi = int(remote_time[10:12])

remote_time_minus_1_hour=datetime.datetime(yy, mm, dd, hh, mi) - datetime.timedelta(hours=1)

yy = str(remote_time_minus_1_hour)[0:4]
mm = str(remote_time_minus_1_hour)[5:7]
dd = str(remote_time_minus_1_hour)[8:10]
hh = str(remote_time_minus_1_hour)[11:13]
mi = str(remote_time_minus_1_hour)[14:16]
remote_time_minus_1_hour = yy+mm+dd+hh+mi

print remote_time_minus_1_hour

 

Author: Dean Capps

Database consultant at Amazon Web Services.

142
51
73
77