views:

63

answers:

3

Is it possible to generate an embedded Unix timestamp via a preprocessor macro?

For instance: #define VERSION_EXPIRE __TIMESTAMP__

The reason for this is, that i have beta versions and i want to generate an expire timestamp on compile time (in a special Debug build configuration).

Regards, Erik

A: 

Add a custom build step before preprocessing that uses sed to replace __TIMESTAMP__ with date. The easiest way to do this is to tell Xcode to use a makefile.

Jon Rodriguez
+1  A: 

I've solved it as follows:

#define VERSION_TIMESTAMP __DATE__" "__TIME__"\x0"

In some other class

+ (NSDate *)versionExpiresInDays:(NSUInteger)days {
    NSString *dateString = [NSString stringWithUTF8String:VERSION_TIMESTAMP];   
    NSLocale *enLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    NSDate *compiledOn = [NSDate dateWithNaturalLanguageString:dateString locale:enLocale];

    return [compiledOn dateByAddingTimeInterval:days*24*60*60];
}
Erik Aigner
The drawback with this is that you must not forget to clean the target every time, because otherwise the precompiled stuff won't change!
Erik Aigner
+2  A: 

If you'd use versioning software like svn or git you may have automatic replacement of strings like $Id: $ or $Date: $ by the id or date of the particular version of the file (svn) or of the "HEAD" release (git).

Edit: For git you can extract the sources

git archive --format=zip -9 -o project.zip HEAD file1 file2...

To replace certain strings during that process you have to tell git in .gitattributes that you want to have things substituted:

file*   export-subst

For the syntax of what and how terms between "$...$", please refer to the formats in the man page of git-log. Just as examples I have in my code

#define P99_VERSION_DATE "$Format:%cd$"
#define P99_VERSION_ID "$Format:%H$"

Which in the distribution version of the file is

#define P99_VERSION_DATE "Thu Oct 7 23:38:43 2010 +0200"
#define P99_VERSION_ID "6f9740a6808ff50f42d24bb7b5234a904f84e6fe"
Jens Gustedt
I had not realized this. Could you refer me to the documentation for this (i use git)?
Erik Aigner
@Erik: see my edit
Jens Gustedt