Skip to main content

C Function to Validate ISO 8601 Date Formats Using ‘strptime’

Here’s a demonstration of how to use strptime and a list of format strings to validate, for example, a supplied ISO 8601 date in C or C++. You can play with the code below over at It’s not an extensive list of all ISO 8601 dates, but these are the ones that work within a MySQL query. One improvement that could be made is to handle timezones in datetime strings like ’2010-01-01T01:01:01-7:00′ and potentially micro seconds (if possible).

This is also a good demonstration for how to easily loop through all elements in an array of undefined size in C.

#define _GNU_SOURCE
#include <stdio.h>
#include <time.h>
#include <string.h>

static int
is_valid_iso8601_date_value(char *in)
    struct tm result;
    char **f;
    char *ret;
    char *formats[] = {
        "%Y-%m-%d %T",
        "%y-%m-%d %T",
        "%Y-%m-%d %TZ",
        "%y-%m-%d %TZ",
        "%Y%m%d %TZ",
        "%y%m%d %TZ",

    memset(&result, 0, sizeof(result));

    for (f = formats; f && *f; f++)
        ret = strptime(in, *f, &result);
        if (ret && *ret == '')
            return 1;

    return 0;

int main(void) {
	char date_str[] = "2010-01-01T01:01:01Z";
    if (is_valid_iso8601_date_value(date_str))
    	printf("%s is a valid iso8601 date!", date_str);
        return 0;
    	printf("%s is not a valid iso8601 date!", date_str);
		return 1;

Using ‘shopt’ To Adjust Bash Terminal Number Columns After Resizing Window

This solved a long time frustration I had within PuTTY in that when changing the window size, the terminal columns get all messed up and you get some pretty strange behavior (as seen in this blog post). The solution is to use shopt with the checkwinsize option. This will make sure that your bash terminal will always have the correct number of columns.

shopt -s checkwinsize

After throwing this into my ~/.bashrc file (or ~/.bash_profile, if you prefer), my frustration is gone! Whew!

Check out the man page or this page for more information.

Use CDPATH to Quickly Change Directories

You can create a shortcut to frequently accessed directories by adding them to the CDPATH environment variable.  So, say I frequently access /var/www/html/.  Instead of typing cd /var/www/html, I can add /var/www/ to CDPATH and then I only have to type cd html.

Open ~/.bashrc (or ~/.bash_profile) and add the following line with your frequently used directories separated with a colon (similar to PATH variable).

export CDPATH=$CDPATH:/var/www/

Here’s an example usage:

dhildreth@hostname:~> export CDPATH=$CDPATH:/var/www/
dhildreth@hostname:~> cd html

There’s one caveat to using this that I’ve ran into in the past: if you are working with Makefiles and building c/c++ apps, this can potentially confuse the Makefile script. So, if you suddenly can’t build your project after adding this variable, try removing it.

A Loving Copyright Notice (With Interesting Date)

I just came across this loving copyright notice in a file at work. For one thing, I enjoyed the content, but then it got me wondering if it was inspired by 9-11. The more I thought about it, the more I enjoyed the content.

** 2001 September 15
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.

Attn Subscribers: Google Reader Being Shutdown

Google announced March 13th that it’s going through some more spring cleaning which includes Google Reader (see and I’d like to remind my subscribers, of which 90% or so are using Reader or iGoogle, may need to resubscribe or transfer their subscription to iGoogle or another feed reader. You should be able to click the RSS Feed link and choose an RSS reader. Thanks for your continued support!

Tutorial: Installing Django on Shared Hosting Service Such as

This guide will walk you through how to install and run Django on a shared host such as Bluehost. Because I use Bluehost, I was able to verify the steps you see below, but you might need to modify some of the steps to work with your specific host. The whole thing should take less than 10 minutes. If I’ve done my job right, you should be able to copy/paste multiple lines to run all commands in a block as if it were a script. If you feel more comfortable, you can run each command line by line. Let’s get to it…

Step 1: Installing Python 2.7

This step is for those of you who have an older installation of Python on your webhost. In the case of Bluehost (as of 12/6/2012), the version that is installed is 2.6.6. If you have version 2.7.0 and up (but not including version 3.0), then you can skip this step. Otherwise, follow along (you should be able to copy/paste this entire block into the terminal to run it all at once):

cd ~
mkdir python27
tar xf Python-2.7.3.tgz
cd Python-2.7.3/
./configure -prefix=$HOME/python27/ --enable-unicode=ucs4
make && make install
mv ~/python27/bin/python ~/python27/bin/python27
echo "PATH=$PATH:$HOME/python27/bin" >> ~/.bashrc
echo "export LC_ALL=en_US.UTF-8" >> ~/.bashrc
echo "export LANG=en_US.UTF-8" >> ~/.bashrc
. ~/.bashrc

At this point, you should be able to run which python27 successfully like this:

# which python27

Step 2: Installing SetupTools and PIP

Now that Python 2.7 has been installed, we’ll need to install setuptools and pip (you should be able to copy/paste this entire block into the terminal to run it all at once):

tar xf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python27 install
cd ~
tar xf pip-1.1.tar.gz
cd pip-1.1
python27 install

At this point, you should be able to run which pip successfully like this:

# which pip

Step 3: Use pip to Install Modules

We’re going to install MySQL-python, flup, and Django using pip (Note: If you plan on using PosgreSQL, you’ll need to install psycopg2):

pip install Django MySQL-python flup #psycopg2

At this point, you should be able to run which successfully like this:

# which

Step 4: Setup an Environment for the Project

I’m going to name the project ‘myproject’. When you go to create your own, you’ll want to replace anything called ‘myproject’ with the project name of your choice (you should be able to copy/paste this entire block into the terminal to run it all at once):

mkdir ~/public_html/myproject
cd ~/public_html/myproject

cat > myproject.fcgi << EOF
import sys, os
project_name = "myproject"

# Add a custom Python path.
sys.path.insert(0, os.path.expanduser("~") + "/python27")
sys.path.insert(13, os.getcwd() + "/" + project_name)

os.environ['DJANGO_SETTINGS_MODULE'] = project_name + '.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

cat > .htaccess << EOF
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ myproject.fcgi/$1 [QSA,L]

chmod 0755 myproject.fcgi

Step 4: Create the Django Project

Lastly, we’re going to use django-admin to start our project called ‘myproject’ making sure to be in the correct directory first:

cd ~/public_html/myproject/ startproject myproject

Now, visit your website at http://mydomain/myproject and you should see the Django start page! Have fun with it.

A final note: If you want to use the admin pages, you’ll need to follow a few more steps to get the page to show up properly with css, js, and images (you’ll need to replace mydomainname with your actual domain name).

ln -s $HOME/python27/lib/python2.7/site-packages/django/contrib/admin/static $HOME/public_html/myproject/static
sed -i "s/^STATIC_ROOT = ''/STATIC_ROOT = 'admin'/g" $HOME/public_html/myproject/myproject/myproject/
sed -i "s/^STATIC_URL = ''/STATIC_URL = ''/g" $HOME/public_html/myproject/myproject/myproject/

Sources: Simply Argh Blog

Handy Terminal Keyboard Shortcuts

Put these into your “Terminal Guru” belt and be more productive!

Cursor Movement Control
Ctrl-a: Move cursor to the start of a line
Ctrl-e: Move cursor to the end of a line
Ctrl-Left/Right: Navigate word by word (may not work in all terminals)

Modify Text
Ctrl-w: Delete the whole word to the left of the cursor
Ctrl-k: Erase to end of line
Ctrl-u: Erase to beginning of line

Scrolling/Buffer Control
Shift-PageUp/PageDown: Scroll through current buffer
Ctrl-s: Pause terminal output (program will keep running)
Ctrl-q: Release terminal output (after being paused)
Ctrl-l: Clears the screen. Use this instead of the clear command.

Ctrl-r: Search the history (enter to run the command once found)

Bonus Tip: Use ‘!!’ command to run last command and ‘!com’ to run the last command starting with ‘com’.

Process Control
Ctrl-d: Exit
Ctrl-c: Kill the current process
Ctrl-z: Put the current process in the background (fg will restore it)

Are there any keyboard shortcuts that you can’t live without? Tell us about them in the comments below.

msmtp – a (fairly) simple mail submission program

As an oldtime Unix guy, I’ve always been used to having the BSD mail utility to hand, and a suitably configured mail system, so that I can script jobs to run and email the results back to me. I use mail as a sort of glorified syslog facility. With smaller single board Linux computers we don’t always want to install a full mail setup – resources often tend to be limited. A few years back I discovered msmtp

This utility is an smtp client that submits a file in standard mail format to a mail server. It can submit plain text email or use TLS/SSL etc. I use a couple of script wrappers to emulate, sort of, sendmail and the sending part of the BSD mail utility.

Of course to use msmtp you need a mail server to which you can submit email for delivery. My home server is my mail server, but you could use your ISP’s smtp server. Another problem is that msmtp just fails if it can’t connect to the mail server – it’s up to you to handle that and do something else with that precious message you can’t mail just now! My sample scripts do not deal with that situation.

This very simple script I call sendmail, and it will need customising for your setup…


# set these for your setup...

exec msmtp --host=$MailServer --domain=$Domain --from=$From $*

This is my simple script to emulate the simple parts of the send functionality in the BSD mail utility. It has many shortcomings, but it has served me well…

# A sort of shell replacment for the send functionality of
# the standard "mail" utility.
# mail [-s subject] recipient(s)

u=`id -un`

while [ $# -gt 0 ]; do
 case "$p" in
        if [ $# -gt 0 ]; then shift ; fi
        echo 1>&2 "Option "$p" not recognised."
        exit 1
        r="$r , $p"
if [ "$r" = "" ]; then
 echo 1>&2 "No Recipients."
 exit 1
if [ "$s" = "" ]; then
 printf "Subject: "
 read s

s="Subject: $s n"

(echo -e "From: ${u}@$d nTo: $r n$s nn"
 cat ) | sendmail -t $v

So if you have a job to run on the platform, then this will email the output to you…

my_job | mail -s “my_job output”

msmtp can be loaded from the package systems of most distributions, but I have had occasion to cross-compile the package for installing on a system without package management. I had only limited libraries on my cross compile system, and found that after downloading and extracting the sourcecode from sourceforge, I had to cross compile without some of the advanced features. I used

./configure –build=arm –disable-ssl –disable-gsasl –disable-nls

before doing the make to build the binaries. The resultant binary just submitted plain text email, but that was ok for my use – YMMV. The resultant binary, suitably stripped,is pretty lean.

I recently revisited using msmtp to pre-test a change to my ISP’s new smtp server, before committing the change to my mailserver’s sendmail setup. It can also be useful for testing security settings etc on mail submissions systems.


Kermit lives!

As a veteran of the IT industry I’ve seen software and OSes come and go, but there are some pieces of software that I seem to have used on a lot of platforms for a lot of years. One of the oldest of these is the file transfer software Kermit, from Columbia University –

I’ve used kermit in the early 80′s, on various CP/M systems, the very first IBM PCs and on the BBC Micro, and have been using it since on various early Unices, and other OSes that are now only memories in old fogy minds like mine. I even used hacked versions of kermit for building an email system between various computers joined by rs232 links, and then gatewayed out to the big wide world in the late 1980′s.

I used it extensively in the 90′s for automating and controlling transfers and connections over dial-up modems. It is still my console of choice when I need to hook up microcontrollers and single board computers with rs232. It’s configurability, features and programmability make it second to none.

However it has always erked me that it’s licensing prevented it being available in the standard repositories of the major Linux distributions. I have got used to downloading the source tar ball and compiling my own executables whenever I’ve needed kermit, and cursed silently that yum or apt-get would not simply do the job for me.

So imagine my joy when I belatedly discovered that Columbia University have cancelled the Kermit project and allowed it to be re-licensed and development continue at The new license is a Revised 3-Clause BSD License which will at last allow Kermit to join the Free Software Family as a full member – and about time too!

If you are not familiar with Kermit, and need to go beyond where minicom etc can take you, then do check it out. It might appear a bit old school but it is very, very powerfull.


Resuming stalled or held CUPS print jobs…

I have a network printer, and it’s surprising how often I print a job and then find the printer is not switched on, and the print job gets put in HOLD state. Using “lpstat -l” one can view the print job status, and with the “lp” command can resume the print job. But I’m idle, so I created this very simple script to do it all in one command – I call it “lprel”.

# script to resume the top job on the default printer queue

j=`lpstat | head -1 | cut -d' ' -f1`
if [ "$j" = "" ]; then
  echo 1>&2 "No jobs in queue."
  exit 1
echo "releasing "$j"..."
lp -i "$j" -H resume
lpstat -l