1

Here's a piece of code for obtaining the time when a .NET assembly was built. Note this line:

int secondsSince1970 = System.BitConverter.ToInt32(b, i + c_LinkerTimestampOffset);

this code extracts the TimeDateStamp member of IMAGE_FILE_HEADER structure that is stored inside the assembly. The structure is defined as follows:

typedef struct _IMAGE_FILE_HEADER {
  WORD  Machine;
  WORD  NumberOfSections;
  DWORD TimeDateStamp;
  DWORD PointerToSymbolTable;
  DWORD NumberOfSymbols;
  WORD  SizeOfOptionalHeader;
  WORD  Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

and DWORD is defined as follows:

typedef unsigned long DWORD;

and the struct description says that TimeDateStamp is a number of seconds since an arbitrary moment in the past, so it can't be negative.

Why does the C# code use signed type int to store that unsigned value?

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 4
    "number of seconds elapsed since midnight (00:00:00), January 1, 1970" could in theory need to be a negative number. (Although I suppose not many .NET assemblies were _actually_ built before then, it also says "according to the system clock" which... could be lying?) – Rawling Sep 28 '12 at 09:43
  • @Rawling: PE file format was surely invented after that moment. – sharptooth Sep 28 '12 at 09:44
  • Point taken, although I've edited my comment :) – Rawling Sep 28 '12 at 09:44
  • However, Could I simulate time travel with some dodgy compiler? – Jodrell Sep 28 '12 at 09:47
  • I guess it is a bug. IMAGE_FILE_HEADER specifies that it can *not* be negative. – usr Sep 28 '12 at 09:48
  • 1
    If this is (very) old C, `long` is 32 bits. But otherwise the main question is why it use Int32 and not Int64. – H H Sep 28 '12 at 09:48
  • since it is "just" the code from another stackoverflow user, I believe he used it the right way. As sharptooth said, the seconds since the epoch can be a negative amount too, if the date was earlier than 1970. The coder most likely used it because it is good practice to do so, but in this case it is ikely to be unnessessary and an UInt wouldve worked well too – Najzero Sep 28 '12 at 09:49
  • @Najzero: Actually the problem with a signed value is that it will "overflow" at some point and turn negative. – sharptooth Sep 28 '12 at 09:52
  • 1
    It's actually overflowing (more than 2^31 seconds) in around 2030, causing serious software failure ... – Walter Sep 28 '12 at 09:58
  • yep, still waiting for the Y2K nuclear fallout... maybe the unix timestamp will solve that ;-) – Najzero Sep 28 '12 at 11:46

1 Answers1

0

It is because unsigned int is not a CLS compliant variable type and all .NET libraries should follow Common Language Specification.

More info about CLS compliance:
http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx

bozydar.sz
  • 359
  • 2
  • 12