Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created July 2, 2013 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bbengfort/5912071 to your computer and use it in GitHub Desktop.
Save bbengfort/5912071 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)
@bbengfort
Copy link
Author

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. Simply add your formats as necessary then add clock.py to your path. You can then call it as follows:

$ clock.py --json 
2013-07-02T19:00:28.439402Z
$ clock.py --code
Tue Jul 02 15:00:47 2013 -0400

Adding your own formats will automatically add the option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment