Ad

The checkWorkHours(dateTime time.Time) function accepts a time.Time object and returns true or false if the time given is within work hours.

Includes a function to test the outputs of your function.

package main

import (
	"fmt"
	"time"
)

func main() {
	testCheckWorkHours()
}

func checkWorkHours(dateTime time.Time) bool {
	beginWorkHour := 8
	endWorkHour := 18

	currentHour := dateTime.Hour()
	currentDay := dateTime.Weekday()

	if currentDay > 0 && currentDay < 6 {
		if currentHour < endWorkHour && currentHour >= beginWorkHour {
			return true
		}
	}

	return false
}

func testCheckWorkHours() {
	// Tests the function
	timeFormat := "2006-01-02 15:04:05 -0700 MST"

	// True Cases
	dateTime, _ := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Monday @ 3pm should be True: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")
	fmt.Println(fmt.Sprintf("Friday @ 5:59pm should be True: %t", checkWorkHours(dateTime)))

	// False Cases
	dateTime, _ = time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Sunday @ 3pm should be False: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Friday @ 6pm should be False: %t", checkWorkHours(dateTime)))

	dateTime, _ = time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")
	fmt.Println(fmt.Sprintf("Tuesday @ 7am should be False: %t", checkWorkHours(dateTime)))
}
Code
Diff
  • package main
    
    import "fmt"
    
    func main() {
        age := 28 * 365 / 687
        fmt.Print(fmt.Sprintf("My age on the surface of Mars is %d years old.", age))
    }
    • package main
    • import "fmt"
    • func main() {
    • fmt.Print("My age on the surface of Mars is ")
    • fmt.Print(28 * 365 / 687)
    • fmt.Print(" years old.")
    • age := 28 * 365 / 687
    • fmt.Print(fmt.Sprintf("My age on the surface of Mars is %d years old.", age))
    • }