The Time Formatting Rabbit hole

This past weekend I decided to go down the rabbit hole for a few hours on time formatting for different timezones. I thought I would dig deeper and do the same for Windows, Mac and Linux.

I work with a lot of log files and trying to remember the date in UTC ( or others) and convert in your head gets tiresome when you have logs from different systems that you are trying to correlate.  

Lets say you are working in the console and want to quickly know what the date is in some other timezone, these are the commands to do that on Linux, MacOS and Windows Powershell

Query the list of Timezones

Note: Using London England here

On Linux ( for London)

timedatectl list-timezones | grep London

On MacOS

sudo systemsetup -listtimezones | grep London

On Windows Powershell

(Get-TimeZone -Name "*GMT*").Id 

To use the above to show the current date and time in the place you queried from in your terminal

Current time in London and give me the date in 24hour format

On Linux

TZ=$(timedatectl list-timezones | grep London) date +"%a %b %d %Y  %T"

Same with 12 hour time format

TZ=$(timedatectl list-timezones | grep London) date +"%a %b %d %Y %I:%M:%S") 

On MacOS 

Note: you have to know the Zone ( can’t query easily in the terminal without sudo – see above)

echo $(TZ="Europe/London" date +"%a %b %d %Y  %T")

On Powershell you have to know the Zone (  “Get-TimeZone -ListAvailable”  to see the list )

[datetime]::UtcNow, (Get-TimeZone -Name "*GMT*").Id 

To create an Alias in your current terminal ( not persistent)! 

( I am using “gt” as Great Britain Timezone here)

Current time in London – 24 hour time

alias gt='echo $(TZ=$(timedatectl list-timezones | grep London) date +"%a %b %d %Y  %T")'

Same with 12 hour time format

alias gt='echo $(TZ=$(timedatectl list-timezones | grep London) date +"%a %b %d %Y %I:%M:%S")’

On MacOS 

alias gt='echo $(TZ="Europe/London" date +"%a %b %d %Y  %T")'

On Powershell its Two Lines

Function WD {Write-Host ([datetime]::UtcNow, (Get-TimeZone -Name "*GMT*").Id)}
Set-Alias gt WD

Finally – If you want to make this persistent on your OS by putting an Alias in your profile

Note:  I am using ~/.zshrc here but you can also use ~/.profile or ~/.bash_profile depending on where your current settings are stored.

Notice all the “\” backslashes so that you can echo “’s and $’s

For Linux

echo "alias gt='echo \$(TZ=\$(timedatectl list-timezones | grep London) date +\"%a %b %d %Y  %T\")'" >> ~/.zshrc

For MacOS

echo "alias gt='echo \$(TZ=\”Europe/London\” date +\"%a %b %d %Y  %T\")'" >> ~/.zshrc

For Powershell:

For your Persistent Profile in Powershell its quite complicated:

You need to decide whether only your username will use the profile

  • If only your user:
    • whether you want it while just running a Powershell terminal ($myProfile) 
    • or also when you are in the ISE editor ($myISEProfile)

Note: the default I have shown below is for both…

If you want all users to use the profile:

  • Then decide:
    • whether you want it while just running a Powershell terminal ($globalProfile64)
    • or also when you are in the ISE editor as well ($globalISEProfile64)
  • And finally whether you want the 32-bit Powershell profile available:
    • in Powershell32 ($globalProfile32) 
    • in ISE editor on Powershell32 ($globalISEProfile32)

I have tried to make it easy enough to set for your user by default.

You just copy and paste the following and run it.

Note: You need to modify $profiles if you want to set it globally (bottom of the script)

#You need this folder anyway
md $HOME\Documents\WindowsPowershell -ErrorAction SilentlyContinue

#If you want to execute in powershell windows for you only
$myProfile="$HOME\Documents\WindowsPowershell\Microsoft.PowerShell_profile.ps1"

#If you want to execute in powershell ISE windows for you only
$myISEProfile="$HOME\Documents\WindowsPowershell\Microsoft.PowerShellISE_profile.ps1"

#If you want to execute in powershell 64-bit windows for All Users
$globalProfile64="C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1"

#If you want to execute in powershell ISE 64-bit windows for All Users
$globalISEProfile64="C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1"

# This is weird but but the 32-bit profile is in the syswow64 folder
#If you want to execute in powershell 32-bit windows for All Users
$globalProfile32="C:\Windows\SysWow64\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1"

#If you want to execute in powershell ISE 64-bit windows for All Users
$globalISEProfile32="C:\Windows\SysWow64\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1"

#Change the $profiles here to whatever you want to set from above
$profiles=@($myProfile,$myISEProfile)
$profiles | ForEach-Object -Process {
Add-Content -Path $_ -Value 'Function WD {Write-Host ([datetime]::UtcNow, (Get-TimeZone -Name "*GMT*").Id)}'
Add-Content -Path $_ -Value 'Set-Alias gt WD'
} 

I hope this helps you out. Drop me a line or comment if there are mistakes obviously.

Leon

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.