Skip to content

Instantly share code, notes, and snippets.

@rasoulian
Created September 15, 2019 16:48
Show Gist options
  • Save rasoulian/78b9dfe136e6a8bd5ff07a5c30e9ae48 to your computer and use it in GitHub Desktop.
Save rasoulian/78b9dfe136e6a8bd5ff07a5c30e9ae48 to your computer and use it in GitHub Desktop.
public static DateTime GetLinkerTime(this Assembly assembly, TimeZoneInfo target = null)
{
var filePath = assembly.Location;
const int c_PeHeaderOffset = 60;
const int c_LinkerTimestampOffset = 8;
var buffer = new byte[2048];
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
stream.Read(buffer, 0, 2048);
var offset = BitConverter.ToInt32(buffer, c_PeHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(buffer, offset + c_LinkerTimestampOffset);
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var linkTimeUtc = epoch.AddSeconds(secondsSince1970);
var tz = target ?? TimeZoneInfo.Local;
var localTime = TimeZoneInfo.ConvertTimeFromUtc(linkTimeUtc, tz);
return localTime;
}
var linkTimeLocal = Assembly.GetExecutingAssembly().GetLinkerTime();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment