简体   繁体   English

如何获取 a.csproj 文件中两个日期之间的差异?

[英]How do I get the difference between two dates in a .csproj file?

I saw some code like this , in a csproj file我在csproj文件中看到了一些这样的代码

$([System.DateTime]::UtcNow.ToString(mmff))

to autoincrement the assembly version:自动增加程序集版本:

<VersionSuffix>2.0.0.$([System.DateTime]::UtcNow.ToString(mmff))</VersionSuffix>
<AssemblyVersion Condition=" '$(VersionSuffix)' == '' ">0.0.0.1</AssemblyVersion>

What kind of language/script is that?那是什么样的语言/脚本? How do I use it to get the difference between two dates?如何使用它来获取两个日期之间的差异?

I tried to do something like this:我试图做这样的事情:

<VersionMajor>2</VersionMajor>
<VersionMinor>1</VersionMinor>
<DaysFromLastRelease>$(([System.DateTime]::UtcNow - new [System.DateTime](2021,1,1))::TotalDays)</DaysFromLastRelease>

but it does not work:)但它不起作用:)

.csproj files are basically MSBuild files (XML). .csproj文件基本上是MSBuild文件 (XML)。 The embedded syntax you are referring to is called a Property Function .您所指的嵌入式语法称为Property Function

It appears that subtraction using the minus ( - ) might not be supported.似乎不支持使用减号 ( - ) 进行减法。 There is a Subtract() property function in Property Functions . Property Functions中有一个Subtract()属性 function 。

Perhaps this could be the base for a solution.也许这可能是解决方案的基础。 I have not tried it!我没试过!

<Now>$([System.DateTime]::UtcNow.DayOfYear)</Now>

<January>$([System.DateTime]::new(2021,1,1)).DayOfYear</January>
<!-- or... (not sure about the below)
<January>$([System.DateTime]::Parse("1/1/2021").DayOfYear)</January>
 -->

<DaysFromLastRelease>$([MSBuild]::Subtract($(Now), $(January)))</DaysFromLastRelease>

Other possibilities其他可能性

  • calculate the date difference by writing an MSBuild task通过编写 MSBuild 任务计算日期差
  • call out to a simple program you write调用你编写的一个简单程序
  • somehow use an external program to set an environment variable, and then reference that variable in your .csproj以某种方式使用外部程序设置环境变量,然后在.csproj中引用该变量

That's what worked for me:这对我有用:

<PropertyGroup>
    <VersionMajor Condition="'$(VersionMajor)' == ''">0</VersionMajor>
    <VersionMinor Condition="'$(VersionMinor)' == ''">0</VersionMinor>
    <VersionPatch Condition="'$(VersionPatch)' == ''">$([System.DateTime]::UtcNow.Subtract($([System.DateTime]::new(2001,1,1))).TotalDays.ToString("0"))</VersionPatch>
    <VersionRevision Condition="'$(VersionRevision)' == ''">$([System.DateTime]::UtcNow.TimeOfDay.TotalMinutes.ToString("0"))</VersionRevision>
    <Version>$(VersionMajor).$(VersionMinor).$(VersionPatch).$(VersionRevision)</Version>
</PropertyGroup>

Here I manually set VersionMajor and VersionMinor.这里我手动设置了VersionMajor 和VersionMinor。 Then I have autoincremented values for Patch and revision.然后我有补丁和修订的自动递增值。

  • Patch: number of days since 2001/01/01 (first day of century XXI).补丁:自 2001/01/01(二十一世纪的第一天)以来的天数。
  • Revision: total minutes of the day修订:一天中的总分钟数

That's good enought untill June 2180 (Remember máx versión number is 65534).直到 2180 年 6 月,这已经足够了(记住最大版本号是 65534)。


Extra tip: I put all this lines into a Version.Build.props file into Properties folder.额外提示:我将所有这些行放入一个 Version.Build.props 文件到 Properties 文件夹中。 Then I import it from csproj file with this tag:然后我用这个标签从 csproj 文件中导入它:

<Import Project="$([MSBuild]::GetPathOfFileAbove('Version.Build.props', '$(MSBuildThisFileDirectory)/Properties/'))" />

This way, I can manually set proyect versions manually in my csproj file, left them in auto or a mix of them by just setting VersionMajor and VersionMinor which is what I actually do.这样,我可以在我的 csproj 文件中手动设置 proyect 版本,通过设置 VersionMajor 和 VersionMinor 将它们保留为自动或混合它们,这就是我实际所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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