The Intellectual Wilderness There is nothing more useless than doing efficiently that which should not be done at all.

2013.04.20 13:46

Most common Bash date commands for timestamping

Filed under: Computing — Tags: , , , , — zxq9 @ 13:46

From time to time I get asked how to use the date command to generate a timestamp. Here is an idiot-friendly script you can post for reference in your team’s bin/ if you get interrupted about timestamp questions or have an aversion to typing phrases like “man date” (with or without a space).

All but the first one and last three produce filename-friendly strings.

A big thanks to the following folks for pointing out mistakes and suggesting useful format inclusions:

  • 2013-05: Rich for the reminder to include UTC and timezoned stamps.
  • 2017-10: “Hamilton and Meg” (haha!) for pointing out I had my 4 year example formats messed up and for prodding me to include a 2-year example.
  • 2018-01: Autumn Gray for suggesting that I add examples of including short and long days of the week.
  • 2020-09: rade noticed that I had a “Zulu” notation for UTC listed as ISO8601, which it is not, and provided a correction. I’ve specified the Z-notation UTC timestamp separately from the (now corrected) ISO8601 timestamp. Thanks, rade!
#! /bin/bash

# An overly obvious reference for most commonly requested bash timestamps
# Now all you Mac fags can stop pestering me.

cat << EOD
        Format/result           |       Command              |          Output
YYYY-MM-DD_hh:mm:ss             | date +%F_%T                | $(date +%F_%T)
YYYYMMDD_hhmmss                 | date +%Y%m%d_%H%M%S        | $(date +%Y%m%d_%H%M%S)
YYYYMMDD_hhmmss (UTC version)   | date --utc +%Y%m%d_%H%M%SZ | $(date --utc +%Y%m%d_%H%M%SZ)
YYYYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z      | $(date +%Y%m%d_%H%M%S%Z)
YYYYMMSShhmmss                  | date +%Y%m%d%H%M%S         | $(date +%Y%m%d%H%M%S)
YYYYMMSShhmmssnnnnnnnnn         | date +%Y%m%d%H%M%S%N       | $(date +%Y%m%d%H%M%S%N)
YYMMDD_hhmmss                   | date +%y%m%d_%H%M%S        | $(date +%y%m%d_%H%M%S)
Seconds since UNIX epoch:       | date +%s                   | $(date +%s)
Nanoseconds only:               | date +%N                   | $(date +%N)
Nanoseconds since UNIX epoch:   | date +%s%N                 | $(date +%s%N)
Z-notation UTC timestamp        | date --utc +%FT%TZ         | $(date --utc +%FT%TZ)
Z-notation UTC timestamp + ms   | date --utc +%FT%T.%3NZ     | $(date --utc +%FT%T.%3NZ)
ISO8601 UTC timestamp           | date --utc +%FT%T%Z        | $(date --utc +%FT%T%Z)
ISO8601 UTC timestamp + ms      | date --utc +%FT%T.%3N%Z    | $(date --utc +%FT%T.%3N%Z)
ISO8601 Local TZ timestamp      | date +%FT%T%Z              | $(date +%FT%T%Z)
YYYY-MM-DD (Short day)          | date +%F\(%a\)             | $(date +%F\(%a\))
YYYY-MM-DD (Long day)           | date +%F\(%A\)             | $(date +%F\(%A\))

If executed, it will produce the (obvious) output:

        Format/result           |       Command              |          Output
YYYY-MM-DD_hh:mm:ss             | date +%F_%T                | 2020-09-09_11:17:10
YYYYMMDD_hhmmss                 | date +%Y%m%d_%H%M%S        | 20200909_111710
YYYYMMDD_hhmmss (UTC version)   | date --utc +%Y%m%d_%H%M%SZ | 20200909_021710Z
YYYYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z      | 20200909_111710JST
YYYYMMSShhmmss                  | date +%Y%m%d%H%M%S         | 20200909111710
YYYYMMSShhmmssnnnnnnnnn         | date +%Y%m%d%H%M%S%N       | 20200909111710866549239
YYMMDD_hhmmss                   | date +%y%m%d_%H%M%S        | 200909_111710
Seconds since UNIX epoch:       | date +%s                   | 1599617830
Nanoseconds only:               | date +%N                   | 873291281
Nanoseconds since UNIX epoch:   | date +%s%N                 | 1599617830874893393
Z-notation UTC timestamp        | date --utc +%FT%TZ         | 2020-09-09T02:17:10Z
Z-notation UTC timestamp + ms   | date --utc +%FT%T.%3NZ     | 2020-09-09T02:17:10.878Z
ISO8601 UTC timestamp           | date --utc +%FT%T%Z        | 2020-09-09T02:17:10UTC
ISO8601 UTC timestamp + ms      | date --utc +%FT%T.%3N%Z    | 2020-09-09T02:17:10.881UTC
ISO8601 Local TZ timestamp      | date +%FT%T%Z              | 2020-09-09T11:17:10JST
YYYY-MM-DD (Short day)          | date +%F\(%a\)             | 2020-09-09(水)
YYYY-MM-DD (Long day)           | date +%F\(%A\)             | 2020-09-09(水曜日)

Note that the last two, short and long day-of-week are dependent on the environment variable LANG. After setting LANG=en_US we wind up with the following:

YYYY-MM-DD (Short day)          | date +%F\(%a\)             | 2020-09-09(Wed)
YYYY-MM-DD (Long day)           | date +%F\(%A\)             | 2020-09-09(Wednesday)

2012.11.19 14:37

ODFapper 0.04 for Django on Unix

Filed under: Computing — Tags: , , , , , — zxq9 @ 14:37

I’ve pulled the most common bits out of the Django views where I had rendered ODFs before and have the itty-bitty beginnings of an ODF handling/rendering library started now. “Library” is a bit of a stretch, since its only a few functions and a Bash script, but it abstracts the most mysterious/tedious parts of ODF handling already.

This is just a simple tarball right now. It unpacks like a Django app would, but only contains a copy of odfapper, and a templatetags/ But since we are doing template tag registration you need to include it in your INSTALLED_APPS in and add a new variable ODFAPPER_PATH which needs to be a string with the absolute path to /your/project/location/odfapper/odfapper. Importing into a (or wherever) that you want to render ODFs in is done with from odfapper.funcs import render_odf, and I go into a bit more detail in the README included in the tarball. At the moment this post and the stuff in README (which is just an expansion of internal notes) is all the documentation.

I’ve got a ton more work I’d like to do on this. If there is any interest I could put a GitHub repo up — but other (paying) work calls to which this isn’t central, so… let me know if this is a direction anyone wants to head in and I’ll keep it going. There are a bajillion tiny things that are not so hard to do that would make ODF handling enormously more intuitive.

Link to 0.04 archive: odfapper_django-0.04.bz2
Link to just the shell update: odfapper-0.04.bz2

It bears mentioning that this is tested against Django 1.4, but not 1.5 yet. Unless the template loader classes have changed a lot then this should still work fine anyway, though.

2012.11.17 00:37

Packing/Unpacking ODFs: Now Automated

Filed under: Computing — Tags: , , — zxq9 @ 00:37

I do lots of automated ODF rendering using framework data from AOW, Snap and Django. Its a pain to keep it straight, so I’m slowly extracting the common bits. The most annoying and easiest to distill is the basic process of guarding against clobbering something else in the filesystem, getting all the files out, reformatting the XML to be easier to edit, and later repacking it in a way that doesn’t make an office program tell you “This file is screwed up — I can fix it, but I hate you”.

Here is a script that handles the packing and unpacking of ODFs in a more friendly way than having to remember how all the time: ODFapper-0.01.bz2.

It takes very few options because it is written against my most common use case: unpacking to touch up a template, render the marked-up content.xml (and add images, or whatever) and then repack it to a new location from within a framework. In other words, ODFs can be used identically to HTML page rendering.

This is just a tiny part of what I do with ODFs, but it covers the most common bits (in particular, all that checking that a framework isn’t clobbering something in the filesystem), and answers the most frequent question I get from people who are curious about how I render ODFs: how to make them palatable to LibreOffice later.

I’ll eventually pull the rest of the render/repack ODF process out of my various programs that already do that and maybe make a for-real project of ODFapper. But that takes time, for now if you want an easy way to pack/unpack ODFs without screwing them up feel free to incorporate this script in whatever you’re doing. If you are a Bash wizard with suggestions, of course, I’m all ears…

Powered by WordPress