This is a creation in Article, where the information may have evolved or changed.
PackageMainImport "Flag"Import "FMT"Import "Bufio"Import "OS"Import "StrConv"Import "Strings"varInFile *string= Flag. String ("I","Before.txt","Work hours to convert")varOutFile *string= Flag. String ("O","After.txt","Work hours converted")//Declare a variable to temporarily record a new row of datavarNewLinestring//Declare a slice and use him to save all new rows of datavarnewlines []string/** * Please manually remove the line of absence or late, leave early (these conditions require special treatment) * Calculate normal commute, the day of absence, leave early on the work hours (unit: Hours), reserved to two digits after the decimal point. * Before calculating the file, please name it before.txt, put it in the Code sibling directory. * Go Language Sort * *funcMain () {//Initialize the slice that holds the new row data, given a maximum of 31 days per month, assign it an initial size of 32, and the average time to print more than one rowNewlines = Make([]string, +) flag. Parse ()ifInFile! =Nil{FMT. Println ("infile=", *infile,"outfile=", *outfile)}//Open files that are not processedFile, err: = OS. Open (*infile)ifErr! =Nil{FMT. Println ("Failed to open file", infile)return}deferFile. Close () br: = Bufio. Newreader (file) Sumhour: =0.0Index: =0 for{//read one row at a time, allowing the new row to be returned emptyNewLine ="" //Read a row of dataLine, Isprefix, Readlineerr: = Br. ReadLine ()ifReadlineerr! =Nil{//already finished reading Break}ifIsprefix {//Line too long, unable to process, exit programFmt. Println ("The line is too long.")return}//character array to stringSTR: =string(line)//tab to split one row of data, No. 0 string is date, 1th data is working time, 2nd data is off dutySTRs: = Strings. Split (str,"\ T")//Jean Cong first record the dateNewLine = STRs[0] +" " //Handle office hours, split by Colon, the former is hour, the latter is minutesStart: = Strings. Split (STRs[1],":") Starthour, Starthourerr: = StrConv. Atoi (Start[0])ifStarthourerr! =Nil{//Switch to work hours when something goes wrongFmt. Println ("An error occurred during the conversion to work hours")return} startminute, Startminuteerr: = StrConv. Atoi (Start[1])ifStartminuteerr! =Nil{//Convert to work minute errorFmt. Println ("Conversion to work minutes error")return}//Handle off hours, split according to Colon, the former is hour, the latter is minutesEnd: = Strings. Split (STRs[2],":") Endhour, Endhourerr: = StrConv. Atoi (end[0])ifEndhourerr! =Nil{//Conversion error When the hours are offFmt. Println ("Conversion of an off-hours error occurred")return} endminute, Endminuteerr: = StrConv. Atoi (end[1])ifEndminuteerr! =Nil{//Conversion error when working out of hoursFmt. Println ("Conversion of off-hours minutes error")return}//filter for non-compliant time (late arrivals) ifStarthour >9{//LateFmt. Println ("Please remove the time of late")return}//filter for non-compliant time (with early leave) ifEndhour = = -&& Endminute < -|| Endhour < -{// leave earlyFmt. Println ("Please remove the time to leave early")return}//daily work hours in decimal formCurrentdayhour: =0.0 //Office hours converted to decimal formStartfloat: =float64(float64(Starthour) +float64(Startminute)/60)//In decimal form off-hoursEndfloat: =0.0 //Since 5:30 to 6:00 is the meal time, does not count into the work hours, therefore must divide the situation to deal with ifEndhour < -{//Between 5:30 P.M. to 6 o ' clock, equivalent to 17:30 off duty, that is 17.5Endfloat =17.5}Else{//6 hours after work, minus 30/60 hour .Endfloat =float64(float64(Endhour) +float64(Endminute)/60-0.5) }//minus 1.5 hours of noon break is the day of office hoursCurrentdayhour = Endfloat-startfloat-1.5Sumhour + = Currentdayhour//Pau a row of data into slice currentdayhourNewlines[index] = NewLine + StrConv. Formatfloat (Currentdayhour,' F ',2, -) index++} Average: = Sumhour/float64(index) Newlines[index] ="Average daily work Hours:"+ StrConv. Formatfloat (Average,' F ',2, -) +"Hours" //Save fileAfterfile, Aftererr: = OS. Create (*outfile)ifAftererr! =Nil{FMT. Println ("Failed to create output file")return}deferAfterfile.close ()//Turn the slice back to the output to make the time from small to large forI: =Len(newlines)-1; I >=0; i--{afterfile.writestring (Newlines[i] +"\ r \ n") }}