[英]Format string to add leading zero in 24 hour time
我正在使用PowerShell的-match
運算符和正則表達式\\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\\d{2})
來獲取日期和時間a重新啟動應該發生。
樣本數據:
Patching - Prod - Fri 2:00
Patching - Prod - Fri 22:00
Patching - Prod - Thu 22:00
Patching - Prod - Fri 22:00
Patching - Prod - Sat 18:00
Patching - Prod - Sun 2:00
Patching - Prod - Sun 00:00
Patching - Prod - Sat 2:00
$Rebootinfo = "Patching - Prod - Sat 2:00"
"$Rebootinfo" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null
這很好用,但是我發現當時間是2:00 AM
我會獲得2:00
並且我希望將02:00
的前導零填充到該結果中,如果時間是午夜,則結果將是0
而不是所需的00:00
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null
$a = $Matches[2]
$a.ToString("00:00")
返回錯誤找不到“ ToString”的重載和參數計數:“ 1”。
我這樣做的目的是將數據傳遞到PowerShell中,以獲取直到該重啟時間為止的天數。 例如,如果在周六運行,則周日2 AM需要增加1天。
您不能在字符串上使用數字格式,因此您需要先將小時/分鍾轉換為int。 以下是一些示例:
#Convert 2:00 to 200 int-number and format it to 00:00-style -> 02:00.
#18:00 -> 1800 -> 18:00
"{0:00:00}" -f ([int]$a.Replace(":",""))
要么
#Capture hour and minutes in their own groups
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9])[:](\d{2})" | Out-Null
#Format 00 only works with digits, so convert to int
"{0:00}:{1:00}" -f [int]$Matches[2], [int]$Matches[3]
或者,您可以將其解析為DateTime並轉換回具有正確格式的字符串(如果需要,也可以使用DateTime對象)。
$date = [datetime]::ParseExact($Matches[0], "ddd H:mm", [cultureinfo]::InvariantCulture)
$date.ToString("ddd HH:mm", [cultureinfo]::InvariantCulture)
您可以這樣做:
"Prod - Sun 2:00" -match "\b(Thu|Fri|Sat|Sun).([012]?[0-9][:]\d{2})" | Out-Null $a = $Matches[2] $ts = [TimeSpan]::Parse($a) $formatted = $ts.ToString("c").Substring(0, 5) $formatted
它為$formatted
輸出02:00
。
您的帖子似乎沒有問題(目標,但沒有問題)。 因此,我猜您的問題是“為什么正則表達式不返回'02:00AM'”?
由於源字符串在2之前不包含零,因此您將無法獲得源字符串中不包含零的匹配項。 您需要將其添加為單獨的步驟。
通過使用.NET的內置日期時間解析,可以避免一些麻煩: [datetime]::parseexact
。 不幸的是,如果星期日不是當天,ParseExact無法處理諸如“ Sun 2:00 AM”之類的字符串,因此需要一些額外的工作。 這是一些相同的示例代碼。
$Rebootinfo = "Patching - Prod - Thu 2:00AM"
$splitUp = $Rebootinfo -split "\b(Thu|Fri|Sat|Sun)"
# $splitUp[-1] now contains time and $splitUp[-2] contains day of week
$cult = [Globalization.CultureInfo]::InvariantCulture
try {
$rebootTime = [datetime]::parseexact( $splitUp[-1], " h:mmtt", $cult)
} catch {
# put your own error handling here
throw "Date time parse failed"
}
$weekDayToWeekDayNumber = @{Sun=0;Mon=1;Tue=2;Wed=3;Thu=4;Fri=5;Sat=6}
$rebootWeekDayNumber = $weekDayToWeekDayNumber[$splitUp[-2]]
$todayWeekDayNumber = $weekDayToWeekDayNumber[[datetime]::today.DayOfWeek.tostring().substring(0,3)]
# This calculation fails if the reboot day of the week is same as current
# day of week and reboot time is before current time. However I'm guessing
# this won't be a problem because if this
# happens you've already missed the boot or the boot is almost a week away.
# Assuming the later: since you only have days of the week (and not dates)
# I'm guessing that
# boots almost a week away aren't a concern. The reason is that if you
# handle boots almost a
# week away, there's no guarantee (that I see) that the boot won't be a
# little more than a week away (since you don't know exactly when the boot
# is, hence the script). And if boots can be more than a week away you won't
# be able to distinguish between boots this week and boots next week (since
# you only have the day of the week).
# However if this is a problem, just compare $rebootTime to [datetime]::now
# and if less, then add 7 more days to $rebootTime.
$rebootTime = $rebootTime.AddDays( ($rebootWeekDayNumber - $todayWeekDayNumber + 7) % 7)
write-host Amount of time till reboot ($rebootTime - [datetime]::now)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.