用数字解码遗留数据库中的日期存储又名 "fun"

Decoding date storage in legacy database aka "fun" with numbers

我正在编写一个实用程序来从遗留数据库中删除记录(我们无法查询),但我在解释日期字段的存储方式时遇到了问题。

所有日期都将采用 MM/DD/YYYY 格式。十六进制将是由空格分隔的字节(2 位数字)。

我们知道的:

  1. 小时和分钟存储在不同的位置。增加一个小时或分钟 日期时间不影响有问题的 4 个字节
  2. 月日年对应的字段为4个字节:
    • 01/01/1800 == 70 8E 00 00
    • 01/15/1800 == 7E 8E 00 00
    • 01/16/1800 == 7F 8E 00 00
    • 01/31/1800 == 8E 8E 00 00
    • 02/01/1800 == 8F 8E 00 00
    • 02/02/1800 == 90 8E 00 00
    • 02/15/1800 == 9D 8E 00 00
    • 02/16/1800 == 9E 8E 00 00
    • 02/28/1800 == AA 8E 00 00
    • 02/29/1800 == AB 8E 00 00 #闰年占位符
    • 03/01/1800 == AC 8E 00 00
    • 12/01/1800 == BF 8F 00 00
    • 12/02/1800 == C0 8F 00 00
    • 12/03/1800 == C1 8F 00 00
    • 12/15/1800 == CD 8F 00 00
    • 12/16/1800 == CE 8F 00 00
    • 12/30/1800 == DC 8F 00 00
    • 12/31/1800 == DD 8F 00 00
    • 01/01/1801 == DE 8F 00 00
    • 12/31/1801 == 4A 91 00 00

有人有什么想法吗?是的,我熟悉纪元时间。

有4个字节。每个新的一天都会增加最左边的字节。一旦该字节到达 "FF" ,它就会将 1 添加到它右侧的字节。试试这个..(写在Ruby)

 def parse_date(hex)
   actual_known_date = "1/1/2050".to_date
   known_date = "21F30100"
   total_days_since_known_date = 0

   first_byte = hex[0,2]
   second_byte = hex[2,2]
   third_byte = hex[4,2]
   fourth_byte = hex[6,2]

   known_first_byte = known_date[0,2]
   known_second_byte = known_date[2,2]
   known_third_byte = known_date[4,2]
   known_fourth_byte = known_date[6,2]

   byte_4_days = known_fourth_byte.hex - fourth_byte.hex
   byte_3_days = 0
   byte_2_days = 0
   byte_1_days = 0

   if known_third_byte.hex >= third_byte.hex
     byte_3_days = known_third_byte.hex - third_byte.hex
   else
     byte_4_days -= 1
     ktb = known_third_byte.hex + 256
     byte_3_days = ktb - third_byte.hex
   end

   if known_second_byte.hex >= second_byte.hex
     byte_2_days = known_second_byte.hex - second_byte.hex
   else
     byte_3_days -= 1
     ktb = known_second_byte.hex + 256
     byte_2_days = ktb - second_byte.hex
   end

   if known_first_byte.hex >= first_byte.hex
     byte_1_days = known_first_byte.hex - first_byte.hex
   else
     byte_2_days -= 1
     ktb = known_first_byte.hex + 256
     byte_1_days = ktb - first_byte.hex
   end

   total_days_since_known_date = (byte_1_days + (byte_2_days * 256) + (byte_3_days * (256 * 256)) + (byte_4_days * (256 * 256 * 256)))

   number_of_leap_days = 0
   date_we_want = actual_known_date - (total_days_since_known_date).days
   return date_we_want
 end