Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Exporting Time Zones - C#

RSS
Modified on Fri, Dec 29, 2017, 6:21 AM by Administrator Categorized as ·Net Framework
public static void ExportTimeZonesToDesktopFile()
{
    var timeZones = TimeZoneInfo.GetSystemTimeZones();

    var result = timeZones.Select(TimeZoneDto.FromDotNet).ToList();

    var contents = XmlHelpers.SerializeToXml(result);

    var targetFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop),
                                    "timezone.xml");

    File.WriteAllText(targetFile, contents);
}

public static DateTime TweakDateTimeForSqlSvr(DateTime input)
{
    var minDate = new DateTime(1900, 1, 1);

    if (input.Date != DateTime.MinValue.Date)
        return input;

    return minDate + input.TimeOfDay;
}

public class TimeZoneDto
{
    public static TimeZoneDto FromDotNet(System.TimeZoneInfo a)
    {
        var tz = new TimeZoneDto
        {
            BaseUtcOffset = (int)a.BaseUtcOffset.TotalMinutes,
            DaylightName = a.DaylightName,
            DisplayName = a.DisplayName,
            DotNetId = a.Id,
            StandardName = a.StandardName,
            SupportsDst = a.SupportsDaylightSavingTime,
            AdjustmentRules = a.GetAdjustmentRules().Select(AdjustmentRuleDto.FromDotNet).ToList()
        };

        return tz;
    }

    public int BaseUtcOffset { get; set; }
    public string DaylightName { get; set; }
    public string DisplayName { get; set; }
    public string DotNetId { get; set; }
    public string StandardName { get; set; }
    public bool SupportsDst { get; set; }
    public List<AdjustmentRuleDto> AdjustmentRules { get; set; }

}

public class AdjustmentRuleDto
{
    public static AdjustmentRuleDto FromDotNet(System.TimeZoneInfo.AdjustmentRule a)
    {
        var result = new AdjustmentRuleDto
        {
            DateEnd = TweakDateTimeForSqlSvr(a.DateEnd),
            DateStart = TweakDateTimeForSqlSvr(a.DateStart),
            DaylightDelta = (int)a.DaylightDelta.TotalMinutes,
            DstEnd = TransitionTimeDto.FromDotNet(a.DaylightTransitionEnd),
            DstStart = TransitionTimeDto.FromDotNet(a.DaylightTransitionStart)
        };

        return result;
    }

    public DateTime DateEnd { get; set; }
    public DateTime DateStart { get; set; }
    public int DaylightDelta { get; set; }
    public TransitionTimeDto DstEnd { get; set; }
    public TransitionTimeDto DstStart { get; set; }
}

public class TransitionTimeDto
{
    public static TransitionTimeDto FromDotNet(System.TimeZoneInfo.TransitionTime a)
    {
        var result = new TransitionTimeDto
        {
            Day = a.Day,
            DayOfWeek = a.DayOfWeek,
            IsFixedDateRule = a.IsFixedDateRule,
            Month = a.Month,
            TimeOfDay = a.TimeOfDay,
            Week = a.Week
        };

        return result;
    }

    public int Day { get; set; }
    public System.DayOfWeek DayOfWeek { get; set; }
    public bool IsFixedDateRule { get; set; }
    public int Month { get; set; }
    public DateTime TimeOfDay { get; set; }
    public int Week { get; set; }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.