-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (63 loc) · 2.73 KB
/
Program.cs
File metadata and controls
81 lines (63 loc) · 2.73 KB
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
using System;
using MSPro.CLArgs;
namespace CLArgs.Sample.ConvertToUtc
{
/// <summary>
/// This application converts a given datetime (incl. time-zone) into UTC.
/// The application does not supports *Verbs* or *Targets*, it simply uses *Options*.
/// </summary>
internal static class Program
{
/// <summary>
/// The test command-line for this example.
/// </summary>
private const string COMMAND_LINE = "--LocalDateTime=\"2020-08-01 08:10:00\" --LocalTimeZone=\"Pacific Standard Time\"";
/// <summary>
/// Parse the command-line args
/// and print Verbs and Options.
/// </summary>
private static void Main(string[] args)
{
Console.WriteLine(">>> Start Main");
args = Helper.SplitCommandLine( COMMAND_LINE);
// ------------------------------------------------
CommandLineArguments commandLineArguments = CommandLineParser.Parse(args);
Console.WriteLine($"Command-Line: {commandLineArguments.CommandLine}");
Commander.ExecuteCommand(args);
// ------------------------------------------------
Console.WriteLine("<<< End Main");
}
}
[Command("ConvertToUtc")]
class ConvertToUtcCommand : CommandBase<ConvertToUtcParameters>
{
protected override void Execute(ConvertToUtcParameters ps)
{
// Time Zone checking - inline: string to TimeZone
var localTimeZone= TimeZoneInfo.FindSystemTimeZoneById(ps.LocalTimeZone);
Console.WriteLine($"LocalDateTime={ps.LocalDateTime} "+
$"in TimeZone '{ps.LocalTimeZone}'");
DateTime utc = TimeZoneInfo.ConvertTimeToUtc( ps.LocalDateTime, localTimeZone);
Console.WriteLine($"is UTC: {utc}");
}
/// <summary>
/// OPTIONAL: Error handler to display errors instead of getting an Exception.
/// </summary>
/// <param name="errors"></param>
/// <param name="handled"></param>
protected override void OnError(ErrorDetailList errors, bool handled)
{
Console.WriteLine(errors.ToString());
base.OnError(errors, true);
}
}
class ConvertToUtcParameters
{
[OptionDescriptor("LocalDateTime", required:true,
helpText:"A local date and time that should be converted into UTC.")]
public DateTime LocalDateTime { get; set; }
[OptionDescriptor("LocalTimeZone", required:true,
helpText:"Specify the LocalDateTime's time zone")]
public string LocalTimeZone { get; set; }
}
}