Exemplo n.º 1
0
        public void ConvertBackAndForthWithMillisecond()
        {
            var dateTime = new DateTime(2001, 10, 17, 12, 01, 02, 64);

            var dateFormat = "dd.MM.yyyy HH:mm:ss.fff";
            var dateSep    = ".";
            var timeSep    = "-";
            var toString   = StringConversion.DateTimeToString(dateTime, dateFormat, dateSep, timeSep);
            var fromString = StringConversion.StringToDateTime(toString, dateFormat, dateSep, timeSep, false);

            Assert.AreEqual(dateTime, fromString, dateFormat);

            dateFormat = @"MM/dd/yyyy HH:mm:ss.fff";
            dateSep    = "/";
            timeSep    = ":";
            toString   = StringConversion.DateTimeToString(dateTime, dateFormat, dateSep, timeSep);
            fromString = StringConversion.StringToDateTime(toString, dateFormat, dateSep, timeSep, false);
            Assert.AreEqual(dateTime, fromString, dateFormat);

            dateFormat = @"yyyyMMdd HH:mm:ss.fff";
            dateSep    = "";
            timeSep    = ":";
            toString   = StringConversion.DateTimeToString(dateTime, dateFormat, dateSep, timeSep);
            fromString = StringConversion.StringToDateTime(toString, dateFormat, dateSep, timeSep, false);
            Assert.AreEqual(dateTime, fromString, dateFormat);
        }
Exemplo n.º 2
0
 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 = "."
     }));
 }
Exemplo n.º 3
0
        public void TestConvertDateToString()
        {
            var dateTime = new DateTime(2001, 10, 17, 12, 01, 02, 00);

            var actual = StringConversion.DateTimeToString(dateTime, @"yyyyMMdd", "", "-");

            Assert.AreEqual("20011017", actual);

            actual = StringConversion.DateTimeToString(dateTime, @"yyyy/MM/dd HH:mm:ss", "/", ":");
            Assert.AreEqual("2001/10/17 12:01:02", actual);

            dateTime = new DateTime(2001, 10, 17, 12, 01, 02, 09);

            actual = StringConversion.DateTimeToString(dateTime, @"yyyyMMdd HH:mm:ss.fff", "", "-");
            Assert.AreEqual("20011017 12-01-02.009", actual);

            actual = StringConversion.DateTimeToString(dateTime, @"yyyy/MM/dd HH:mm:ss.fff", "/", ":");
            Assert.AreEqual("2001/10/17 12:01:02.009", actual);

            // nonsense value for date separator
            actual = StringConversion.DateTimeToString(dateTime, @"yyyy-MM-dd HH:mm:ss.fff", "-", ":");
            Assert.AreEqual("2001-10-17 12:01:02.009", actual);
        }
Exemplo n.º 4
0
        protected string TextEncodeField([NotNull] IFileFormat fileFormat, object dataObject,
                                         [NotNull] WriterColumn columnInfo, bool isHeader,
                                         [CanBeNull] IDataReader reader, [CanBeNull] Func <string, DataType, IFileFormat, string> handleQualify)
        {
            if (columnInfo is null)
            {
                throw new ArgumentNullException(nameof(columnInfo));
            }

            if (fileFormat.IsFixedLength && columnInfo.FieldLength == 0)
            {
                throw new FileWriterException("For fix length output the length of the columns needs to be specified.");
            }

            string displayAs;

            if (isHeader)
            {
                if (dataObject is null)
                {
                    throw new ArgumentNullException(nameof(dataObject));
                }
                displayAs = dataObject.ToString();
            }
            else
            {
                try
                {
                    if (dataObject == null || dataObject is DBNull)
                    {
                        displayAs = columnInfo.ValueFormat.DisplayNullAs;
                    }
                    else
                    {
                        switch (columnInfo.ValueFormat.DataType)
                        {
                        case DataType.Integer:
                            displayAs = Convert.ToInt64(dataObject).ToString(columnInfo.ValueFormat.NumberFormat, CultureInfo.InvariantCulture).Replace(
                                CultureInfo.InvariantCulture.NumberFormat.NumberGroupSeparator, columnInfo.ValueFormat.GroupSeparator);
                            break;

                        case DataType.Boolean:
                            displayAs = (bool)dataObject
                  ? columnInfo.ValueFormat.True
                  : columnInfo.ValueFormat.False;
                            break;

                        case DataType.Double:
                            displayAs = StringConversion.DoubleToString(
                                dataObject is double d ? d : Convert.ToDouble(dataObject.ToString(), CultureInfo.InvariantCulture),
                                columnInfo.ValueFormat);
                            break;

                        case DataType.Numeric:
                            displayAs = StringConversion.DecimalToString(
                                dataObject is decimal @decimal
                    ? @decimal
                    : Convert.ToDecimal(dataObject.ToString(), CultureInfo.InvariantCulture),
                                columnInfo.ValueFormat);
                            break;

                        case DataType.DateTime:
                            displayAs = reader == null
                  ? StringConversion.DateTimeToString((DateTime)dataObject, columnInfo.ValueFormat)
                  : StringConversion.DateTimeToString(HandleTimeZone((DateTime)dataObject, columnInfo, reader),
                                                      columnInfo.ValueFormat);

                            break;

                        case DataType.Guid:
                            // 382c74c3-721d-4f34-80e5-57657b6cbc27
                            displayAs = ((Guid)dataObject).ToString();
                            break;

                        case DataType.String:
                        case DataType.TextToHtml:
                        case DataType.TextToHtmlFull:
                        case DataType.TextPart:
                            displayAs = dataObject.ToString();
                            if (columnInfo.ValueFormat.DataType == DataType.TextToHtml)
                            {
                                displayAs = HTMLStyle.TextToHtmlEncode(displayAs);
                            }

                            // a new line of any kind will be replaced with the placeholder if set
                            if (fileFormat.NewLinePlaceholder.Length > 0)
                            {
                                displayAs = StringUtils.HandleCRLFCombinations(displayAs, fileFormat.NewLinePlaceholder);
                            }

                            if (fileFormat.DelimiterPlaceholder.Length > 0 && fileFormat.FieldDelimiterChar != '\0')
                            {
                                displayAs = displayAs.Replace(fileFormat.FieldDelimiterChar.ToStringHandle0(),
                                                              fileFormat.DelimiterPlaceholder);
                            }

                            if (fileFormat.QuotePlaceholder.Length > 0 && fileFormat.FieldQualifierChar != '\0')
                            {
                                displayAs = displayAs.Replace(fileFormat.FieldQualifierChar.ToStringHandle0(), fileFormat.QuotePlaceholder);
                            }
                            break;

                        default:
                            displayAs = string.Empty;
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    // In case a cast did fail (eg.g trying to format as integer and providing a text, use the
                    // original value
                    displayAs = dataObject?.ToString() ?? string.Empty;
                    if (string.IsNullOrEmpty(displayAs))
                    {
                        HandleError(columnInfo.Name, ex.Message);
                    }
                    else
                    {
                        HandleWarning(columnInfo.Name,
                                      "Value stored as: " + displayAs +
                                      $"\nExpected {columnInfo.ValueFormat.DataType} but was {dataObject?.GetType()}" + ex.Message);
                    }
                }
            }

            // Adjust the output in case its is fixed length
            if (fileFormat.IsFixedLength)
            {
                if (displayAs.Length <= columnInfo.FieldLength || columnInfo.FieldLength <= 0)
                {
                    return(displayAs.PadRight(columnInfo.FieldLength, ' '));
                }
                HandleWarning(columnInfo.Name,
                              $"Text with length of {displayAs.Length} has been cut off after {columnInfo.FieldLength} character");
                return(displayAs.Substring(0, columnInfo.FieldLength));
            }

            // Qualify text if required
            if (fileFormat.FieldQualifierChar != '\0' && handleQualify != null)
            {
                return(handleQualify(displayAs, columnInfo.ValueFormat.DataType, fileFormat));
            }

            return(displayAs);
        }