0

I have found a problem when insert data about time.

Usually, User will save CSV file from tester to me at server.

I'll read and insert data by program.

I have problem with column time cause no have symbol ":" to indicating Hour, minute and second

Example : 231620 I fixed by substring function.

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

But some file has wrong format.

Example : 22411 It should be 022411 for 02:24:11

I think column time have to change format to text.

I don't know why user can't fix it they do nothing and still send me more files.

I need help to fixed this by coding but I don't know keyword to search.

Sorry for my english

  • 1
    Mandatory https://xkcd.com/1179/ link... If you can't enforce proper ISO8601 input... please [edit] the question to clarify what you expect as result of "1234" *valid* input (which is also good example of why you should not even try)... – Alexei Levenkov Apr 20 '21 at 02:41
  • 3
    Would it not be easier to have the user save the data in a proper time format to begin with? – JohnG Apr 20 '21 at 02:44
  • 1
    `var dtm = DateTime.ParseExact(start_InspHour.PadLeft(6, '0'), "HHmmss", null);`, if the Users won't. – Jimi Apr 20 '21 at 02:49
  • 1
    `string output = null; if (DateTime.TryParseExact(input.PadLeft(6, '0'), "HHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result)) output = result.ToString("HH:mm:ss");` – 41686d6564 stands w. Palestine Apr 20 '21 at 02:50
  • 3
    You may want to consider this… IF “YOUR” code determines a time value from a malformed string where ambiguity exists, and “YOUR” code incorrectly determines a time, and the user is impacted by this… then… the user will blame “YOUR” code. Make your life easier and put the proper time format requirement on the user, then they have no one to blame for incorrect times but themselves. Just a thought. – JohnG Apr 20 '21 at 02:55

2 Answers2

1

Could you just have a simple condition like this?

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);
}
RidicCoder
  • 88
  • 8
1

Define a class 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")}";
    }

This is how to use it.

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();
    }
}
Nicholas Hunter
  • 1,791
  • 1
  • 11
  • 14