tech.guitarrapc.cóm

Technical updates

WindowsのTimeZone設定をコマンドラインから行う

WindowsのTimeZone設定といえばコマンドプロンプト以外にもPowerShellや .NETでもできます。

Windows 10でどのようなやり方があるのか軽く見てみましょう。

各種方法

ざっくり見てみます。

コマンドプロンプト

TZutilが安定でしょうか。

https://msdn.microsoft.com/ja-jp/library/hh825053.aspx

書式が相変わらずめんどくさいです。

TZUTIL </? | /g | /l | /s TimeZoneValue[_dstoff]>

今のタイムゾーンを取得したり、タイムゾーンの一覧取得、設定が可能です。

https://gist.github.com/guitarrapc/c605e64f943a5a0c52848f30014d8590

PowerShell

Powershell 5.0からMicrosoft.PowerShell.ManagementモジュールにTimezoneに関するCmdletが2つ追加されました。GetとSetです。

PS > man Get-TimeZone

NAME
    Get-TimeZone

SYNTAX
    Get-TimeZone [[-Name] <string[]>]  [<CommonParameters>]

    Get-TimeZone -Id <string[]>  [<CommonParameters>]

    Get-TimeZone -ListAvailable  [<CommonParameters>]


ALIASES
    gtz


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Get-TimeZone -Online" or
           go to https://go.microsoft.com/fwlink/?LinkId=799468.

使い方も簡単です。Get-TimeZoneで今のタイムゾーン取得、Get-TimeZoneで一覧取得、Get-TimeZoneで設定します。

https://gist.github.com/guitarrapc/8ac01f75567d625cd1135003040cab07

ちなみにPowerShell DSCでも可能です。まだリリースされてませんが、1.6でWMF5.1やNano Server向けにTimeZone Cmdletを使うように分岐が入ります。

https://github.com/PowerShell/xTimeZone

https://gist.github.com/guitarrapc/ba2fe8a7162453e61705f91e39c5772b

.NET (C#)

当然C# でも操作できます。 Setがだるいので、コマンドをラップしてもいいですね。

https://gist.github.com/guitarrapc/15f04fa68e5766f1dfff681adb3cc640

WSL (Bash on Ubuntu on Windows)

WSLからUbuntuでやるようにはSETがちょっとうまくいかないのでどうすればいいですかねぇ。

手元のbashでsudo壊れて直してないので試してませんが、ln置き換えでいい気がします。

https://gist.github.com/guitarrapc/2aa05cb05a9f57a4d79b0c77b9df74e7

2017/6/21 追記

記事を見た先輩から教えていただきました。

Ubuntuのヘルプ通り、本来ならこれで行けます

https://help.Ubuntu.com/community/UbuntuTime#Using_the_Command_Line_.28unattended.29

https://gist.github.com/guitarrapc/87a1331aacacbcf6bdf82e5dd5b2b6d5

しかし、現在バグがあらいます。

https://bugs.launchpad.net/Ubuntu/+source/tzdata/+bug/1554806

修正までは以下でいいようです。

https://gist.github.com/guitarrapc/53848bfe9e650c4ee0337042dec091fb

まとめ

どれを使ってもいいでしょう。この程度だとPowerShellが一番楽、かな?