A PowerShell Script To Calculate The Perfect Time For Lunch

Because I don’t always start at the same time I found it hard to remember at what time I came into the office. To fix that I wrote a Powershell script.

  • It retrieves the time at which I started up my computer. It starts looking for the first start-up after 5 in the morning, so I can reboot during the day without influencing the output of the script.
  • From this start-up time it calculates the perfect time for lunch and the end of my working day.
1
2
3
4
5
6
7
8
9
10
11
12
13
$hoursPerWeek = 36
$daysPerWeek = 5
$lunchBreakHours = 0.5

$startedAt = (Get-EventLog -LogName "System" -After (Get-Date).Date.AddHours(5) | Sort-Object -Property TimeGenerated | Select-Object -First 1).TimeGenerated
$finishedAt = (Get-Date($startedAt)).AddHours(($hoursPerWeek/$daysPerWeek) + $lunchBreakHours)
$timeRemaining = New-Timespan -Start (Get-Date) -End $finishedAt
$lunchAt = (Get-Date($startedAt)).AddHours($hoursPerWeek/10)

Write-Output ("Your computer started up at {0} on {1} (day {2} of the year)," -f $startedAt.ToShortTimeString(), (Get-Date).ToString("D"), (Get-Date).DayOfYear)
Write-Output ("you work {0} hours in {1} days and have a {2} minute lunchbreak ({3} hours per day)," -f $hoursPerWeek, $daysPerWeek, ($lunchBreakHours*60), (($hoursPerWeek/$daysPerWeek) + $lunchBreakHours))
Write-Output ("which means you're finished today at {0} (that's in {1}:{2})." -f $finishedAt.ToShortTimeString(), $timeRemaining.Hours, $timeRemaining.Minutes)
Write-Output "The perfect time for lunch is $($lunchAt.ToShortTimeString())."

Output:

1
2
3
4
Your computer started up at 09:01 on Thursday, November 22, 2018 (day 326 of the year),
you work 36 hours in 5 days and have a 30 minute lunchbreak (7.7 hours per day),
which means you're finished today at 16:43 (that's in 0:10).
The perfect time for lunch is 12:37.

You should change the $hoursPerWeek, $daysPerWeek and $lunchBreakHours for your personal situation. So if you work 40 hours in 4 days and have a one hour lunch you change it to this:

1
2
3
$hoursPerWeek = 40
$daysPerWeek = 4
$lunchBreakHours = 1

Output:

1
2
3
4
Your computer started up at 09:01 on Thursday, November 22, 2018 (day 326 of the year),
you work 40 hours in 4 days and have a 60 minute lunchbreak (11 hours per day),
which means you're finished today at 20:01 (that's in 3:20).
The perfect time for lunch is 13:01.