How can I convert timestamp value to DateTime in C#

Hi,

How can I convert timestamp value (which in ulong type) to DateTime in C#?

Thanks,
Muhammad Masood

If you are referring to the timestamp in the GTFS header, this is in POSIX time. You can convert it like this:

	private DateTime ConvertPOSIX(ulong posix)
	{
		DateTime dt = new DateTime();

		try
		{
			// POSIX is seconds since 1 Jan 1970
			dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

			// add POSIX seconds
			dt = dt.AddSeconds((double)posix);

			// get timezone
			TimeZoneInfo timezoneInfo = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");

			// convert from UTC to the timezone
			dt = TimeZoneInfo.ConvertTimeFromUtc(dt, timezoneInfo);
		}
		catch (Exception e)
		{
			Console.WriteLine("ERROR: Exception received during ConvertPOSIX: " + e.Message);
		}

		return dt;
	}
1 Like