Introduction
This post is the first in a series entitled LinqPAD script of the day in which we will be looking at some scripts I used from time to time to help me with a specific issue or question.
Keep in mind that some of these scripts might be silly, useless, geeky but hey, whatever helps right?
All the scripts will have a GitHub link provided so you can inspect, download and play with them. Also please note that some scripts might require nuget packages, so for those scripts, you will need either a developer license or higher; as an alternative, create a Visual Studio project and compile it in Visual Studio (or Visual Studio code if it works).
If you wish, you might even integrate the logic of a script into your own application if it satisfies one of your needs.
Now to the matter at hand; I have written a showcase about a tool called LinqPad which has been my de facto tool for running small snippets of code or just as a scratch pad.
Today, we will discuss a script I called WorldClock
(link can be found here).
Why Does This Script Exist?
In today’s day and age, we have friends and clients all over the world, and it sometimes gets difficult to keep track of what time it is where especially when taking daylight savings into account.
So this script lets me see in real time what is the clock of a specific client or friend, and if they are in daylight savings.
The code with its comments is as follows:
void Main()
{
var constantRun = true;
var dumpContainer = new DumpContainer().Dump();
{
var currentDateTimeOffset = DateTimeOffset.Now;
dumpContainer.Content = new [] {
AssociatedTime.Create("Friend 1", currentDateTimeOffset, "Pacific Standard Time"),
AssociatedTime.Create("Friend 1", currentDateTimeOffset, "Pacific Standard Time"),
AssociatedTime.Create("Client 1", currentDateTimeOffset, "Eastern Standard Time"),
AssociatedTime.Create("Friend 3", currentDateTimeOffset, "Eastern Standard Time"),
AssociatedTime.Create("Client 2", currentDateTimeOffset, "GMT Standard Time"),
AssociatedTime.Create("Client 3", currentDateTimeOffset, "Romance Standard Time"),
AssociatedTime.Create("Local Time", currentDateTimeOffset, "GTB Standard Time"),
};
Thread.Sleep(TimeSpan.FromSeconds(1));
} while (constantRun);
}
class AssociatedTime
{
public string Name { get; }
public DateTimeOffset Time { get; }
public bool IsDaylightSaving { get; }
private AssociatedTime(string name, DateTimeOffset currentDateTimeOffset,
string timezoneId)
{
Name = name;
TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById(timezoneId);
Time = TimeZoneInfo.ConvertTime(currentDateTimeOffset, timezone);
IsDaylightSaving = timezone.IsDaylightSavingTime(Time);
}
public static AssociatedTime Create(string name, DateTimeOffset currentDateTimeOffset,
string timeZoneId)
{
return new AssociatedTime(name, currentDateTimeOffset, timeZoneId);
}
}
It might be overkill to write a script just for that, but being a script I can tweak it to answer other questions, for example:
- If I want to set up a meeting 3 hours from now, I can just update line 7 to
var currentDateTimeOffset = DateTimeOffset.Now.AddHours(3)
, likewise - If I want the script to run only once, I can just turn the flag
constantRun
to false
on line 3 - If I only want the time to update every hour or minute, I just change the value of the
Thread.Sleep()
method call on line 17
Ok, so far so good, but how do you know someone or somewhere’s timezone id? It’s not like everyone knows how a specific timezone is identified, right?
Well, here’s a trick, if you know their location, like the city they are in or even just their current time, you can open up a new script with one line TimeZoneInfo.GetSystemTimeZones()
and from that list, you can narrow down what is the Id for their time zone, then you just use that Id when you create an AssociatedTime
object.
I hope you found this script and its explanation useful and I hope to see you in the next post.
Cheers!
CodeProject