Skip to content

Instantly share code, notes, and snippets.

@ixtel
Forked from bbengfort/clock.py
Created October 19, 2015 12:56
Show Gist options
  • Save ixtel/5ce024f63e656605f942 to your computer and use it in GitHub Desktop.
Save ixtel/5ce024f63e656605f942 to your computer and use it in GitHub Desktop.
A quick command line utility for printing out dates formatted as you'd like them. I use this utility to quickly print out JSON formatted strings, or strings for use in code documentation.
#!/usr/bin/env python
import sys
from datetime import datetime
from dateutil.tz import tzlocal
class Clock(object):
FORMATS = {
"code":"%a %b %d %H:%M:%S %Y %z",
"json":"%Y-%m-%dT%H:%M:%S.%fZ",
"cute":"%b %d, %Y",
}
@classmethod
def local_now(self):
return datetime.now(tzlocal())
@classmethod
def utc_now(self):
return datetime.utcnow()
def __init__(self, formats={}):
self.formats = self.FORMATS
self.formats.update(formats)
def _local_format(self, fmt):
return Clock.local_now().strftime(fmt)
def _utc_format(self, fmt):
return Clock.utc_now().strftime(fmt)
def get_stamp(self, name):
name = name.strip("-")
mname = name + "_stamp"
# Try to find method table first
if hasattr(self, mname):
method = getattr(self, mname)
return method()
# Try to use the format string with local timezone
if name in self.formats:
return self._local_format(self.formats[name])
return None
def print_stamp(self, name):
stamp = self.get_stamp(name)
if stamp:
print stamp
else:
print "No stamp format for name %s" % name
def help_stamp(self):
output = ["Prints a timestamp represented with a format.",
"",
"The formats are stored in a lookup table with names, that",
"you can pass to the function. For instance, if you pass the",
"following arguments, you'll get the following results:",
""]
for name in ('--code', '--json'):
output.append("\t%s: %s" % (name, self.get_stamp(name)))
output.append("")
output.append("The current formats are:")
output.append("")
for item in self.formats.items():
output.append("\t%s: \"%s\"" % item)
output.append("")
output.append("Note that the timezone will default to the system timezone unless")
output.append("The format requires a UTC or other timezone (like JSON)")
output.append("")
return "\n".join(output)
def json_stamp(self):
return self._utc_format(self.formats['json'])
if __name__ == "__main__":
args = sys.argv[1:]
clock = Clock()
for arg in args:
clock.print_stamp(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment