【问题标题】:How to fix wrong format time? [duplicate]如何修复错误的格式时间? [复制]
【发布时间】:2021-07-14 05:18:19
【问题描述】:

我在插入关于时间的数据时发现了一个问题。

通常,用户会将 CSV 文件从测试仪保存到服务器上。

我将通过程序读取和插入数据。

我的列时间有问题,因为没有符号“:”来表示小时、分钟和秒

示例:231620 我通过子字符串函数修复了。

string start_InspHour_Converted = 
   start_InspHour.Substring(0, 2) + ":" + 
   start_InspHour.Substring(2, 2) + ":" + 
   start_InspHour.Substring(4, 2);

但有些文件格式错误。

示例:22411 02:24:11 应该是 022411

我认为列时间必须将格式更改为文本。

我不知道为什么用户无法修复它,他们什么也不做,仍然向我发送更多文件。

我需要帮助通过编码解决此问题,但我不知道要搜索的关键字。

对不起我的英语

【问题讨论】:

  • 强制xkcd.com/1179 链接...如果您无法强制执行正确的 ISO8601 输入...请edit 澄清您对“1234”结果的期望有效 输入(这也是你为什么不应该尝试的好例子)...
  • 让用户一开始就以适当的时间格式保存数据不是更容易吗?
  • var dtm = DateTime.ParseExact(start_InspHour.PadLeft(6, '0'), "HHmmss", null);,如果用户不同意的话。
  • string output = null; if (DateTime.TryParseExact(input.PadLeft(6, '0'), "HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result)) output = result.ToString("HH:mm:ss");
  • 您可能需要考虑这一点……如果“YOUR”代码从存在歧义的格式错误的字符串中确定时间值,并且“YOUR”代码错误地确定时间,并且用户受到此影响……然后......用户将责怪“你的”代码。让您的生活更轻松,并将正确的时间格式要求放在用户身上,这样他们就不会因为错误的时间而责备他们自己。只是一个想法。

标签: c#


【解决方案1】:

你能有这样一个简单的条件吗?

if (start_InspHour.Length == 6)
{
     string start_InspHour_Converted = start_InspHour.Substring(0, 2) + ":" + 
     start_InspHour.Substring(2, 2) + ":" + start_InspHour.Substring(4, 2);
}
else
{
     string start_InspHour_Converted = "0" + start_InspHour.Substring(0, 1) + ":" + 
     start_InspHour.Substring(1, 2) + ":" + start_InspHour.Substring(3, 2);
}

【讨论】:

    【解决方案2】:

    定义一个 TimeStr 类。

    class TimeStr {
    
        // private data members.
        private int _hours;
        private int _minutes;
        private int _seconds;
    
        // public accessor methods.
        public int Hours => _hours;
    
        public int Minutes => _minutes;
    
        public int Seconds => _seconds;
    
        // class constructor.
        public TimeStr(string strIn) {
    
            // HH:MM:SS
            Match m = Regex.Match(strIn, @"^(\d{2}):(\d{2}):(\d{2})$");
            if (m.Success) {
                this._hours = int.Parse(m.Groups[1].Value);
                this._minutes = int.Parse(m.Groups[2].Value);
                this._seconds = int.Parse(m.Groups[3].Value);
    
            } else {
    
                // HHMMSS
                m = Regex.Match(strIn, @"^(\d{2})(\d{2})(\d{2})$");
                if (m.Success) {
                    this._hours = int.Parse(m.Groups[1].Value);
                    this._minutes = int.Parse(m.Groups[2].Value);
                    this._seconds = int.Parse(m.Groups[3].Value);
    
                } else {
    
                    // HMMSS
                    m = Regex.Match(strIn, @"^(\d)(\d{2})(\d{2})$");
                    if (m.Success) {
                        this._hours = int.Parse(m.Groups[1].Value);
                        this._minutes = int.Parse(m.Groups[2].Value);
                        this._seconds = int.Parse(m.Groups[3].Value);
    
                    } else {
    
                        // Unrecognized format.
                        throw new ArgumentException();
                    }
                }
            }
        }
    
        // returns time as HH:MM:SS
        public override string ToString() {
            return $"{this._hours.ToString("00")}:{this._minutes.ToString("00")}:{this._seconds.ToString("00")}";
        }
    

    这是如何使用它。

    class Program
    {
        public static void Main(String[] args) {
    
            TimeStr t1 = new Time("23:16:20");
            Console.WriteLine(t1.ToString());
    
            TimeStr t2 = new Time("231620");
            Console.WriteLine(t2.ToString());
    
            TimeStr t3 = new Time("31620");
            Console.WriteLine(t3.ToString());
    
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
    }
    

    【讨论】:

    • 我的荣幸....
    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 2011-10-10
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    相关资源
    最近更新 更多