+ Reply to Thread
Results 1 to 2 of 2

Thread: time stamp reversal

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6

    time stamp reversal

    I want to save the time to log file , so i did that function :
    Code:
    public static string TimeStamp(
       this DateTime dt, string timeFormat = "yyyyMMddHHmmssffff")
       {
         return dt.ToString(timeFormat);
       }
    But i found that code on internet :
    Code:
    static public string ToReverseTimestamp(this DateTime dateTime)
    {
    return string.Format("{0:10}", DateTime.MaxValue.Ticks - dateTime.Ticks);
    }
    If the second snippet is correct , then how do you convert it back to "normal" timestamp or how do you get readable datetime information from it?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    15
    Make sure that the DateTime is converted to universal time.
    Code:
    public static string ToReverseTimestamp(this DateTime DT)
    {
        return (long.MaxValue - DT.ToUniversalTime().Ticks).ToString();
    }
    You can convert the value back to a DateTime value by parsing the string to a long, calculating MaxValue - (MaxValue - x) = x and constructing a new DateTime with DateTimeKind.Utc from x:
    Code:
    public static DateTime FromReverseTimestamp(string timestamp)
    {
        return new DT(long.MaxValue - long.Parse(timestamp), DateTimeKind.Utc);
    }
    Example:
    Code:
    var input = DT.Now;                      // {17/05/2012 16:03:17} (Local)
    var timestamp = ToReverseTimestamp(input);     // "2520650302020786038"
    var result = FromReverseTimestamp(timestamp);  // {17/05/2012 18:03:17} (Utc)

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts