Preprocessor Timestamp

前端 未结 3 887
时光说笑
时光说笑 2021-01-25 09:17

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

For instance: #define VERSION_EXPIRE __TIMESTAMP__

The reason for th

相关标签:
3条回答
  • 2021-01-25 09:25

    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];
    }
    
    0 讨论(0)
  • 2021-01-25 09:28

    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"
    
    0 讨论(0)
  • 2021-01-25 09:33

    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.

    0 讨论(0)
提交回复
热议问题