在Msbuild中比较DateTime戳 [英] Comparing DateTime stamps in Msbuild
问题描述
我正在尝试比较msbuild中的两个日期时间戳.我已完成以下操作:
I am trying to compare two date times stamps in msbuild. I have done the following:
<CreateItem Include="@(Compile)->'@(Compile).cache'"
Condition="('%(Compile.ExcludeFromStyleCop)' != 'true') and ('%(Compile.ExcludeFromSourceAnalysis)' != 'true') and (@(Compile.ModifiedTime) > @(Compile.cache.ModifiedTime))">
<Output TaskParameter="Include" ItemName="StyleCopFiles"/>
</CreateItem>
但是,它将引发以下内容:
However, it throws the following:
错误MSB4086:尝试进行数值比较 评估为"@(Compile.ModifiedTime)"的"@(Compile.ModifiedTime)" 而不是数字,条件为('%(Compile.ExcludeFromStyleCop)' !='true')和('%(Compile.ExcludeFromSourceAnalysis)'!='true')和 (@(Compile.ModifiedTime)> @(Compile.cache.ModifiedTime))". [c:\ dev \ apt \ DotNetMvc \ src \ Apt.Lib.Data.Elmah \ Apt.Lib.Data.Elmah.csproj]
error MSB4086: A numeric comparison was attempted on "@(Compile.ModifiedTime)" that evaluates to "@(Compile.ModifiedTime)" instead of a number, in condition "('%(Compile.ExcludeFromStyleCop)' != 'true') and ('%(Compile.ExcludeFromSourceAnalysis)' != 'true') and (@(Compile.ModifiedTime) > @(Compile.cache.ModifiedTime))". [c:\dev\apt\DotNetMvc\src\Apt.Lib.Data.Elmah\Apt.Lib.Data.Elmah.csproj]
如何在msbuild中比较两个日期时间戳?
How does one compare two date time stamps in msbuild?
推荐答案
一种更简单的解决方案是使用属性函数访问Ticks属性将DateTime对象转换为整数.
A much simpler solution here is to convert the DateTime object into an integer using a property function to access the Ticks property.
对于命名文件:
$([System.IO.File]::GetLastWriteTime('SomeFile.bin').Ticks)
或来自项目元数据:
$([System.DateTime]::Parse('%(ItemGroup.ModifiedTime)').Ticks)
然后您可以使用如下条件表达式来比较项目:
You can then compare items using conditional expressions like the following:
Condition="($([System.DateTime]::Parse('%(ItemGroup.ModifiedTime)').Ticks) > $([System.IO.File]::GetLastWriteTime('SomeFile').Ticks)))"
对于这里的问题,我认为这应该可行:
In the case of the question here, I believe this should work:
Condition="($([System.DateTime]::Parse('%(Compile.ModifiedTime)').Ticks) > ($([System.DateTime]::Parse('%(Compile.cache.ModifiedTime)').Ticks)"
这篇关于在Msbuild中比较DateTime戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!