To Date Time from dmtf date : DateTimeFormatInfo « Date Time « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Date Time » DateTimeFormatInfoScreenshots 
To Date Time from dmtf date
      

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NetworkAssetManager.WMI
{
    class WMIUtil
    {

        public static DateTime ToDateTime(string dmtfDate)
        {
            DateTime initializer = DateTime.MinValue;
            int year = initializer.Year;
            int month = initializer.Month;
            int day = initializer.Day;
            int hour = initializer.Hour;
            int minute = initializer.Minute;
            int second = initializer.Second;
            long ticks = 0;
            string dmtf = dmtfDate;
            DateTime datetime = DateTime.MinValue;
            string tempString = string.Empty;
            if ((dmtf == null))
            {
                return DateTime.MinValue;
            }
            if ((dmtf.Length == 0))
            {
                return DateTime.MinValue;
            }
            if ((dmtf.Length != 25))
            {
                //return DateTime.MinValue;
            }
            try
            {
                tempString = dmtf.Substring(04);
                if (("****" != tempString))
                {
                    year = int.Parse(tempString);
                }
                tempString = dmtf.Substring(42);
                if (("**" != tempString))
                {
                    month = int.Parse(tempString);
                }
                tempString = dmtf.Substring(62);
                if (("**" != tempString))
                {
                    day = int.Parse(tempString);
                }
                tempString = dmtf.Substring(82);
                if (("**" != tempString))
                {
                    hour = int.Parse(tempString);
                }
                tempString = dmtf.Substring(102);
                if (("**" != tempString))
                {
                    minute = int.Parse(tempString);
                }
                tempString = dmtf.Substring(122);
                if (("**" != tempString))
                {
                    second = int.Parse(tempString);
                }
                tempString = dmtf.Substring(156);
                if (("******" != tempString))
                {
                    ticks = (long.Parse(tempString((long)((TimeSpan.TicksPerMillisecond / 1000))));
                }
                if (((((((((year < 0)
                            || (month < 0))
                            || (day < 0))
                            || (hour < 0))
                            || (minute < 0))
                            || (minute < 0))
                            || (second < 0))
                            || (ticks < 0)))
                {
                    return DateTime.MinValue;
                }
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
            datetime = new DateTime(year, month, day, hour, minute, second, 0);
            datetime = datetime.AddTicks(ticks);
            TimeSpan tickOffset = TimeZone.CurrentTimeZone.GetUtcOffset(datetime);
            int UTCOffset = 0;
            int OffsetToBeAdjusted = 0;
            long OffsetMins = ((long)((tickOffset.Ticks / TimeSpan.TicksPerMinute)));
            tempString = dmtf.Substring(223);
            if ((tempString != "******"))
            {
                tempString = dmtf.Substring(214);
                try
                {
                    UTCOffset = int.Parse(tempString);
                }
                catch (Exception)
                {
//HACK: (2009/08/06) Sometimes the UTC info in not proper so set UTC to 0 in such cases 
                    UTCOffset = 0
                    //return DateTime.MinValue;
                }
                OffsetToBeAdjusted = ((int)((OffsetMins - UTCOffset)));
                datetime = datetime.AddMinutes(((double)(OffsetToBeAdjusted)));
            }
            return datetime;
        }
    }
}

   
    
    
    
    
    
  
Related examples in the same category
1.CultureInfo Provides information about a specific culture
2.Determines the specific cultures that use the Chinese language, and displays the parent culture
3.Gets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.
4.Gets the list of calendars that can be used by the culture.
5.Custom Date and Time Format Strings
6.DateTimeFormatInfo Class
7.DateTimeFormatInfo.AbbreviatedMonthGenitiveNames
8.DateTimeFormatInfo.FullDateTimePattern
9.ALL the patterns
10.DateTimeFormatInfo.LongDatePattern
11.DateTimeFormatInfo.LongTimePattern
12.DateTimeFormatInfo.MonthDayPattern
13.DateTimeFormatInfo.RFC1123Pattern
14.DateTimeFormatInfo.ShortDatePattern
15.DateTimeFormatInfo.ShortTimePattern
16.DateTimeFormatInfo.SortableDateTimePattern
17.DateTimeFormatInfo.UniversalSortableDateTimePattern
18.DateTimeFormatInfo.YearMonthPattern
19.To Short Date Time
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.