From Unix Time - CSharp System

CSharp examples for System:DateTime Unix

Description

From Unix Time

Demo Code


using System.Globalization;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
using System;//  w ww .  j  a  v a 2s  . c  o  m

public class Main{
        [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "unixTime")]
        public static DateTime FromUnixTime(string unixTime)
        {
            if (string.IsNullOrEmpty(unixTime))
            {
                throw new ArgumentNullException("unixTime");
            }
            Contract.EndContractBlock();

            long seconds;

            if (!long.TryParse(unixTime, out seconds) || seconds < 0)
            {
                throw new FormatException("The unix time provided was not in the correct format.");
            }

            DateTime epoch = BaseUtcDateTime;
            long max = Int64.Parse(ToUnixTime(DateTime.MaxValue));

            return seconds < max ? epoch.AddSeconds(seconds) : DateTime.MaxValue;
        }
}

Related Tutorials