MS-DOS datetime: Perl parsing library

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:

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

References

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.

Perl source code to parse MS-DOS datetime

DosDatetime.pm

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

use strict;
use warnings;
use IO::KaitaiStruct 0.009_000;

########################################################################
package DosDatetime;

our @ISA = 'IO::KaitaiStruct::Struct';

sub from_file {
    my ($class, $filename) = @_;
    my $fd;

    open($fd, '<', $filename) or return undef;
    binmode($fd);
    return new($class, IO::KaitaiStruct::Stream->new($fd));
}

sub new {
    my ($class, $_io, $_parent, $_root) = @_;
    my $self = IO::KaitaiStruct::Struct->new($_io);

    bless $self, $class;
    $self->{_parent} = $_parent;
    $self->{_root} = $_root || $self;;

    $self->_read();

    return $self;
}

sub _read {
    my ($self) = @_;

    $self->{time} = DosDatetime::Time->new($self->{_io}, $self, $self->{_root});
    $self->{date} = DosDatetime::Date->new($self->{_io}, $self, $self->{_root});
}

sub time {
    my ($self) = @_;
    return $self->{time};
}

sub date {
    my ($self) = @_;
    return $self->{date};
}

########################################################################
package DosDatetime::Time;

our @ISA = 'IO::KaitaiStruct::Struct';

sub from_file {
    my ($class, $filename) = @_;
    my $fd;

    open($fd, '<', $filename) or return undef;
    binmode($fd);
    return new($class, IO::KaitaiStruct::Stream->new($fd));
}

sub new {
    my ($class, $_io, $_parent, $_root) = @_;
    my $self = IO::KaitaiStruct::Struct->new($_io);

    bless $self, $class;
    $self->{_parent} = $_parent;
    $self->{_root} = $_root || $self;;

    $self->_read();

    return $self;
}

sub _read {
    my ($self) = @_;

    $self->{second_div_2} = $self->{_io}->read_bits_int_le(5);
    $self->{minute} = $self->{_io}->read_bits_int_le(6);
    $self->{hour} = $self->{_io}->read_bits_int_le(5);
}

sub second {
    my ($self) = @_;
    return $self->{second} if ($self->{second});
    $self->{second} = (2 * $self->second_div_2());
    return $self->{second};
}

sub padded_second {
    my ($self) = @_;
    return $self->{padded_second} if ($self->{padded_second});
    $self->{padded_second} = ($self->second() <= 9 ? "0" : "") . sprintf('%d', $self->second());
    return $self->{padded_second};
}

sub padded_minute {
    my ($self) = @_;
    return $self->{padded_minute} if ($self->{padded_minute});
    $self->{padded_minute} = ($self->minute() <= 9 ? "0" : "") . sprintf('%d', $self->minute());
    return $self->{padded_minute};
}

sub padded_hour {
    my ($self) = @_;
    return $self->{padded_hour} if ($self->{padded_hour});
    $self->{padded_hour} = ($self->hour() <= 9 ? "0" : "") . sprintf('%d', $self->hour());
    return $self->{padded_hour};
}

sub second_div_2 {
    my ($self) = @_;
    return $self->{second_div_2};
}

sub minute {
    my ($self) = @_;
    return $self->{minute};
}

sub hour {
    my ($self) = @_;
    return $self->{hour};
}

########################################################################
package DosDatetime::Date;

our @ISA = 'IO::KaitaiStruct::Struct';

sub from_file {
    my ($class, $filename) = @_;
    my $fd;

    open($fd, '<', $filename) or return undef;
    binmode($fd);
    return new($class, IO::KaitaiStruct::Stream->new($fd));
}

sub new {
    my ($class, $_io, $_parent, $_root) = @_;
    my $self = IO::KaitaiStruct::Struct->new($_io);

    bless $self, $class;
    $self->{_parent} = $_parent;
    $self->{_root} = $_root || $self;;

    $self->_read();

    return $self;
}

sub _read {
    my ($self) = @_;

    $self->{day} = $self->{_io}->read_bits_int_le(5);
    $self->{month} = $self->{_io}->read_bits_int_le(4);
    $self->{year_minus_1980} = $self->{_io}->read_bits_int_le(7);
}

sub year {
    my ($self) = @_;
    return $self->{year} if ($self->{year});
    $self->{year} = (1980 + $self->year_minus_1980());
    return $self->{year};
}

sub padded_day {
    my ($self) = @_;
    return $self->{padded_day} if ($self->{padded_day});
    $self->{padded_day} = ($self->day() <= 9 ? "0" : "") . sprintf('%d', $self->day());
    return $self->{padded_day};
}

sub padded_month {
    my ($self) = @_;
    return $self->{padded_month} if ($self->{padded_month});
    $self->{padded_month} = ($self->month() <= 9 ? "0" : "") . sprintf('%d', $self->month());
    return $self->{padded_month};
}

sub padded_year {
    my ($self) = @_;
    return $self->{padded_year} if ($self->{padded_year});
    $self->{padded_year} = ($self->year() <= 999 ? "0" . ($self->year() <= 99 ? "0" . ($self->year() <= 9 ? "0" : "") : "") : "") . sprintf('%d', $self->year());
    return $self->{padded_year};
}

sub day {
    my ($self) = @_;
    return $self->{day};
}

sub month {
    my ($self) = @_;
    return $self->{month};
}

sub year_minus_1980 {
    my ($self) = @_;
    return $self->{year_minus_1980};
}

1;