.Net Core 相当于 $(date +%s%N)
.Net Core equivalent to $(date +%s%N)
当我 运行 这个 Linux shell 命令 $(date +%s%N)
我得到: 1628718908367130365
我希望能够在我的 .Net Core 3.1 应用程序中获得这个值。
这是我尝试过的,它给了我什么:
DateTime.Now.Ticks
: 637642942785036026
DateTimeOffset.Now.ToUnixTimeMilliseconds()
: 1628719160465
但其中 none 个是相同的。 Ticks
选项在精度上似乎最接近,但并不相同。
是否可以使用 .Net Core 获取此日期值?
您的 linux 命令为您提供纳秒精度。一个刻度为 100 纳秒。 (参见:https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=net-5.0#remarks)。
如果您真的需要纳秒精度,我认为标准库不可能实现(参见:Is there a high resolution (microsecond, nanosecond) DateTime object available for the CLR?)。
您真的需要纳秒级的精度,还是只需要生成具有纳秒级值的结构?如果是后者,您可以乘以 100,就像 Microsoft 示例中的那样:
DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;
long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
Console.WriteLine("Elapsed from the beginning of the century to {0:f}:",
currentDate);
Console.WriteLine(" {0:N0} nanoseconds", elapsedTicks * 100);
Console.WriteLine(" {0:N0} ticks", elapsedTicks);
Console.WriteLine(" {0:N2} seconds", elapsedSpan.TotalSeconds);
Console.WriteLine(" {0:N2} minutes", elapsedSpan.TotalMinutes);
Console.WriteLine(" {0:N0} days, {1} hours, {2} minutes, {3} seconds",
elapsedSpan.Days, elapsedSpan.Hours,
elapsedSpan.Minutes, elapsedSpan.Seconds);
// This example displays an output similar to the following:
//
// Elapsed from the beginning of the century to Thursday, 14 November 2019 18:21:
// 595,448,498,171,000,000 nanoseconds
// 5,954,484,981,710,000 ticks
// 595,448,498.17 seconds
// 9,924,141.64 minutes
// 6,891 days, 18 hours, 21 minutes, 38 seconds
此外,刻度以 2000 年 1 月 1 日为起点,而 Unix 时间以 1970 年 1 月 1 日为起点。你必须考虑到这一点。举例说明:
using System;
public class Program
{
public static void Main()
{
// provided Unix string = "1628718908367130365";
// the above, converted via https://www.unixtimestamp.com/index.php, returns the following:
// Wed Aug 11 2021 21:55:08 GMT+0000 (to the seconds), plus:
// milliseconds: 367
// microseconds: 130
// nanoseconds: 365
Console.WriteLine($"provided unix timestamp: 1628718908367130365");
var testDateTime = new DateTime(2021, 08, 11, 21, 55, 08, 367); // millisecond precision
var testDto = new DateTimeOffset(testDateTime);
var asUnixTime = testDto.ToUnixTimeMilliseconds();
Console.WriteLine($"unix time with milli precision: {asUnixTime}");
Console.WriteLine($"unix time with nano (fake) precision: {asUnixTime * 1000 * 1000}");
var asTicks = testDateTime.Ticks;
Console.WriteLine($"ticks since microsoft epoch: {asTicks}");
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = testDateTime - unixEpoch;
Console.WriteLine($"ticks since unix epoch: {span.Ticks}");
Console.WriteLine($"times 100 for unix-like format: {span.Ticks * 100}");
}
}
输出:
provided unix timestamp: 1628718908367130365
unix time with milli precision: 1628718908367
unix time with nano (fake) precision: 1628718908367000000
ticks since microsoft epoch: 637643157083670000
ticks since unix epoch: 16287189083670000
times 100 for unix-like format: 1628718908367000000
阅读 this blog 和 Jonathan 的回答后。我可以为您找到更好的解决方案。
但是我想到了一个可行的方案,execute linux command in .net core
,也许对你有用
当我 运行 这个 Linux shell 命令 $(date +%s%N)
我得到: 1628718908367130365
我希望能够在我的 .Net Core 3.1 应用程序中获得这个值。
这是我尝试过的,它给了我什么:
DateTime.Now.Ticks
: 637642942785036026DateTimeOffset.Now.ToUnixTimeMilliseconds()
: 1628719160465
但其中 none 个是相同的。 Ticks
选项在精度上似乎最接近,但并不相同。
是否可以使用 .Net Core 获取此日期值?
您的 linux 命令为您提供纳秒精度。一个刻度为 100 纳秒。 (参见:https://docs.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=net-5.0#remarks)。
如果您真的需要纳秒精度,我认为标准库不可能实现(参见:Is there a high resolution (microsecond, nanosecond) DateTime object available for the CLR?)。
您真的需要纳秒级的精度,还是只需要生成具有纳秒级值的结构?如果是后者,您可以乘以 100,就像 Microsoft 示例中的那样:
DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;
long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
Console.WriteLine("Elapsed from the beginning of the century to {0:f}:",
currentDate);
Console.WriteLine(" {0:N0} nanoseconds", elapsedTicks * 100);
Console.WriteLine(" {0:N0} ticks", elapsedTicks);
Console.WriteLine(" {0:N2} seconds", elapsedSpan.TotalSeconds);
Console.WriteLine(" {0:N2} minutes", elapsedSpan.TotalMinutes);
Console.WriteLine(" {0:N0} days, {1} hours, {2} minutes, {3} seconds",
elapsedSpan.Days, elapsedSpan.Hours,
elapsedSpan.Minutes, elapsedSpan.Seconds);
// This example displays an output similar to the following:
//
// Elapsed from the beginning of the century to Thursday, 14 November 2019 18:21:
// 595,448,498,171,000,000 nanoseconds
// 5,954,484,981,710,000 ticks
// 595,448,498.17 seconds
// 9,924,141.64 minutes
// 6,891 days, 18 hours, 21 minutes, 38 seconds
此外,刻度以 2000 年 1 月 1 日为起点,而 Unix 时间以 1970 年 1 月 1 日为起点。你必须考虑到这一点。举例说明:
using System;
public class Program
{
public static void Main()
{
// provided Unix string = "1628718908367130365";
// the above, converted via https://www.unixtimestamp.com/index.php, returns the following:
// Wed Aug 11 2021 21:55:08 GMT+0000 (to the seconds), plus:
// milliseconds: 367
// microseconds: 130
// nanoseconds: 365
Console.WriteLine($"provided unix timestamp: 1628718908367130365");
var testDateTime = new DateTime(2021, 08, 11, 21, 55, 08, 367); // millisecond precision
var testDto = new DateTimeOffset(testDateTime);
var asUnixTime = testDto.ToUnixTimeMilliseconds();
Console.WriteLine($"unix time with milli precision: {asUnixTime}");
Console.WriteLine($"unix time with nano (fake) precision: {asUnixTime * 1000 * 1000}");
var asTicks = testDateTime.Ticks;
Console.WriteLine($"ticks since microsoft epoch: {asTicks}");
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan span = testDateTime - unixEpoch;
Console.WriteLine($"ticks since unix epoch: {span.Ticks}");
Console.WriteLine($"times 100 for unix-like format: {span.Ticks * 100}");
}
}
输出:
provided unix timestamp: 1628718908367130365
unix time with milli precision: 1628718908367
unix time with nano (fake) precision: 1628718908367000000
ticks since microsoft epoch: 637643157083670000
ticks since unix epoch: 16287189083670000
times 100 for unix-like format: 1628718908367000000
阅读 this blog 和 Jonathan 的回答后。我可以为您找到更好的解决方案。
但是我想到了一个可行的方案,execute linux command in .net core
,也许对你有用