I have written a simple function to convert any UTC time to current UK time (depending if Daylight Saving Time is being applied at the current season the result is either the same UTC or UTC + 1):
function Convert-UTCToUKTime
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)] $UTCTime
)
$UKTime = (Get-Date -Date $UTCTime)
if ($UKTime.IsDaylightSavingTime() -eq $true)
{
$UKTime = $UKTime.AddHours(1)
}
return $UKTime
}
I am also using this in the different function to get current UK time and it works just fine:
function Get-UKTime
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)] [String] $Format
)
$UKTime = Convert-UTCToUKTime -Time ((Get-Date).ToUniversalTime())
return $UKTime.ToString($Format)
}
However, when I try to pass the converting function a file created time (which is of course in UTC), it fails to recognize daylight saving time and therefore returns the UK time value with one hour behind (I tried to pass the exact same time by using Get-Date
- there were no issues):
[System.IO.FileInfo] $FileInfo = $FullFileName
$FileCreatedTime = Convert-UTCToUKTime -UTCTime (($FileInfo.CreationTimeUTC)
I found the fix which helped me to get this working as I expect to (by converting the DateTime
type to String
before passing as a parameter):
$FileCreatedTime = Convert-UTCToUKTime -UTCTime (($FileInfo.CreationTimeUTC).ToString("yyyy/MM/dd hh:mm:ss"))
However, I am not really sure why this works. What is the difference here between using this with Get-Date
and passing File Created time as a parameter as they both have the same DateTime
type?
Any explanation would be really appreciated.