Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#367919 - 29/11/2016 00:25 Powershell question: Getting ISO8601 date with offset into DateTime
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
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.
_________________________
Tony Fabris

Top
#367922 - 30/11/2016 09:41 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: tfabris]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5680
Loc: London, UK
_________________________
-- roger

Top
#367926 - 30/11/2016 19:10 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: Roger]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
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!
_________________________
Tony Fabris

Top
#367931 - 01/12/2016 00:45 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
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".
_________________________
Tony Fabris

Top
#367932 - 01/12/2016 01:56 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
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.
_________________________
Tony Fabris

Top
#367933 - 01/12/2016 02:01 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
Hm. This crazy syntax seems to work.

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

Top
#367934 - 01/12/2016 02:04 Re: Powershell question: Getting ISO8601 date with offset into DateTime [Re: tfabris]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31565
Loc: Seattle, WA
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' )
_________________________
Tony Fabris

Top