Exercise

in #ita2 days ago

using System;
using System.Globalization;
using System.Runtime.InteropServices;

public enum Location{
NewYork,
London,
Paris
}

public enum AlertLevel{
Early,
Standard,
Late
}

public static class Appointment{
private static bool isWindows = OperatingSystem.IsWindows();
public static DateTime ShowLocalTime(DateTime dtUtc) => dtUtc.ToLocalTime();

public static DateTime Schedule(string appointmentDateDescription, Location location){
            DateTime localDateTimeIn = DateTime.Parse(appointmentDateDescription);
            var timeZoneID = GetTimeZoneID(location);
            var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneID);
            return TimeZoneInfo.ConvertTimeToUtc(localDateTimeIn, timeZone);
}
    

private static string GetTimeZoneID(Location location) => location switch {
    Location.NewYork => isWindows ? "Eastern Standard Time" : "America/New_York",
    Location.London => isWindows ? "GMT Standard Time" : "Europe/London",
    Location.Paris => isWindows ? "W. Europe Standard Time" : "Europe/Paris",
    _ => throw new ArgumentOutOfRangeException(),
};

        

public static DateTime GetAlertTime(DateTime appointment, AlertLevel alertLevel){
    if(alertLevel == AlertLevel.Early){
        return appointment.AddDays(-1);
    }
    else if(alertLevel == AlertLevel.Standard){
        return appointment.AddHours(-1).AddMinutes(-45);
    }
    return appointment.AddMinutes(-30);
}

public static bool HasDaylightSavingChanged(DateTime dt, Location location){
   var timeZoneID = GetTimeZoneID(location);
   var timeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneID);
   return timeZone.IsDaylightSavingTime(dt.AddDays(-7)) != timeZone.IsDaylightSavingTime(dt);
}

public static DateTime NormalizeDateTime(string dtStr, Location location){
    CultureInfo culture = CultureInfo.CreateSpecificCulture(location switch{
            Location.NewYork =>"en-US",
            Location.London => "en-GB",
            Location.Paris => "fr-FR",
            _ => throw new ArgumentException()
    });
        return DateTime.TryParse(dtStr, culture, DateTimeStyles.None, out DateTime localDateTimeIn)? localDateTimeIn : new DateTime();
        
}

}