Computer scienceProgramming languagesGolangPackages and modulesStandard libraryTime package

Parsing and formatting time

Working hours

Report a typo

You work for a company (imagine any you'd like to work for). Before the next vacation, you want to check what salary you have received for the current working period. You have the dates when the period starts and ends, as well as information about the working time per day and the salary per hour.

The dates are input in the format of year-month-day. The salary and the working time are integer numbers without any other symbols. You don't work on weekends and Wednesdays. And on Fridays, you work one hour less than on other days! Now you need to find out the total amount for a given period.

The main() function of your program should take the start and end date strings as input and then parse them to the proper date format. Then, the calculateSalary() function should calculate and print the totalSalary based on the total workingTime and the salaryPerHour.

Sample Input 1:

2019-05-01 2019-12-12
120 6

Sample Output 1:

Total: 88320
Write a program in Go
package main

import (
"fmt"
"log"
"time"
)

const (
HoursPerDay = 24
DateLayout = "2006-01-02"
)

func main() {
var startDateString, endDateString string
var salaryPerHour, workingTime uint64

fmt.Scan(&startDateString, ?) // Read the start date and the end date
fmt.Scan(?, ?) // Read the salary per hour and the working time

startDate, err := time.Parse(DateLayout, startDateString)
if err != nil {
log.Fatal(err)
}

endDate, err := time.Parse(?, ?) // Parse the end date of the period
if err != nil {
log.Fatal(err)
}

// The calculateSalary() function outputs the total salary for the given period:
calculateSalary(startDate, endDate, salaryPerHour, workingTime)
}

// Write the required code to finish the implementation of the calculateSalary() function:
func calculateSalary(startDate time.Time, endDate time.Time, salaryPerHour uint64, workingTime uint64) {
var totalSalary uint64

for !startDate.Equal(endDate) {
weekday := startDate.?() // What function returns the day of the week?
startDate = startDate.Add(time.Hour * HoursPerDay)

salaryPerDay := salaryPerHour * ? // Calculate the salary per day

if weekday == time.Wednesday || weekday == time.? || weekday == time.? {
continue
}

if weekday == time.Friday {
salaryPerDay -= salaryPerHour
}

totalSalary += ? // Add the daily salary to the total salary
}
fmt.Println("Total:", totalSalary)
}
___

Create a free account to access the full topic