Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - unexpected conversion for [System.DateTime]

Reply
 
Old 10-29-2006   #1 (permalink)
OK


 
 

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 SpecsSystem Spec
Old 10-29-2006   #2 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Old 10-29-2006   #3 (permalink)
OK


 
 

RE: unexpected conversion for [System.DateTime]

Right! :-)
Stupid me!
Any ideas about time truncation in the next post?
OK

My System SpecsSystem Spec
Old 10-30-2006   #4 (permalink)
dreeschkind


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46