-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildTimestamp.cs
82 lines (68 loc) · 2.7 KB
/
BuildTimestamp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using Microsoft.CodeAnalysis;
namespace cmdwtf.BuildTimestampGenerator
{
[Generator]
internal class BuildTimestamp : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
DateTime executeTimeUtc = DateTime.UtcNow;
DateTimeOffset executeTimeUtcOffset = new(executeTimeUtc);
string source = $@"// Auto-generated code
using System;
using System.Diagnostics.CodeAnalysis;
namespace {nameof(cmdwtf)}
{{
/// <summary>
/// A static class representing when it was generated.
/// </summary>
[ExcludeFromCodeCoverage]
internal static class {nameof(BuildTimestamp)}
{{
/// <summary>
/// The timestamp (in Windows FILETIME, the number of 100ns intervals since
/// 12:00 midnight, January 1, 1601 C.E. UTC) from when this source was generated.
/// </summary>
public const long FileTime = {executeTimeUtc.ToFileTime()};
/// <summary>
/// The timestamp (in ticks elapsed since the beginning of the 21st century)
/// from when this source was generated.
/// </summary>
public const long Ticks = {executeTimeUtc.Ticks};
/// <summary>
/// The time this source was generated, as a <see cref=""DateTimeKind.Utc""/> <see cref=""DateTime""/>.
/// </summary>
public static DateTime BuildTimeUtc {{ get; }} = DateTime.FromFileTimeUtc(FileTime);
/// <summary>
/// The time this source was generated, as a <see cref=""DateTimeOffset""/>.
/// </summary>
public static DateTimeOffset BuildTimeDto {{ get; }} = new DateTimeOffset(BuildTimeUtc);
/// <summary>
/// The time this source was generated, as a <see cref=""DateTimeKind.Local""/> <see cref=""DateTime""/>.
/// </summary>
public static DateTime BuildTime {{ get; }} = BuildTimeDto.LocalDateTime;
/// <summary>
/// The time this source was generated as a Unix timestamp in UTC.
/// </summary>
public const long UnixTimeUtc = {executeTimeUtcOffset.ToUnixTimeSeconds()};
/// <summary>
/// The time this source was generated as a Unix timestamp in local time.
/// </summary>
public static long UnixTime {{ get; }} = BuildTimeDto.ToUnixTimeSeconds();
/// <summary>
/// The time this source was generated as a Unix millisecond timestamp in UTC.
/// </summary>
public const long UnixTimeMillisecondsUtc = {executeTimeUtcOffset.ToUnixTimeMilliseconds()};
/// <summary>
/// The time this source was generated as a Unix millisecond timestamp in local time.
/// </summary>
public static long UnixTimeMilliseconds {{ get; }} = BuildTimeDto.ToUnixTimeMilliseconds();
}}
}}
";
context.AddSource($"{nameof(BuildTimestamp)}.g.cs", source);
}
public void Initialize(GeneratorInitializationContext context) { }
}
}