Skip to content

Instantly share code, notes, and snippets.

@mjf
Created July 8, 2012 15:05
Show Gist options
  • Save mjf/3071307 to your computer and use it in GitHub Desktop.
Save mjf/3071307 to your computer and use it in GitHub Desktop.
WOM -- ISO8601 and ICU compliant week of a month
#! /bin/sh
# WOM -- ISO8601 and ICU compliant week of a month
# Copyright (C) 2012 Matous J. Fialka, <http://mjf.cz/>
# Released under the terms of The MIT License
if [ $# -ne 1 ] || [ "$1" != +%w -a "$1" != +%u ]
then
cat 1>&2 <<- EOT
Usage: wom +%[wu];
Where: "w" for weeks starting with Sunday,
"u" for weeks starting with Monday;
Environment:
WOM_TS - Unix timestamp (Epoch time);
EOT
exit 1
fi
WOM_TS=${WOM_TS:-`date +%s`}
# Print time in date(1) format according to the actual WOM_TS variable.
now()
{
date -d 1970-01-01\ 00:00:00\ $WOM_TS\ SEC "$*"
}
# Print the week of the month (from 0 to 5). The first week of the
# month is the first week that contains a Thursday. This is based
# on the ICU definition of week of month, and correlates to the ISO8601
# week of year definition. A day in the week before the week with the
# first Thursday will be week 0.
printf -- %d\\n $(( ( ( `now +%e` + 4 - `now $1` ) + 6 ) / 7 ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment