简体   繁体   中英

Preprocessor Timestamp

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 build configuration).

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];
}

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"

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM