我为自己工作,我是一名自雇职业编码人员,因此我没有代码审查的豪华感或根据同行编程进行改进的能力。我将以此作为练习,以查看StackOverflow社区是否可以帮助检查我写的一个简单方法;重构速度:转换为日期
internal static DateTime CONVERT_To_DateTime(int binDate)
{
// 3/10/2008 = 1822556159
// 2/10/2008 = 1822523391
// 1/10/2008 = 1822490623
// 30/09/2008 = 1822392319
// 29/09/2008 = 1822359551
// September 30th 2008
// 1822392319 = 0x6c9f7fff
// 0x6c = 108 = 2008 (based on 1900 start date)
// 0x9 = 9 = September
// 0xf7fff - take top 5 bits = 0x1e = 30
// October 1st 2008
// 1822490623 = 0x6ca0ffff
// 0 x6c = 108 = 2008
// 0 xa = 10 = October
// 0x0ffff - take top 5 bits = 0x01 = 1
// OR using Binary (used by this function)
// a = 1822556159 (3/10/2008)
// 1101100 1010 00011 111111111111111
// b = 1822523391 (2/10/2008)
// 1101100 1010 00010 111111111111111
// c = 1822490623 (1/10/2008)
// 1101100 1010 00001 111111111111111
// D = 1822392319 (30/09/2008)
// 1101100 1001 11110 111111111111111
// Excess 111111 are probably used for time/seconds which
// we do not care for at the current time
var BaseYear = 1900;
// Dump the long date to binary
var strBinary = Convert.ToString(binDate);
// Calculate the year
var strBYear = strBinary.Substring(0, 7);
var iYear = Convert.ToInt32(strBYear, 2) + BaseYear;
// Calculate the month
var strBMonth = strBinary.Substring(7, 4);
var iMonth = Convert.ToInt32(strBMonth, 2);
// Calculate the day
var strBDay = strBinary.Substring(11, 5);
var iDay = Convert.ToInt32(strBDay, 2);
// ensure that month and day have two digits
var strDay = iDay < 10 ? "0" + iDay : iDay.ToString();
var strMonth = iMonth < 10 ? "0" + iMonth : iMonth.ToString();
// Build the final date
var convertedDate = iYear + strMonth + strDay;
return DateTime.ParseExact(convertedDate, "yyyyMMdd", null);
}
这是一种采用数字表示日期并将其转换为DateTime数据类型的方法。我希望对方法进行审查,以实现尽可能快的执行时间,因为它是在循环内执行的。
对这个方法的任何评论都很赞赏,因为这对我来说是一个练习。我期待着一些回应。
您应该总是用编程语言来标记这样的问题,以帮助人们找到它。 – unwind 2010-02-03 14:54:00
使用位掩码和位移方法可以更快地执行二进制操作 – tooleb 2010-02-03 15:02:27
仅供参考,省略二进制数字中的前导0可能会让其他开发人员感到困惑。期望全部32位被表示是公平的。 – 2010-02-03 15:30:36