Skip to content

Instantly share code, notes, and snippets.

@MatthewKing
Last active December 21, 2015 18:49
Show Gist options
  • Save MatthewKing/6350173 to your computer and use it in GitHub Desktop.
Save MatthewKing/6350173 to your computer and use it in GitHub Desktop.
Extension methods for System.Reflection.Assembly.* Returns the DateTime represented by the linker timestamp in the specified assembly.
using System;
using System.IO;
using System.Reflection;
/// <summary>
/// Extension methods for the Assembly class.
/// </summary>
internal static class AssemblyExtensions
{
/// <summary>
/// Returns the DateTime represented by the linker timestamp in the specified assembly.
/// </summary>
/// <param name="assembly">The assembly to extract the linker timestamp from.</param>
/// <returns>
/// The DateTime represented by the linker timestamp in the specified assembly.
/// </returns>
public static DateTime GetLinkerTimestamp(this Assembly assembly)
{
if (assembly == null)
throw new ArgumentNullException("assembly should not be null", "assembly");
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
byte[] buffer = new byte[2048];
string path = assembly.Location;
using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
stream.Read(buffer, 0, buffer.Length);
}
int i = BitConverter.ToInt32(buffer, peHeaderOffset);
int secondsSince1970 = BitConverter.ToInt32(buffer, i + linkerTimestampOffset);
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);
dateTime = dateTime.AddSeconds(secondsSince1970);
dateTime = dateTime.AddHours(TimeZone.CurrentTimeZone.GetUtcOffset(dateTime).Hours);
return dateTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment