Your question is not PowerShell question, but a .NET question. PowerShell scripts can use the .NET structure [datetime] as it is, i.e., PowerShell does not change its behavior.
What are the rules for casting an object to a string, if not calling ToString()?
Casting uses culture invariant definitions. The ToString()
method can contain culture dependent implementations, because it is overridable.
Is the DateTime class simply a special case because of its overloading of ToString(String)?
First, DateTime is not a class; it's a structure.
Second, there's no ToString()
method overloading; in this case, the correct denomination is overriding (it is overriding the Object.ToString()
method).
To understand better what I'm meaning, have fun with these pretty funny date and time printing in different cultures (copy, paste and run):
function f{
$x=get-date
[CultureInfo]$currentCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('en-US')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ar-IQ')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('de-DE')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ru-RU')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('fr-FR')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-CN')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-HK')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-TW')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('hu-HU')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ko-KR')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ja-JP')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ka-GE')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('pt-BR')
$x.ToString()
[System.Threading.Thread]::CurrentThread.CurrentCulture=$currentCulture
}
f
Notice that the code above will produce different printing if run either in ISE or in non-ISE versions of PowerShell.