Powershell question: Getting ISO8601 date with offset into DateTime

Posted by: tfabris

Powershell question: Getting ISO8601 date with offset into DateTime - 29/11/2016 00:25

Code:
$oldestOccurenceTimeDate = Get-Date $oldestOccurrenceBuildResult.build.finishDate

OR

$oldestOccurenceTimeDate = [DateTime]::ParseExact($oldestOccurrenceBuildResult.build.finishDate,'yyyyMMddTHHmmssZZZ',$null)


(where the variable $oldestOccurrenceBuildResult.build.finishDate contains an ISO8601 string with a timezone offset baked in, which I retrieved from the Team City API, such as, "20161116T144230-0800")

These all result in "Error: "String was not recognized as a valid DateTime.""

Google search results have been sending me around in circles. There is an option for wmi for ConvertToDateTime, but that also is not working for me. But perhaps I have the syntax wrong.

Has anyone else already had to jump through this hoop?

This is very similar to the issue I was dealing with in PHP in this thread. However this is for a totally different project in a different language, and Powershell seems to be failing me here.

I could re-parse the entire string and rebuild it for the PowerShell Get-Date command, but I'd like to avoid doing that if possible.
Posted by: Roger

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 30/11/2016 09:41

http://stackoverflow.com/questions/3405335/parsing-a-teamcity-java-generated-timestamp-in-net
Posted by: tfabris

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 30/11/2016 19:10

I was about to type a reply which said "But I already tried that and it didn't work!"

But then I looked closer. My "ZZZ" needed to be changed to lower case. Then it worked.

GAAAAAAAAHHHHHH! smile

Thanks very much, Roger!
Posted by: tfabris

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 01/12/2016 00:45

Okay! Next question! smile

I need to split a string, which usually contains multiple lines, into an array, via a simple .Split() command in Powershell.

Easy enough. But...

I need to split on any/all of the following:
- A CRLF pair (0x0D followed by 0x0A)
- A LF only (0x0A)
- The literal string of ascii characters "\r\n" (i.e., not a CRLF but the actual ascii letters backslash, lowercase r, backslash, lowercase n)

The first two are easy in powershell:
Code:
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n")


That *seems* to work though I'm not 100 percent certain it's working as designed. I get output I like at least.

The last one is the problem. No matter what I type into the last Split command, it either doesn't split at all, or it splits on the letters r and n rather than the string "\r\n":
Code:
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n").Split('\r\n') 
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n").Split('\\r\\n') 
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n").Split('\\\r\\\n') 
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n").Split('\\\\r\\\\n') 
$stackTraceLines = $stackTraceToParse.Split("`r`n").Split("`n").Split('[\\r\\n]') 


Nothing is working. Does anyone know what is the correct syntax is here?

All my google searches are showing me how to split on CRLFs instead, because it's really hard to search on google for asking how to split on a LITERAL "\r\n".
Posted by: tfabris

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 01/12/2016 01:56

Okay, I've found the root of my problem.

It's not splitting on the full string, it's splitting on individual characters in the string.

There is a different syntax for splitting on full strings. This works...
Code:
$stackTraceLines = $stackTraceToParse -Split '\\r\\n'

... but then I don't get to combine it with other split methods on the same line that I can see. I don't know how to do the "chaining" of split commands with that syntax.
Posted by: tfabris

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 01/12/2016 02:01

Hm. This crazy syntax seems to work.

Code:
$stackTraceLines = $($($($stackTraceToParse -Split "`n") -Split "`r`n") -Split '\\r\\n' )
Posted by: tfabris

Re: Powershell question: Getting ISO8601 date with offset into DateTime - 01/12/2016 02:04

Actually, no, for it to work correctly I have to swap the positions of the CRLF and the LF splits in the sequence. Then it works correctly (I think).

Code:
$stackTraceLines = $($($($stackTraceToParse -Split "`r`n") -Split "`n") -Split '\\r\\n' )