![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | unexpected conversion for [System.DateTime] Guys, I run some asynchronous scripts and need to measure their timing: I run into following “unexpected” conversion, below is a small script to demonstrate it. The question is: Why [System.DateTime]::Now gets converted to [String] type when passed as parameter, while a variable of [System.DateTime] type is not? Doesn’t is look like an inconsistent behavior? #------------------------------------------------------------------------------ function test([DateTime] $a) { Start-Sleep -milli 200 [DateTime] $private:b = [System.DateTime]::Now Start-Sleep -milli 200 @" A started $a. A elapsed time $($b - $a)<br> B started $b. B elapsed time $([System.DateTime]::Now - $b)<br> Total elapsed time $([System.DateTime]::Now - $a)<br> "@ } #------------------------------------------------------------------------------ function test2($a) { $a.GetType() } #------------------------------------------------------------------------------ #-- Main script --------------------------------------------------------------- #------------------------------------------------------------------------------ # Works fine [DateTime] $private:x = [System.DateTime]::Now test $x test2 $x # Doesn't work test [System.DateTime]::Now test2 [System.DateTime]::Now return #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ #------------------------------------------------------------------------------ > ./g A started 10/29/2006 13:24:24. A elapsed time 00:00:00.2031224<br> B started 10/29/2006 13:24:24. B elapsed time 00:00:00.2031224<br> Total elapsed time 00:00:00.4062448<br> IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DateTime System.ValueType test : Cannot convert value "[System.DateTime]::Now" to type "System.DateTime". Error: "String was not recognized as a valid DateTime." At .\g.ps1:37 char:5 + test <<<< [System.DateTime]::Now True True String System.Object |
My System Specs![]() |
| | #2 (permalink) |
| | RE: unexpected conversion for [System.DateTime] There is no unexpected conversion, because there is no conversion! :-) The whole parameter "[System.DateTime]::Now" is simply _interpreted_ as a string. Then, converting the _String_ "[System.DateTime]::Now" into a DateTime will of course fail. function testf ($p) {$p; $p.GetType()} PS> testf [System.DateTime]::Now [System.DateTime]::Now IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object As you can see, the parameter gets is of type String. Therefore you should to put your parameter inside $( ). This will calculate the value before passing it as a parameter: PS> test $([System.DateTime]::Now) A started 10/29/2006 20:22:59. A elapsed time 00:00:00.2031250<br> B started 10/29/2006 20:23:00. B elapsed time 00:00:00.2031250<br> Total elapsed time 00:00:00.4062500<br> PS> test2 $([System.DateTime]::Now) IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DateTime System.ValueType -- greetings dreeschkind "OK" wrote: > Guys, > I run some asynchronous scripts and need to measure their timing: > I run into following “unexpected” conversion, below is a small script to > demonstrate it. > > The question is: > Why [System.DateTime]::Now gets converted to [String] type when passed as > parameter, while a variable of [System.DateTime] type is not? > Doesn’t is look like an inconsistent behavior? > > #------------------------------------------------------------------------------ > function test([DateTime] $a) { > > Start-Sleep -milli 200 > [DateTime] $private:b = [System.DateTime]::Now > Start-Sleep -milli 200 > > > @" > A started $a. A elapsed time $($b - $a)<br> > B started $b. B elapsed time $([System.DateTime]::Now - $b)<br> > Total elapsed time $([System.DateTime]::Now - $a)<br> > "@ > > } > > > #------------------------------------------------------------------------------ > function test2($a) { > $a.GetType() > } > > > #------------------------------------------------------------------------------ > #-- Main script > --------------------------------------------------------------- > #------------------------------------------------------------------------------ > > > # Works fine > [DateTime] $private:x = [System.DateTime]::Now > test $x > test2 $x > > > # Doesn't work > test [System.DateTime]::Now > test2 [System.DateTime]::Now > > return > > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > #------------------------------------------------------------------------------ > > > ./g > A started 10/29/2006 13:24:24. A elapsed time 00:00:00.2031224<br> > B started 10/29/2006 13:24:24. B elapsed time 00:00:00.2031224<br> > Total elapsed time 00:00:00.4062448<br> > > IsPublic IsSerial Name BaseType > -------- -------- ---- -------- > True True DateTime System.ValueType > test : Cannot convert value "[System.DateTime]::Now" to type > "System.DateTime". Error: "String was not recognized as a valid DateTime." > At .\g.ps1:37 char:5 > + test <<<< [System.DateTime]::Now > True True String System.Object > > |
My System Specs![]() |
| | #3 (permalink) |
| | RE: unexpected conversion for [System.DateTime] Right! :-) Stupid me! Any ideas about time truncation in the next post? OK |
My System Specs![]() |
| | #4 (permalink) |
| | RE: unexpected conversion for [System.DateTime] "OK" wrote: > Any ideas about time truncation in the next post? No, sorry, I could not reproduce your results at all. You should probably check your posted scripts again. -- greetings dreeschkind |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Unexpected system restore | General Discussion | |||
| Unexpected shut down of the guest operating system. | Virtual Server | |||
| System Restore Unexpected Error 8007007B | Vista General | |||
| bug in string<->datetime conversion? | PowerShell | |||
| unexpected truncation for [System.DateTime] | PowerShell | |||