crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianStructuresLib/Debug/Trace/TraceTimeStamp.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.IO;
       
    20 using System.Text;
       
    21 using SymbianUtils;
       
    22 using SymbianUtils.Threading;
       
    23 
       
    24 namespace SymbianStructuresLib.Debug.Trace
       
    25 {
       
    26     public class TraceTimeStamp : IFormattable
       
    27     {
       
    28         #region Constructors
       
    29         public TraceTimeStamp( ulong aNanoSeconds )
       
    30         {
       
    31             iValue = aNanoSeconds;
       
    32         }
       
    33         #endregion
       
    34 
       
    35         #region API
       
    36         #endregion
       
    37 
       
    38         #region Properties
       
    39         public ulong NanoSeconds
       
    40         {
       
    41             get { return iValue; }
       
    42         }
       
    43         #endregion
       
    44 
       
    45         #region From System.Object
       
    46         public override string ToString()
       
    47         {
       
    48             string ret = ToString( "full", null );
       
    49             return ret;
       
    50         }
       
    51         #endregion
       
    52 
       
    53         #region From IFormattable
       
    54         public string ToString( string aFormat, IFormatProvider aFormatProvider )
       
    55         {
       
    56             StringBuilder ret = new StringBuilder();
       
    57             //
       
    58             if ( aFormat == "nanoseconds" )
       
    59             {
       
    60                 ret.AppendFormat( "[{0:d}]", iValue );
       
    61             }
       
    62             else
       
    63             {
       
    64                 try
       
    65                 {
       
    66                     ulong milliseconds = iValue / KNanosecondsToMilliseconds;
       
    67                     ulong ticks = milliseconds * (ulong) TimeSpan.TicksPerMillisecond;
       
    68                     
       
    69                     // Now we have a time span, but it includes the rather ugly number of days
       
    70                     // and also an inaccurate number of microseconds.
       
    71                     TimeSpan ts = new TimeSpan( (long) ticks );
       
    72                     
       
    73                     // First with discard all the milli seconds
       
    74                     ts = ts.Subtract( new TimeSpan( 0, 0, 0, 0, ts.Milliseconds ) );
       
    75 
       
    76                     // Next, we calculate the microseconds fraction
       
    77                     ulong oneNsInSeconds = KNanosecondsToMilliseconds * 1000;
       
    78                     ulong totalNsInSeconds = (ulong) ( ts.TotalSeconds * oneNsInSeconds );
       
    79                     ulong nsLeftOver = iValue % totalNsInSeconds;
       
    80                     float usFraction = (float) nsLeftOver / (float) oneNsInSeconds;
       
    81 
       
    82                     // Now discard the number of days - we're not interested in this at all.
       
    83                     ts = ts.Subtract( new TimeSpan( ts.Days, 0, 0, 0, 0 ) );
       
    84 
       
    85                     // Finally, we can assemble the time stamp. First we'll add in the
       
    86                     // hh:mm:ss part, and there will be no ms, so the decimal aspect is
       
    87                     // entirely missing at this stage.
       
    88                     ret.Append( ts.ToString() );
       
    89 
       
    90                     // Next, we'll add back in the fractional part, ignoring the leading 0 prefix
       
    91                     // (i.e. the non fraction part of the string representation of a fractional number, 
       
    92                     // e.g. in 0.05, we throw away the leading 0, to leave .05).
       
    93                     string fraction = usFraction.ToString( "0.00000000" ).Substring( 1 );
       
    94                     ret.Append( fraction );
       
    95                 }
       
    96                 catch
       
    97                 {
       
    98                     // Fall back
       
    99                     ret.Append( this.ToString() );
       
   100                 }
       
   101             }
       
   102             //
       
   103             return ret.ToString();
       
   104         }
       
   105         #endregion
       
   106 
       
   107         #region Internal constants
       
   108         private const ulong KNanosecondsToMilliseconds = 1000000;
       
   109 
       
   110 	    private const int TEN = 10;
       
   111 	    private const int THOUSAND = 1000;
       
   112 	    private const int HOURS_IN_DAY = 24;
       
   113 	    private const int MINUTES_IN_HOUR = 60;
       
   114 	    private const int SECONDS_IN_MINUTE = 60;
       
   115 	    private const int MILLISECS_IN_SECOND = THOUSAND;
       
   116 	    private const ulong MILLISECS_IN_MINUTE = MILLISECS_IN_SECOND * SECONDS_IN_MINUTE;
       
   117 	    private const ulong MILLISECS_IN_HOUR = MILLISECS_IN_MINUTE * MINUTES_IN_HOUR;
       
   118 	    private const ulong MILLISECS_IN_DAY = MILLISECS_IN_HOUR * HOURS_IN_DAY;
       
   119         #endregion
       
   120 
       
   121         #region Data members
       
   122         private ulong iValue;
       
   123         #endregion
       
   124     }
       
   125 }