Example #1
0
        /// <summary>
        /// This method parses the string passed into a time value using
        /// the format specified.
        /// </summary>
        /// <param name="str">The string to parse.</param>
        /// <param name="format">The enumeration specifying the input format.</param>
        /// <returns>The time value or <c>null</c> if the operation failed.</returns>
        public static UnixTime Parse(string str, UnixTimeFormat format)
        {
            switch (format)
            {
            case UnixTimeFormat.Time64:

                long ticks;

                try
                {
                    ticks = long.Parse(str) * TimeSpan.TicksPerSecond;
                    return(new UnixTime(unixTimeZero + new TimeSpan(ticks)));
                }
                catch
                {
                    return(null);
                }

            case UnixTimeFormat.YYYYMMDDHHMMSS:

                int year, month, day, hour, min, sec;

                if (str.Length != 14)
                {
                    return(null);
                }

                try
                {
                    year  = int.Parse(str.Substring(0, 4));
                    month = int.Parse(str.Substring(4, 2));
                    day   = int.Parse(str.Substring(6, 2));
                    hour  = int.Parse(str.Substring(8, 2));
                    min   = int.Parse(str.Substring(10, 2));
                    sec   = int.Parse(str.Substring(12, 2));

                    return(new UnixTime(year, month, day, hour, min, sec));
                }
                catch
                {
                    return(null);
                }

            default:

                Assertion.Test(false, "Unexpected time format.");
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// This method formats the time into a string using the
        /// format type passed.
        /// </summary>
        /// <param name="format">The enumerator specifying the desired output format.</param>
        public string Format(UnixTimeFormat format)
        {
            switch (format)
            {
            case UnixTimeFormat.Time64:

                var delta = this.time - unixTimeZero;

                return((delta.Ticks / TimeSpan.TicksPerSecond).ToString());

            case UnixTimeFormat.YYYYMMDDHHMMSS:

                return(string.Format("{0:D4}{1:D2}{2:D2}{3:D2}{4:D2}{5:D2}", time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second));

            default:

                Assertion.Test(false, "Unexpected time format.");
                return("");
            }
        }
 /// <summary>
 /// Initializes a new converter for the specified <paramref name="format"/>.
 /// </summary>
 /// <param name="format">The format to be used when serializing to JSON.</param>
 public UnixTimeConverter(UnixTimeFormat format)
 {
     Format = format;
 }
 /// <summary>
 /// Initializes a new converter with default options.
 /// </summary>
 public UnixTimeConverter()
 {
     Format = UnixTimeFormat.Seconds;
 }