Files
DepotDownloaderMod/SteamKit/Types/MicroTime.cs
Ryan Stecker d4b8b920f7 Renamed SteamLib namespace to SteamKit.
--HG--
extra : convert_revision : svn%3A946a0da7-ebce-4904-9acb-2f1e67aed693%4077
2010-09-15 21:07:18 +00:00

80 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace SteamKit
{
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
public class MicroTime : Serializable<MicroTime>, IEquatable<ulong>, IComparable<ulong>
{
public ulong Time;
public static MicroTime Now
{
get
{
return new MicroTime( MicroTime.GetMicroSecNow() );
}
}
public MicroTime()
: this( 0 )
{
}
public MicroTime( ulong time )
{
this.Time = time;
}
public static implicit operator ulong( MicroTime microTime )
{
return microTime.Time;
}
public static implicit operator MicroTime( ulong microTime )
{
return new MicroTime( microTime );
}
public DateTime ToDateTime()
{
return ( DateTime.MinValue + TimeSpan.FromTicks( ( long )Time * 10 ) );
}
public uint ToUnixTime()
{
return MicroTime.GetUnixTime( this.ToDateTime() );
}
public override string ToString()
{
return ToDateTime().ToString();
}
public bool Equals( ulong other )
{
return Time.Equals( other );
}
public int CompareTo( ulong other )
{
return Time.CompareTo( other );
}
static ulong GetMicroSecNow()
{
return 0xDCBFFEFF2BC000UL + ( ( ulong )GetUnixTime( DateTime.UtcNow ) * 1000000UL );
}
static uint GetUnixTime( DateTime dt )
{
TimeSpan ts = ( dt - new DateTime( 1970, 1, 1, 0, 0, 0 ) );
return ( uint )ts.TotalSeconds;
}
}
}