Snippets

Dmitry Popov TimeSpanExtensions

Created by Dmitry Popov
namespace System
{
    public static class TimeSpanExtensions
    {
        /// <summary>
        /// <para>Truncates a DateTime to a specified resolution.</para>
        /// <para>A convenient source for resolution is TimeSpan.TicksPerXXXX constants.</para>
        /// </summary>
        /// <param name="value">The <see cref="TimeSpan"/> object to truncate.</param>
        /// <param name="resolution">e.g. to round to nearest second, TimeSpan.TicksPerSecond.</param>
        /// <returns>Truncated TimeSpan.</returns>
        /// <remarks>Found at https://src-bin.com/ru/q/f549a.</remarks>
        public static TimeSpan Truncate(this TimeSpan value, long resolution)
        {
            return new TimeSpan(value.Ticks - (value.Ticks % resolution));
        }
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.