MS-DOS date and time are packed 16-bit values that specify local date/time. The time is always stored in the current UTC time offset set on the computer which created the file. Note that the daylight saving time (DST) shifts also change the UTC time offset.
For example, if you pack two files A and B into a ZIP archive, file A last modified at 2020-03-29 00:59 UTC+00:00 (GMT) and file B at 2020-03-29 02:00 UTC+01:00 (BST), the file modification times saved in MS-DOS format in the ZIP file will vary depending on whether the computer packing the files is set to GMT or BST at the time of ZIP creation.
It follows that you are unable to determine the actual last modified time of any file stored in the ZIP archive, if you don't know the locale time setting of the computer at the time it created the ZIP.
This format is used in some data formats from the MS-DOS era, for example:
This page hosts a formal specification of MS-DOS datetime using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.
All parsing code for C++11/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.
For C++, the easiest way is to clone the runtime library sources and build them along with your project.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
std::istream
). One can open local file for that, or use existing std::string
or char*
buffer.
#include <fstream>
std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
#include <sstream>
std::istringstream is(str);
#include <sstream>
const char buf[] = { ... };
std::string str(buf, sizeof buf);
std::istringstream is(str);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
dos_datetime_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.time() // => get time
#pragma once
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <memory>
#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif
/**
* MS-DOS date and time are packed 16-bit values that specify local date/time.
* The time is always stored in the current UTC time offset set on the computer
* which created the file. Note that the daylight saving time (DST) shifts
* also change the UTC time offset.
*
* For example, if you pack two files A and B into a ZIP archive, file A last modified
* at 2020-03-29 00:59 UTC+00:00 (GMT) and file B at 2020-03-29 02:00 UTC+01:00 (BST),
* the file modification times saved in MS-DOS format in the ZIP file will vary depending
* on whether the computer packing the files is set to GMT or BST at the time of ZIP creation.
*
* - If set to GMT:
* - file A: 2020-03-29 00:59 (UTC+00:00)
* - file B: 2020-03-29 01:00 (UTC+00:00)
* - If set to BST:
* - file A: 2020-03-29 01:59 (UTC+01:00)
* - file B: 2020-03-29 02:00 (UTC+01:00)
*
* It follows that you are unable to determine the actual last modified time
* of any file stored in the ZIP archive, if you don't know the locale time
* setting of the computer at the time it created the ZIP.
*
* This format is used in some data formats from the MS-DOS era, for example:
*
* - [zip](/zip/)
* - [rar](/rar/)
* - [vfat](/vfat/) (FAT12)
* - [lzh](/lzh/)
* - [cab](http://justsolve.archiveteam.org/wiki/Cabinet)
* \sa https://learn.microsoft.com/en-us/windows/win32/sysinfo/ms-dos-date-and-time Source
* \sa https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-dosdatetimetofiletime Source
* \sa https://github.com/reactos/reactos/blob/c6b64448ce4/dll/win32/kernel32/client/time.c#L82-L87 DosDateTimeToFileTime
* \sa https://download.microsoft.com/download/0/8/4/084c452b-b772-4fe5-89bb-a0cbf082286a/fatgen103.doc page 25/34
*/
class dos_datetime_t : public kaitai::kstruct {
public:
class time_t;
class date_t;
dos_datetime_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, dos_datetime_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~dos_datetime_t();
class time_t : public kaitai::kstruct {
public:
time_t(kaitai::kstream* p__io, dos_datetime_t* p__parent = nullptr, dos_datetime_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~time_t();
private:
bool f_second;
int32_t m_second;
public:
int32_t second();
private:
bool f_padded_second;
std::string m_padded_second;
public:
std::string padded_second();
private:
bool f_padded_minute;
std::string m_padded_minute;
public:
std::string padded_minute();
private:
bool f_padded_hour;
std::string m_padded_hour;
public:
std::string padded_hour();
private:
uint64_t m_second_div_2;
uint64_t m_minute;
uint64_t m_hour;
dos_datetime_t* m__root;
dos_datetime_t* m__parent;
public:
uint64_t second_div_2() const { return m_second_div_2; }
uint64_t minute() const { return m_minute; }
uint64_t hour() const { return m_hour; }
dos_datetime_t* _root() const { return m__root; }
dos_datetime_t* _parent() const { return m__parent; }
};
class date_t : public kaitai::kstruct {
public:
date_t(kaitai::kstream* p__io, dos_datetime_t* p__parent = nullptr, dos_datetime_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~date_t();
private:
bool f_year;
int32_t m_year;
public:
/**
* only years from 1980 to 2107 (1980 + 127) can be represented
*/
int32_t year();
private:
bool f_padded_day;
std::string m_padded_day;
public:
std::string padded_day();
private:
bool f_padded_month;
std::string m_padded_month;
public:
std::string padded_month();
private:
bool f_padded_year;
std::string m_padded_year;
public:
std::string padded_year();
private:
uint64_t m_day;
uint64_t m_month;
uint64_t m_year_minus_1980;
dos_datetime_t* m__root;
dos_datetime_t* m__parent;
public:
uint64_t day() const { return m_day; }
uint64_t month() const { return m_month; }
uint64_t year_minus_1980() const { return m_year_minus_1980; }
dos_datetime_t* _root() const { return m__root; }
dos_datetime_t* _parent() const { return m__parent; }
};
private:
std::unique_ptr<time_t> m_time;
std::unique_ptr<date_t> m_date;
dos_datetime_t* m__root;
kaitai::kstruct* m__parent;
public:
time_t* time() const { return m_time.get(); }
date_t* date() const { return m_date.get(); }
dos_datetime_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
};
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "dos_datetime.h"
#include "kaitai/exceptions.h"
dos_datetime_t::dos_datetime_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, dos_datetime_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = this;
m_time = nullptr;
m_date = nullptr;
_read();
}
void dos_datetime_t::_read() {
m_time = std::unique_ptr<time_t>(new time_t(m__io, this, m__root));
m_date = std::unique_ptr<date_t>(new date_t(m__io, this, m__root));
}
dos_datetime_t::~dos_datetime_t() {
_clean_up();
}
void dos_datetime_t::_clean_up() {
}
dos_datetime_t::time_t::time_t(kaitai::kstream* p__io, dos_datetime_t* p__parent, dos_datetime_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
f_second = false;
f_padded_second = false;
f_padded_minute = false;
f_padded_hour = false;
_read();
}
void dos_datetime_t::time_t::_read() {
m_second_div_2 = m__io->read_bits_int_le(5);
if (!(second_div_2() <= 29)) {
throw kaitai::validation_greater_than_error<uint64_t>(29, second_div_2(), _io(), std::string("/types/time/seq/0"));
}
m_minute = m__io->read_bits_int_le(6);
if (!(minute() <= 59)) {
throw kaitai::validation_greater_than_error<uint64_t>(59, minute(), _io(), std::string("/types/time/seq/1"));
}
m_hour = m__io->read_bits_int_le(5);
if (!(hour() <= 23)) {
throw kaitai::validation_greater_than_error<uint64_t>(23, hour(), _io(), std::string("/types/time/seq/2"));
}
}
dos_datetime_t::time_t::~time_t() {
_clean_up();
}
void dos_datetime_t::time_t::_clean_up() {
}
int32_t dos_datetime_t::time_t::second() {
if (f_second)
return m_second;
m_second = (2 * second_div_2());
f_second = true;
return m_second;
}
std::string dos_datetime_t::time_t::padded_second() {
if (f_padded_second)
return m_padded_second;
m_padded_second = ((second() <= 9) ? (std::string("0")) : (std::string(""))) + kaitai::kstream::to_string(second());
f_padded_second = true;
return m_padded_second;
}
std::string dos_datetime_t::time_t::padded_minute() {
if (f_padded_minute)
return m_padded_minute;
m_padded_minute = ((minute() <= 9) ? (std::string("0")) : (std::string(""))) + kaitai::kstream::to_string(minute());
f_padded_minute = true;
return m_padded_minute;
}
std::string dos_datetime_t::time_t::padded_hour() {
if (f_padded_hour)
return m_padded_hour;
m_padded_hour = ((hour() <= 9) ? (std::string("0")) : (std::string(""))) + kaitai::kstream::to_string(hour());
f_padded_hour = true;
return m_padded_hour;
}
dos_datetime_t::date_t::date_t(kaitai::kstream* p__io, dos_datetime_t* p__parent, dos_datetime_t* p__root) : kaitai::kstruct(p__io) {
m__parent = p__parent;
m__root = p__root;
f_year = false;
f_padded_day = false;
f_padded_month = false;
f_padded_year = false;
_read();
}
void dos_datetime_t::date_t::_read() {
m_day = m__io->read_bits_int_le(5);
if (!(day() >= 1)) {
throw kaitai::validation_less_than_error<uint64_t>(1, day(), _io(), std::string("/types/date/seq/0"));
}
m_month = m__io->read_bits_int_le(4);
if (!(month() >= 1)) {
throw kaitai::validation_less_than_error<uint64_t>(1, month(), _io(), std::string("/types/date/seq/1"));
}
if (!(month() <= 12)) {
throw kaitai::validation_greater_than_error<uint64_t>(12, month(), _io(), std::string("/types/date/seq/1"));
}
m_year_minus_1980 = m__io->read_bits_int_le(7);
}
dos_datetime_t::date_t::~date_t() {
_clean_up();
}
void dos_datetime_t::date_t::_clean_up() {
}
int32_t dos_datetime_t::date_t::year() {
if (f_year)
return m_year;
m_year = (1980 + year_minus_1980());
f_year = true;
return m_year;
}
std::string dos_datetime_t::date_t::padded_day() {
if (f_padded_day)
return m_padded_day;
m_padded_day = ((day() <= 9) ? (std::string("0")) : (std::string(""))) + kaitai::kstream::to_string(day());
f_padded_day = true;
return m_padded_day;
}
std::string dos_datetime_t::date_t::padded_month() {
if (f_padded_month)
return m_padded_month;
m_padded_month = ((month() <= 9) ? (std::string("0")) : (std::string(""))) + kaitai::kstream::to_string(month());
f_padded_month = true;
return m_padded_month;
}
std::string dos_datetime_t::date_t::padded_year() {
if (f_padded_year)
return m_padded_year;
m_padded_year = ((year() <= 999) ? (std::string("0") + ((year() <= 99) ? (std::string("0") + ((year() <= 9) ? (std::string("0")) : (std::string("")))) : (std::string("")))) : (std::string(""))) + kaitai::kstream::to_string(year());
f_padded_year = true;
return m_padded_year;
}