Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / threads

LinqPAD Script of the Day: WorldClock

0.00/5 (No votes)
13 Nov 2018CPOL2 min read 1.8K  
LinqPAD Script of the Day: WorldClock

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:

JavaScript
void Main()
{
    var constantRun = true; // this is a flag that keeps the clock running to see 
                            // what time it is for everyone at the same time
    var dumpContainer = new DumpContainer().Dump(); // this is a LinqPad specific class 
                    // that allows for updates without refreshing the whole output do
                    // we use a do-while approach because if the *constantRun* flag is 
                    // set to false, we still get the output once.
    {
        var currentDateTimeOffset = DateTimeOffset.Now; // this is the time that 
                               // will be used to compute the clock in different regions
        dumpContainer.Content = new [] { // here we will create an array that holds 
                                         // all the locations we want to keep track of time wise.
                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)); // this sleep is used to throttle the loop 
                                               // so that we don't take up more resources than needed
    } while (constantRun); 
}

class AssociatedTime // this is a container class that will just hold the name of 
                     // a location we want to track, its time and if it's in daylight savings.
{
    public string Name { get; } // the name of the location, or friend
    public DateTimeOffset Time { get; } // the current time at that location
    public bool IsDaylightSaving { get; } // if that location is in daylight savings

    private AssociatedTime(string name, DateTimeOffset currentDateTimeOffset, 
            string timezoneId) // this constructor was made private so that I can use a static 
                               // factory method which I consider cleaner
    {
        Name = name;
        TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById(timezoneId); // here we will 
                                                // get the TimeZoneInfo based of the Id we have it
        Time = TimeZoneInfo.ConvertTime(currentDateTimeOffset, timezone); // here we convert 
                                                // the current passed-in time to the specific timezone
        IsDaylightSaving = timezone.IsDaylightSavingTime(Time); // here we interrogate if the 
                                                // current time is in daylight savings
    }

    public static AssociatedTime Create(string name, DateTimeOffset currentDateTimeOffset, 
                 string timeZoneId) // the factory method for creating a location
    {
        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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)