2014-08-28 77 views
0

我最近试图确定这个问题的答案。我在Flexera的网站上找到了这个主题的唯一帖子,就是这个旧的未回复的post

我想知道这个问题的答案,并入一个管理字符串翻译的工具。我已经发现了答案(我的同事和我花了我们一天中的更多时间试图找出答案),但我想我会在Stack Overflow上发布问题/答案,以防其他人搜索它。InstallShield的ISString表中的时间戳格式是什么?

回答

0

答案是时间戳是一个32位整数,不同位表示日期的不同部分。

下面是它的分解

Bits 1-5 : The Day of the Month [1-31] (end range could be 28-31 depending on month) 
Bits 6-9 : The Month [1-12] 
Bits 10-16: The Year after 1980 (only goes to year 2107) [0-127] 
Bits 17-21: (?) Seconds rounded to even (only 5 bits so can only contain 31 values) [0-30] 
Bits 22-27: Minutes [0-59] 
Bits 28-32: Hours from 12 AM [0-23] 

如果32位整数是它的评估,以默认的日期Dec/30/1899 12:00 AM

这里无效日期是一个例子:

-------BINARY-32-bit-Integer-----   | Decimal | Date String 
DOM Month Year Seconds*2 Min Hour |   |    
00111 0111 0010000 00001 010000 00000 | 999295488 | Jul/07/1996 12:16 AM 
7  7  16  1   16  0 

这里是一些C#代码,用于在DateTime和ISString时间戳的字符串表示之间进行转换(Small Disclaimer:该代码当前不处理无效时间夯输入):

private static int bitsPerDOM = 5; 
    private static int bitsPerMonth = 4; 
    private static int bitsPerYear = 7; 
    private static int bitsPerEvenSecond = 5; 
    private static int bitsPerMinute = 6; 
    private static int bitsPerHour = 5; 

    private static int startYear = 1980; 

    public static string getISTimestamp(DateTime date) 
    { 
     int[] shiftValues = { bitsPerDOM, bitsPerMonth, bitsPerYear, bitsPerEvenSecond, bitsPerMinute, bitsPerHour }; 
     int[] dateValues = { date.Day, date.Month, date.Year -startYear, date.Second/2, date.Minute, date.Hour }; 
     int shift = 32; 
     int dateInt = 0; 
     for (int i = 0; i < dateValues.Length; i++) 
     { 
      shift -= shiftValues[i]; 
      dateInt |= (dateValues[i] << shift); 
     } 

     return dateInt.ToString(); 
    } 

    public static DateTime getTimeFromISTimestampStr(string ISTimestampStr) 
    { 
     int timestampInt = Int32.Parse(ISTimestampStr); 
     int dom = getBits(timestampInt, 0, 4); 
     int month = getBits(timestampInt, 5, 8); 
     int year = startYear + getBits(timestampInt, 9, 15); 
     int seconds = getBits(timestampInt, 16, 20) * 2; 
     int minutes = getBits(timestampInt, 21, 26); 
     int hours = getBits(timestampInt, 27, 31);    
     return new DateTime(year, month, dom, hours, minutes, seconds); 
    } 

    private static int getBits(int n, int start, int end) 
    { 
     //Clear left bits by shifting 
     n <<= start;    
     n >>= 31 + start - end; //Shift to the right 
     return n; 
    }