2013-04-02 17 views
5

Ich versuche, eine Zeitstruktur in einen FAT-Zeitstempel zu konvertieren. Mein Code sieht so aus:Unix-Zeitstempel zu FAT-Zeitstempel

unsigned long Fat(tm_struct pTime) 
{ 
    unsigned long FatTime = 0; 

    FatTime |= (pTime.seconds/2) >> 1; 
    FatTime |= (pTime.minutes) << 5; 
    FatTime |= (pTime.hours) << 11; 
    FatTime |= (pTime.days) << 16; 
    FatTime |= (pTime.months) << 21; 
    FatTime |= (pTime.years + 20) << 25; 

    return FatTime; 
} 

Hat jemand den richtigen Code?

+0

Was ist Ihr Problem? –

Antwort

10
The DOS date/time format is a bitmask: 

       24    16     8     0 
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ 
|Y|Y|Y|Y|Y|Y|Y|M| |M|M|M|D|D|D|D|D| |h|h|h|h|h|m|m|m| |m|m|m|s|s|s|s|s| 
+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+ 
\___________/\________/\_________/ \________/\____________/\_________/ 
    year  month  day  hour  minute  second 

The year is stored as an offset from 1980. 
Seconds are stored in two-second increments. 
(So if the "second" value is 15, it actually represents 30 seconds.) 

Ich weiß nicht, die tm_struct Sie verwenden aber wenn es http://www.cplusplus.com/reference/ctime/tm/ dann

unsigned long FatTime = ((pTime.tm_year - 80) << 25) | 
         (pTime.tm_mon << 21) | 
         (pTime.tm_mday << 16) | 
         (pTime.tm_hour << 11) | 
         (pTime.tm_min << 5) | 
         (pTime.tm_sec >> 1); 
+0

Einige Typumwandlungen sind notwendig, wenn 'sizeof (int)' 2 ist (was hier der Fall sein kann, wenn der Code für den DOS-Real-Modus ist). –

+1

Und es wäre auch besser, auf einen "unsigned" -Typ zu übertragen, da das Verschieben von "signierten" Typen nicht vollständig portabel ist. –

+0

Ist der FAT-Zeitstempel in UTC oder in der lokalen Zeitzone? – rustyx

1

Lefteris E gab fast richtige Antwort, aber hier ist ein kleiner Fehler

Sie 1 bis tm_mon hinzufügen sollte, weil tm struct behält Monat als Zahl von 0 bis 11 (struct tm), aber DOS Datum/Uhrzeit von 1 bis 12 (FileTimeToDosDateTime). so richtig ist

unsigned long FatTime = ((pTime.tm_year - 80) << 25) | 
    ((pTime.tm_mon+1) << 21) | 
    (pTime.tm_mday << 16) | 
    (pTime.tm_hour << 11) | 
    (pTime.tm_min << 5) | 
    (pTime.tm_sec >> 1);