To Unix Epoch - CSharp System

CSharp examples for System:DateTime Unix

Description

To Unix Epoch

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
using System;/*  ww w.ja  v a2 s.c  o  m*/

public class Main{
        public static long ToUnixEpoch(this DateTime dateTime)
        {
            var utc = dateTime.ToUniversalTime();

            var from1970 = utc - _UnixEpochZero;

            var totalSeconds = from1970.TotalSeconds;

            var fullSeconds = Math.Floor(totalSeconds);
            if (fullSeconds > long.MaxValue) 
            {
                throw new ArgumentOutOfRangeException("dateTime", dateTime, "The value is too big.");
            }
            if (fullSeconds < long.MinValue)
            {
                throw new ArgumentOutOfRangeException("dateTime", dateTime, "The value is too small.");
            }

            return (long)fullSeconds;
        }
}

Related Tutorials