Brain Teaser: You have 3 dates with 3 offsets, from 3 variables, how do you find the newest date, but without using IF?

C#
using System;
using System.Linq;

public class Program
{
public static void Main()
{
//Uncomment to get a list of timezones.
//foreach(var tz in TimeZoneInfo.GetSystemTimeZones()){
//    Console.WriteLine(tz.Id);
//}

//Alaska
//first, create base DateTime
DateTime dtAlaska = new DateTime(2015,4,15,12,00,00);

//second, use datet ime to calc timespan, or assume local and convert. We will get timespan.
TimeSpan tsAlaska = TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time")
                    .GetUtcOffset(dtAlaska);

//last, put it together
DateTimeOffset dtoAlaska = new DateTimeOffset(2015,4,15,12,00,00,tsAlaska);
Console.WriteLine(dtoAlaska);

//Pacific
DateTime dtPacific = new DateTime(2015,4,15,12,00,00);
TimeSpan tsPacific = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")
                     .GetUtcOffset(dtPacific);
DateTimeOffset dtoPacific = new DateTimeOffset(2015,4,15,12,00,00,tsPacific);
Console.WriteLine(dtoPacific);

//Eastern
DateTime dtEastern = new DateTime(2015,4,15,12,00,00);
TimeSpan tsEastern = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
                     .GetUtcOffset(dtEastern);
DateTimeOffset dtoEastern = new DateTimeOffset(2015,4,15,12,00,00,tsEastern);
Console.WriteLine(dtoEastern);

DateTimeOffset[] dtaCollection = new DateTimeOffset[]{dtoAlaska,dtoPacific,dtoEastern};
Console.WriteLine(dtaCollection);
DateTimeOffset dtoMax = dtaCollection.Max();
Console.WriteLine(dtoMax);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.