Microsoft Windows SYSTEMTIME structure: Ruby 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 Ruby generated by Kaitai Struct depends on the Ruby runtime library. You have to install it before you can parse data.

The Ruby runtime library can be installed from RubyGems:

gem install kaitai-struct

Code

Parse a local file and get structure in memory:

data = WindowsSystemtime.from_file("path/to/local/file.bin")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = WindowsSystemtime.new(Kaitai::Struct::Stream.new(bytes))

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

data.year # => Year

Ruby source code to parse Microsoft Windows SYSTEMTIME structure

windows_systemtime.rb

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

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.9')
  raise "Incompatible Kaitai Struct Ruby API: 0.9 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# Microsoft Windows SYSTEMTIME structure, stores individual components
# of date and time as individual fields, up to millisecond precision.
# @see https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime Source
class WindowsSystemtime < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @year = @_io.read_u2le
    @month = @_io.read_u2le
    @dow = @_io.read_u2le
    @day = @_io.read_u2le
    @hour = @_io.read_u2le
    @min = @_io.read_u2le
    @sec = @_io.read_u2le
    @msec = @_io.read_u2le
    self
  end

  ##
  # Year
  attr_reader :year

  ##
  # Month (January = 1)
  attr_reader :month

  ##
  # Day of week (Sun = 0)
  attr_reader :dow

  ##
  # Day of month
  attr_reader :day

  ##
  # Hours
  attr_reader :hour

  ##
  # Minutes
  attr_reader :min

  ##
  # Seconds
  attr_reader :sec

  ##
  # Milliseconds
  attr_reader :msec
end