Improve check-midnight.ps1, check-noon.ps1, and check-tea-time.ps1

This commit is contained in:
Markus Fleschutz 2021-12-08 15:50:22 +01:00
parent 394cab03a1
commit 3868e56a70
3 changed files with 48 additions and 15 deletions

View File

@ -1,6 +1,6 @@
<#
.SYNOPSIS
Checks the time until Midnight
Checks for Midnight
.DESCRIPTION
This script checks the time until Midnight and replies by text-to-speech (TTS).
.EXAMPLE
@ -11,16 +11,27 @@
https://github.com/fleschutz/PowerShell
#>
function TimeSpanToString { param([TimeSpan]$Delta)
$Result = ""
if ($Delta.Hours -eq 1) { $Result += "1 hour, "
} elseif ($Delta.Hours -gt 1) { $Result += "$($Delta.Hours) hours, "
}
if ($Delta.Minutes -eq 1) { $Result += "1 minute"
} else { $Result += "$($Delta.Minutes) minutes"
}
return $Result
}
try {
$Now = [DateTime]::Now
if ($Now.Hour -lt 12) {
$Midnight = Get-Date -Hour 0 -Minute 0 -Second 0
$Delta = $Now - $Midnight
$Reply = "Midnight was $($Delta.Hours) hours, $($Delta.Minutes) minutes ago."
$TimeSpan = TimeSpanToString($Now - $Midnight)
$Reply = "Midnight was $TimeSpan ago."
} else {
$Midnight = Get-Date -Hour 23 -Minute 59 -Second 59
$Delta = $Midnight - $Now
$Reply = "Midnight is in $($Delta.Hours) hours, $($Delta.Minutes) minutes."
$TimeSpan = TimeSpanToString($Midnight - $Now)
$Reply = "Midnight is in $TimeSpan."
}
& "$PSScriptRoot/give-reply.ps1" "$Reply"
exit 0 # success

View File

@ -1,6 +1,6 @@
<#
.SYNOPSIS
Checks the time until Noon
Checks for Noon
.DESCRIPTION
This script checks the time until Noon and replies by text-to-speech (TTS).
.EXAMPLE
@ -11,15 +11,26 @@
https://github.com/fleschutz/PowerShell
#>
function TimeSpanToString { param([TimeSpan]$Delta)
$Result = ""
if ($Delta.Hours -eq 1) { $Result += "1 hour, "
} elseif ($Delta.Hours -gt 1) { $Result += "$($Delta.Hours) hours, "
}
if ($Delta.Minutes -eq 1) { $Result += "1 minute"
} else { $Result += "$($Delta.Minutes) minutes"
}
return $Result
}
try {
$Now = [DateTime]::Now
$Noon = Get-Date -Hour 12 -Minute 0 -Second 0
if ($Now -lt $Noon) {
$Delta = $Noon - $Now
$Reply = "Noon is in $($Delta.Hours) hours, $($Delta.Minutes) minutes."
$TimeSpan = TimeSpanToString($Noon - $Now)
$Reply = "Noon is in $TimeSpan."
} else {
$Delta = $Now - $Noon
$Reply = "Noon was $($Delta.Hours) hours, $($Delta.Minutes) minutes ago."
$TimeSpan = TimeSpanToString($Now - $Noon)
$Reply = "Noon was $TimeSpan ago."
}
& "$PSScriptRoot/give-reply.ps1" "$Reply"
exit 0 # success

View File

@ -1,6 +1,6 @@
<#
.SYNOPSIS
Checks the time until Tea time
Checks for Tea time
.DESCRIPTION
This script checks the time until Tea time and replies by text-to-speech (TTS).
.EXAMPLE
@ -11,15 +11,26 @@
https://github.com/fleschutz/PowerShell
#>
function TimeSpanToString { param([TimeSpan]$Delta)
$Result = ""
if ($Delta.Hours -eq 1) { $Result += "1 hour, "
} elseif ($Delta.Hours -gt 1) { $Result += "$($Delta.Hours) hours, "
}
if ($Delta.Minutes -eq 1) { $Result += "1 minute"
} else { $Result += "$($Delta.Minutes) minutes"
}
return $Result
}
try {
$Now = [DateTime]::Now
$TeaTime = Get-Date -Hour 16 -Minute 0 -Second 0
if ($Now -lt $TeaTime) {
$Delta = $TeaTime - $Now
$Reply = "Tea time is in $($Delta.Hours) hours, $($Delta.Minutes) minutes."
$TimeSpan = TimeSpanToString($TeaTime - $Now)
$Reply = "Tea time is in $TimeSpan."
} else {
$Delta = $Now - $TeaTime
$Reply = "Tea time was $($Delta.Hours) hours, $($Delta.Minutes) minutes ago."
$TimeSpan = TimeSpanToString($Now - $TeaTime)
$Reply = "Tea time was $TimeSpan ago."
}
& "$PSScriptRoot/give-reply.ps1" "$Reply"
exit 0 # success