public void DateTimeToStringOK()
 {
     Assert.AreEqual("01/01/2010", StringConversion.DateTimeToString(new DateTime(2010, 01, 1), null));
     Assert.AreEqual("13/01/2010 10:11", StringConversion.DateTimeToString(new DateTime(2010, 1, 13, 10, 11, 14, 0),
                                                                           new ValueFormat
     {
         DateFormat = @"dd/MM/yyyy HH:mm"
     }));
     // Make sure exchanging the default separators do not mess with the result
     Assert.AreEqual("13:01:2010 10/11", StringConversion.DateTimeToString(new DateTime(2010, 1, 13, 10, 11, 14, 0),
                                                                           new ValueFormat
     {
         DateFormat    = @"dd/MM/yyyy HH:mm",
         TimeSeparator = "/",
         DateSeparator = ":"
     }));
     // 24 + 24 + 7 = 55 hrs
     Assert.AreEqual("055:11", StringConversion.DateTimeToString(
                         StringConversion.GetTimeFromTicks(new TimeSpan(2, 7, 11, 0).Ticks), new ValueFormat
     {
         DateFormat    = @"HHH:mm",
         TimeSeparator = ":",
         DateSeparator = "."
     }));
 }
示例#2
0
        public void GetTimeFromTicks()
        {
            var res = StringConversion.GetTimeFromTicks(5 * TimeSpan.TicksPerMillisecond);

            Assert.AreEqual(new DateTime(1899, 12, 30, 0, 0, 0, 5), res);
        }
        /// <summary>
        ///   Builds the value clusters date.
        /// </summary>
        /// <param name="dataTable">The data view.</param>
        /// <param name="columnIndex">Index of the column.</param>
        /// <param name="maxNumber">The maximum number.</param>
        /// <returns></returns>
        private BuildValueClustersResult BuildValueClustersDate(DataTable dataTable, int columnIndex, int maxNumber)
        {
            // Get the distinct values and their counts

            var clusterYear  = new HashSet <int>();
            var clusterMonth = new HashSet <DateTime>();
            var clusterDay   = new HashSet <DateTime>();
            var clusterHour  = new HashSet <long>();

            var columnName = dataTable.Columns[columnIndex].ColumnName;

            foreach (DataRow dataRow in dataTable.Rows)
            {
                int      keyYear;
                DateTime keyMonth;
                DateTime keyDate;
                long     keyHour;
                if (dataRow[columnIndex] == DBNull.Value)
                {
                    keyYear  = 0;
                    keyHour  = 0;
                    keyMonth = DateTime.MinValue;
                    keyDate  = DateTime.MinValue;
                }
                else
                {
                    var value = (DateTime)dataRow[columnIndex];
                    keyHour  = GetHourKey(value);
                    keyDate  = value.Date;
                    keyYear  = GetYearKey(value);
                    keyMonth = GetMonthKey(value);
                }

                clusterHour.Add(keyHour);
                clusterDay.Add(keyDate);
                clusterMonth.Add(keyMonth);
                clusterYear.Add(keyYear);

                // if we have more than the maximum entries stop, no value filter will be used
                if (clusterYear.Count <= maxNumber)
                {
                    continue;
                }
                m_ValueClusters.Clear();
                return(BuildValueClustersResult.TooManyValues);
            }

            if (clusterYear.Count == 0)
            {
                m_ValueClusters.Clear();
                return(BuildValueClustersResult.NoValues);
            }

            if (clusterDay.Count == 1)
            {
                m_Type = ValueClustersGroupType.DateHours;
                foreach (var dic in clusterHour)
                {
                    if (dic != 0)
                    {
                        var from = StringConversion.GetTimeFromTicks(dic * c_TicksPerGroup);
                        var to   = StringConversion.GetTimeFromTicks((dic + 1) * c_TicksPerGroup);
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = $"{from:t} - {to:t}",
                            SQLCondition = string.Format(CultureInfo.InvariantCulture,
                                                         @"({0} >= #{1:MM\/dd\/yyyy HH:mm}# AND {0} < #{2:MM\/dd\/yyyy HH:mm}#)",
                                                         StringUtilsSQL.SqlNameSafe(columnName), from, to),
                            Sort = GetHourSort(dic)
                        });
                    }
                    else
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = ColumnFilterLogic.cOPisNull,
                            SQLCondition = string.Format(CultureInfo.InvariantCulture, "({0} IS NULL)",
                                                         StringUtilsSQL.SqlNameSafe(columnName)),
                            Sort = string.Empty
                        });
                    }
                }
            }
            else if (clusterDay.Count < maxNumber)
            {
                m_Type = ValueClustersGroupType.DateDay;
                foreach (var dic in clusterDay)
                {
                    if (dic != DateTime.MinValue)
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = dic.ToString("d", CultureInfo.CurrentCulture),
                            SQLCondition = string.Format(CultureInfo.InvariantCulture,
                                                         @"({0} >= #{1:MM\/dd\/yyyy}# AND {0} < #{2:MM\/dd\/yyyy}#)", StringUtilsSQL.SqlNameSafe(columnName),
                                                         dic, dic.AddDays(1)),
                            Sort = GetDaySort(dic)
                        });
                    }
                    else
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = ColumnFilterLogic.cOPisNull,
                            SQLCondition =
                                string.Format(CultureInfo.InvariantCulture, c_IsNull, StringUtilsSQL.SqlNameSafe(columnName)),
                            Sort = string.Empty
                        });
                    }
                }
            }
            else if (clusterMonth.Count < maxNumber)
            {
                m_Type = ValueClustersGroupType.DateMonth;
                foreach (var dic in clusterMonth)
                {
                    if (dic != DateTime.MinValue)
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = dic.ToString("Y", CultureInfo.CurrentCulture), // Year month pattern
                            SQLCondition = string.Format(CultureInfo.InvariantCulture,
                                                         @"({0} >= #{1:MM\/dd\/yyyy}# AND {0} < #{2:MM\/dd\/yyyy}#)", StringUtilsSQL.SqlNameSafe(columnName),
                                                         dic, dic.AddMonths(1)),
                            Sort = GetMonthSort(dic)
                        });
                    }
                    else
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = ColumnFilterLogic.cOPisNull,
                            SQLCondition =
                                string.Format(CultureInfo.InvariantCulture, c_IsNull, StringUtilsSQL.SqlNameSafe(columnName)),
                            Sort = string.Empty
                        });
                    }
                }
            }
            else
            {
                m_Type = ValueClustersGroupType.DateYear;
                foreach (var dic in clusterYear)
                {
                    if (dic != 0)
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = dic.ToString("D", CultureInfo.CurrentCulture), // Decimal
                            SQLCondition = string.Format(CultureInfo.InvariantCulture,
                                                         "({0} >= #01/01/{1:d4}# AND {0} < #01/01/{2:d4}#)", StringUtilsSQL.SqlNameSafe(columnName), dic,
                                                         dic + 1),
                            Sort = GetYearSort(dic)
                        });
                    }
                    else
                    {
                        m_ValueClusters.Add(new ValueCluster
                        {
                            Display      = ColumnFilterLogic.cOPisNull,
                            SQLCondition =
                                string.Format(CultureInfo.InvariantCulture, c_IsNull, StringUtilsSQL.SqlNameSafe(columnName)),
                            Sort = string.Empty
                        });
                    }
                }
            }

            return(BuildValueClustersResult.ListFilled);
        }