Skip to content

Instantly share code, notes, and snippets.

@rdooley
Last active November 28, 2018 21:45
Show Gist options
  • Save rdooley/66fd27fdb37f51ff19f06dbbcb22450c to your computer and use it in GitHub Desktop.
Save rdooley/66fd27fdb37f51ff19f06dbbcb22450c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
"""
Run all following args iff DST for the Eastern Timezone
is in the position passed in as the first argument
called in the form
run_if_dist_is on command args
XOR
run_if_dist_is off command args
"""
import subprocess
import sys
def is_dst(timezone="US/Eastern", date_override=None):
"""Determine whether or not Daylight Savings Time (DST)
is currently in effect"""
cmd = 'date'
if date_override:
cmd += ' --date="{0}"'.format(date_override)
cmd += ' +%Z'
out = subprocess.check_output(cmd,
env={'TZ': timezone},
stderr=subprocess.STDOUT,
shell=True)
# strip trailing newline
# ending with DT is probably good enough for Central CDT and Eastern EDT
return out.strip().endswith('DT')
def main(dst_flag, command_line_args):
should_be_dst = None
if dst_flag == "on":
should_be_dst = True
elif dst_flag == "off":
should_be_dst = False
else:
raise Exception("Invalid call to run_if_dst_is, first arg must be 'on' or 'off'")
if is_dst() == should_be_dst:
return subprocess.call(command_line_args)
if __name__ == "__main__":
sys.exit(main(sys.argv[1], sys.argv[2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment