まとめ(実測結果)
- C#のDateTime型に1秒加算してTickの変化を見ると、1秒 = 1 * 1000 * 1000 * 10[Tick]
- C#のDateTime型に1日加算してTickの変化を見ると、1日 = 24L * 60 * 60 * 1000 * 1000 * 10[Tick]
(追記: TimeSpan型に、TicksPerSecond、TicksPerDayの値が既に定義されていました)
// 概要: Represents the number of ticks in 1 day. This field is constant.
public const long TicksPerDay = 864000000000;
// 概要: Represents the number of ticks in 1 hour. This field is constant.
public const long TicksPerHour = 36000000000;
// 概要: Represents the number of ticks in 1 millisecond. This field is constant.
public const long TicksPerMillisecond = 10000;
// 概要: Represents the number of ticks in 1 minute. This field is constant.
public const long TicksPerMinute = 600000000;
// 概要: Represents the number of ticks in 1 second.
public const long TicksPerSecond = 10000000;
テストコード
Program 2301-3 Tickは100ns単位.cs
using System;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
/* 結果:
// C#のDateTime型に1秒加算してTickの変化を見る
10,000,000[Tick/1s] = 10,000,000(計算値)
// C#のDateTime型に1日加算してTickの変化を見る
864,000,000,000[Tick/1D] = 864,000,000,000(計算値)
// 例えば、Tickで日数を算出。月によって1ヵ月の長さが当然違う。
26,784,000,000,000[Tick/1M 2023/01]
31[d(Tick)/1M 2023/01]
24,192,000,000,000[Tick/1M 2023/02]
28[d(Tick)/1M 2023/02]
26,784,000,000,000[Tick/1M 2024/01]
31[d(Tick)/1M 2024/01]
25,056,000,000,000[Tick/1M 2024/02]
29[d(Tick)/1M 2024/02]
*/
// C#のDateTime型に1秒加算してTickの変化を見る
var t1 = DateTime.Parse("2023/01/01");
var t2 = t1.AddSeconds(1);
var tick_1s = 1 * 1000 * 1000 * 10; // Tickは100ns単位
Console.WriteLine($"{t2.Ticks - t1.Ticks:#,0}[Tick/1s] = {tick_1s:#,0}(計算値)");
// C#のDateTime型に1日加算してTickの変化を見る
t2 = t1.AddDays(1);
var tick_1d = 24L * 60 * 60 * 1000 * 1000 * 10; // Tickは100ns単位
Console.WriteLine($"{t2.Ticks - t1.Ticks:#,0}[Tick/1D] = {tick_1d:#,0}(計算値)");
// 例えば、Tickで日数を算出。月によって1ヵ月の長さが当然違う。
t2 = t1.AddMonths(1);
Console.WriteLine($"{t2.Ticks - t1.Ticks:#,0}[Tick/1M 2023/01]");
Console.WriteLine($"{(t2.Ticks - t1.Ticks) / tick_1d:#,0}[d(Tick)/1M 2023/01]");
var t3 = t2.AddMonths(1);
Console.WriteLine($"{t3.Ticks - t2.Ticks:#,0}[Tick/1M 2023/02]");
Console.WriteLine($"{(t3.Ticks - t2.Ticks) / tick_1d:#,0}[d(Tick)/1M 2023/02]");
t1 = DateTime.Parse("2024/01/01");
t2 = t1.AddMonths(1);
Console.WriteLine($"{t2.Ticks - t1.Ticks:#,0}[Tick/1M 2024/01]");
Console.WriteLine($"{(t2.Ticks - t1.Ticks) / tick_1d:#,0}[d(Tick)/1M 2024/01]");
t3 = t2.AddMonths(1);
Console.WriteLine($"{t3.Ticks - t2.Ticks:#,0}[Tick/1M 2024/02]");
Console.WriteLine($"{(t3.Ticks - t2.Ticks) / tick_1d:#,0}[d(Tick)/1M 2024/02]");
}
}
}
環境
Microsoft Visual Studio Community 2019 Version 16.11.22
VisualStudio.16.Release
Microsoft .NET Framework
Version 4.8.04084