我需要用UTC日期时间打包字符串,使用最小数量的字节/字符。我只需要精确到秒。使用.NET 4.0,最节省空间的方式是什么?Ticks看起来并不那么小。
所有的想法都值得欣赏。谢谢。
编辑:感谢Joel Coehoorn,打包/解包的动作是最好的。谢谢!以下是一些证据:
DateTimeOffset nowStamp = DateTimeOffset.UtcNow;
Console.WriteLine( nowStamp.ToString() ); // 9/9/2011 2:17:17 PM +00:00
Console.WriteLine( nowStamp.ToString( "u" ) ); // 2011-09-09 14:17:17Z
Console.WriteLine( nowStamp.Ticks.ToString() ); // 634511746376767889
Console.WriteLine( PackDate( nowStamp ) ); // 7R9qTgAAAAA=
Console.WriteLine( UnpackDate( PackDate( nowStamp ) ) ); // 9/9/2011 2:17:17 PM +00:00发布于 2011-09-09 21:19:07
也许是unix时间(自1970年1月1日以来的秒数,而不是毫秒) base64编码的变体。
//Helpers
private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long toUnixTime(this DateTime d)
{
return (long)((d.ToUniversalTime() - Jan1st1970).TotalMilliseconds);
}
public static string Base64Encode(long toEncode)
{
return Convert.ToBase64String(BitConverter.GetBytes(toEncode));
}
//Encode
public static string PackDate(DateTime toPack)
{
return Base64Encode(toPack.toUnixTime()/1000);
}
//Decode
public static DateTime UnpackDate(string toUnpack)
{
long time = BitConverter.ToInt64(Convert.FromBase64String(toUnpack),0);
return Jan1st1970.AddSeconds(time); //you may or may not want a "ToLocaltime()" call here.
}请注意,所有这些都是在没有IDE帮助的情况下完成的-上面可能有一两个bug。但这应该能让你开始。
这应该会产生一个固定宽度的字符串。因为我们只做秒,而不是毫秒,你可能会发现你在结果中总是有一些你不需要的额外填充。您甚至可以使用int而不是long,因为这样会将字符串一分为二。不过,要小心地去掉这个填充,因为你越接近1970,这个数字就越小,但是你得到的越远,你就越有可能需要它。您需要确保您的日期值将适合进行任何修剪的新的、较小的范围内。例如,当前日期很适合int,但即使是28年后也不能。UInt32将帮助您更深入地了解未来,但会阻止您使用1970年之前的日期。
发布于 2011-09-14 03:07:40
如果您重新布局需要节省一些字节,并且非常确定日期-时间界限,则此解决方案将会起作用:
internal class Program
{
private static DateTime _lbound = new DateTime(2011, 1, 1).ToUniversalTime();
private static DateTime _ubound = new DateTime(2013, 1, 1).ToUniversalTime();
private static int Pack(DateTime utcTime)
{
var totalSeconds = (_ubound - _lbound).TotalSeconds;
return (int) (utcTime - _lbound).TotalSeconds;
}
private static DateTime Unpack(int packedTime)
{
return _lbound.AddSeconds(packedTime);
}
private static void Check(DateTime time)
{
var unpacked = Unpack(Pack(time));
var areEquals = Math.Abs((time - unpacked).TotalSeconds) < 1.0;
Console.WriteLine("Verify: {0} - {1}", time, areEquals);
}
static void Main(string[] args)
{
Check(_lbound);
Check(_ubound);
Check(DateTime.UtcNow);
}
}它将适合时间表示,在定义的时间范围内(从2011到2013)具有1秒精度,以4字节(int)表示。然而,从维护的角度来看,这真的很糟糕。
https://stackoverflow.com/questions/7362174
复制相似问题