/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
* 版权所有 (C)阿里云计算有限公司
*/
using System;
using System.Diagnostics;
using System.Globalization;
#pragma warning disable 0618
namespace Aliyun.Api.LOG.Common.Utilities
{
///
/// Description of DateUtils.
///
public static class DateUtils
{
private static DateTime _1970StartDateTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
private const string _rfc822DateFormat = "ddd, dd MMM yyyy HH:mm:ss \\G\\M\\T";
private const string _iso8601DateFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z'";
///
/// Formats an instance of to a GMT string.
///
/// The date time to format.
///
public static string FormatRfc822Date(DateTime dt)
{
return dt.ToUniversalTime().ToString(_rfc822DateFormat,
CultureInfo.InvariantCulture);
}
///
/// Formats a GMT date string to an object of .
///
///
///
public static DateTime ParseRfc822Date(String dt)
{
Debug.Assert(!string.IsNullOrEmpty(dt));
return DateTime.SpecifyKind(
DateTime.ParseExact(dt,
_rfc822DateFormat,
CultureInfo.InvariantCulture),
DateTimeKind.Utc);
}
///
/// Formats a date to a string in the format of ISO 8601 spec.
///
///
///
public static string FormatIso8601Date(DateTime dt)
{
return dt.ToUniversalTime().ToString(_iso8601DateFormat,
CultureInfo.CreateSpecificCulture("en-US"));
}
///
/// convert time stamp to DateTime.
///
/// seconds
///
public static DateTime GetDateTime(uint timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = ((long)timeStamp * System.TimeSpan.TicksPerSecond);
System.TimeSpan toNow = new System.TimeSpan(lTime);
DateTime targetDt = dtStart.Add(toNow);
return targetDt;
}
public static uint TimeSpan() {
return (uint)Math.Round((DateTime.Now - _1970StartDateTime).TotalSeconds, MidpointRounding.AwayFromZero);
}
}
}