The thoughts of G. Allen Morris III

ISO 8601 for Programmers

April 7th 2016

I came across a nice article on ISO 8601 and thought I would share it here and expand on it some.

8601

  • date: %Y-%m-%d or %F for recent implementations (example: 2013-12-31)
  • time: %H:%M:%S or %T for recent implementations (example: 10:10:10)

However time should always include a timezone. In the simple case, where the time is in UTC you can simply add a Z to the end of the time %TZ.

If you want to represent a particular point in time you can use %FT%T%z.

On a modern posix system you should see output similar to 2016-04-07T04:44:30+0200.

In Ruby you can execute puts Time.now().strftime("%FT%T%z").

In C/C++ you can compile:

#include <time.h>
#include <stdio.h>

int
main()
{
  time_t t;
  struct tm *tmp;
  char buffer[51];

  t = time(NULL);
  tmp = localtime(&t);

  strftime(buffer, sizeof(buffer), "%FT%T%z", tmp);
  printf("%s\n", buffer);
  return 0;
}

There is also a standard for Durations.