Microsoft Windows SYSTEMTIME structure: C++11/STL parsing library

Microsoft Windows SYSTEMTIME structure, stores individual components of date and time as individual fields, up to millisecond precision.

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of Microsoft Windows SYSTEMTIME structure using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime 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.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (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);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    windows_systemtime_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.year() // => Year

C++11/STL source code to parse Microsoft Windows SYSTEMTIME structure

windows_systemtime.h

#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

/**
 * Microsoft Windows SYSTEMTIME structure, stores individual components
 * of date and time as individual fields, up to millisecond precision.
 * \sa https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime Source
 */

class windows_systemtime_t : public kaitai::kstruct {

public:

    windows_systemtime_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, windows_systemtime_t* p__root = nullptr);

private:
    void _read();
    void _clean_up();

public:
    ~windows_systemtime_t();

private:
    uint16_t m_year;
    uint16_t m_month;
    uint16_t m_dow;
    uint16_t m_day;
    uint16_t m_hour;
    uint16_t m_min;
    uint16_t m_sec;
    uint16_t m_msec;
    windows_systemtime_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * Year
     */
    uint16_t year() const { return m_year; }

    /**
     * Month (January = 1)
     */
    uint16_t month() const { return m_month; }

    /**
     * Day of week (Sun = 0)
     */
    uint16_t dow() const { return m_dow; }

    /**
     * Day of month
     */
    uint16_t day() const { return m_day; }

    /**
     * Hours
     */
    uint16_t hour() const { return m_hour; }

    /**
     * Minutes
     */
    uint16_t min() const { return m_min; }

    /**
     * Seconds
     */
    uint16_t sec() const { return m_sec; }

    /**
     * Milliseconds
     */
    uint16_t msec() const { return m_msec; }
    windows_systemtime_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

windows_systemtime.cpp

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

#include "windows_systemtime.h"

windows_systemtime_t::windows_systemtime_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, windows_systemtime_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    _read();
}

void windows_systemtime_t::_read() {
    m_year = m__io->read_u2le();
    m_month = m__io->read_u2le();
    m_dow = m__io->read_u2le();
    m_day = m__io->read_u2le();
    m_hour = m__io->read_u2le();
    m_min = m__io->read_u2le();
    m_sec = m__io->read_u2le();
    m_msec = m__io->read_u2le();
}

windows_systemtime_t::~windows_systemtime_t() {
    _clean_up();
}

void windows_systemtime_t::_clean_up() {
}