コード例 #1
0
        /// <summary>
        /// Truncs <see cref="DateTime"/> object to the selected accuracy.
        /// </summary>
        /// <param name="d"><see cref="DateTime"/> to round.</param>
        /// <param name="rt">Accuracy.</param>
        /// <returns>Truncated <see cref="DateTime"/>.</returns>
        public static DateTime Trunc(this DateTime d, DateTimeAccuracy rt)
        {
            DateTime dtTruncated = new DateTime();

            switch (rt)
            {
            case DateTimeAccuracy.Millisecond:
                dtTruncated = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Millisecond);
                break;

            case DateTimeAccuracy.Second:
                dtTruncated = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
                break;

            case DateTimeAccuracy.Minute:
                dtTruncated = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0);
                break;

            case DateTimeAccuracy.Hour:
                dtTruncated = new DateTime(d.Year, d.Month, d.Day, d.Hour, 0, 0);
                break;

            case DateTimeAccuracy.Day:
                dtTruncated = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0);
                break;
            }

            return(dtTruncated);
        }
コード例 #2
0
        /// <summary>
        /// Rounds <see cref="DateTime"/> object to the selected accuracy.
        /// </summary>
        /// <param name="d"><see cref="DateTime"/> to round.</param>
        /// <param name="rt">Accuracy.</param>
        /// <returns>Rounded <see cref="DateTime"/>.</returns>
        public static DateTime Round(this DateTime d, DateTimeAccuracy rt)
        {
            DateTime dtRounded = new DateTime();

            switch (rt)
            {
            case DateTimeAccuracy.Millisecond:
                dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second, d.Millisecond);
                break;

            case DateTimeAccuracy.Second:
                dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, d.Second);
                if (d.Millisecond >= 500)
                {
                    dtRounded = dtRounded.AddSeconds(1);
                }
                break;

            case DateTimeAccuracy.Minute:
                dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0);
                if (d.Second >= 30)
                {
                    dtRounded = dtRounded.AddMinutes(1);
                }
                break;

            case DateTimeAccuracy.Hour:
                dtRounded = new DateTime(d.Year, d.Month, d.Day, d.Hour, 0, 0);
                if (d.Minute >= 30)
                {
                    dtRounded = dtRounded.AddHours(1);
                }
                break;

            case DateTimeAccuracy.Day:
                dtRounded = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0);
                if (d.Hour >= 12)
                {
                    dtRounded = dtRounded.AddDays(1);
                }
                break;
            }

            return(dtRounded);
        }
コード例 #3
0
        public static int[] GetItemArray(this DateTime x, DateTimeAccuracy accuracy = DateTimeAccuracy.Millisecond)
        {
            switch (accuracy)
            {
            case DateTimeAccuracy.Date:
                return(array(x.Year, x.Month, x.Day));

            case DateTimeAccuracy.Hour:
                return(array(x.Year, x.Month, x.Day, x.Hour));

            case DateTimeAccuracy.Minute:
                return(array(x.Year, x.Month, x.Day, x.Hour, x.Minute));

            case DateTimeAccuracy.Second:
                return(array(x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second));

            case DateTimeAccuracy.Millisecond:
                return(array(x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second, x.Millisecond));
            }
            throw new NotSupportedException();
        }
コード例 #4
0
ファイル: DataTypeExtensions.cs プロジェクト: 0xCM/Meta.Core
    /// <summary>
    /// Renders a string in a more rational manner than the default behavior
    /// </summary>
    /// <param name="t">The instant to render</param>
    /// <param name="accuracy">The accuracy with which to render the instant</param>
    /// <returns></returns>
    public static string ToLexicalString(this DateTime t, DateTimeAccuracy accuracy = DateTimeAccuracy.Millisecond)
    {
        var format = String.Empty;
        var date   = t.ToString("yyyy-MM-dd");
        var hour   = t.Hour.ToString().PadLeft(2, '0');
        var minute = t.Minute.ToString().PadLeft(2, '0');
        var second = t.Second.ToString().PadLeft(2, '0');
        var ms     = t.Millisecond.ToString().PadLeft(3, '0');

        switch (accuracy)
        {
        case DateTimeAccuracy.Date:
            format = date;
            break;

        case DateTimeAccuracy.Hour:
            format = String.Format("{0} {1}:00:00:000", date, hour);
            break;

        case DateTimeAccuracy.Minute:
            format = String.Format("{0} {1}:{2}:00:000", date, hour, minute);
            break;

        case DateTimeAccuracy.Second:
            format = String.Format("{0} {1}:{2}:{3}:000", date, hour, minute, second);
            break;

        case DateTimeAccuracy.Millisecond:
            format = String.Format("{0} {1}:{2}:{3}:{4}", date, hour, minute, second, ms);
            break;

        default:
            throw new NotSupportedException();
        }
        return(format);
    }