Embedding the build date into a Cocoa app

In Sandvox, we recently switched our build system over to automatically choosing the expiration date for betas (previously it was stored in a config file, and had to be updated by hand).

I thought this would make a useful topic for the blog. Always keen to avoid unnecessary duplication though, I first cast around for any existing write-ups on the subject and found How I Expire My iOS Beta App. It's quite an old post now, but nothing's changed and I consider it a decent reference. You should go read it.

From there, it turns out I have a few noteworthy tweaks in our code. Here's a condensed version:

The important differences:

  • Apple recommend the use of the US posix locale for consistent results cross-platform, and in case the US ever fix their date format.
  • I found setting the timezone to GMT was good to give a consistent result regardless of the timezone used to build or run the app. Ultimately leaving the timezone as the default doesn't upset things too badly if you only care about a rough date for expiration purposes. Apps which want something more precise should use the __TIME__ macro too.
  • Since __DATE__ is a string constant, sneaking an @ symbol right in front of it directly gives us an NSString without having to go via a C string.

© Mike Abdullah 2007-2015