预处理器时间戳
是否可以通过预处理器宏生成嵌入式Unix时间戳?
例如: #define VERSION_EXPIRE __TIMESTAMP__
原因是,我有beta版本,我想在编译时生成一个过期时间戳(在一个特殊的构建配置中)。
我解决了它如下:
#define VERSION_TIMESTAMP __DATE__" "__TIME__"\x0"
在其他一些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]; }
如果您使用svn
或git
等版本软件,您可以通过文件的特定版本(svn)或“HEAD”版本的ID或日期自动替换$Id: $
或$Date: $
等字符串(GIT)。
编辑:对于git
您可以提取源
git archive --format=zip -9 -o project.zip HEAD file1 file2...
要在该过程中替换某些字符串,您必须告诉.gitattributes
中的git您想要替换的东西:
file* export-subst
有关“$ … $”之间的内容和方式的语法,请参阅git-log
手册页中的格式。 就像我的代码中的例子一样
#define P99_VERSION_DATE "$Format:%cd$" #define P99_VERSION_ID "$Format:%H$"
哪个在文件的分发版本中
#define P99_VERSION_DATE "Thu Oct 7 23:38:43 2010 +0200" #define P99_VERSION_ID "6f9740a6808ff50f42d24bb7b5234a904f84e6fe"
在使用sed将__TIMESTAMP__
替换为date
预处理之前添加自定义构建步骤。 最简单的方法是告诉Xcode使用makefile 。