LoginSignup
11
11

More than 5 years have passed since last update.

PowershellでUnix時刻とDateTimeの相互変換

Last updated at Posted at 2016-04-04

ぐぐってみて案外日本語の情報がないので、自分の防備録のためにもQiita投稿しておきます。
1970年1月1日 0時0分0秒のEpoch時刻から引き算足し算すればいいだけってのだけ理解してれば簡単に作れます。

Powershellの関数であれば、ログ解析なんかにも組み込めると思うのでどうぞご利用ください。

function Convert-UnixTimeToDateTime($unixTime){
   $origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
   $origin.AddSeconds($unixTime)
}
function Convert-DateTimeToUnixTime($dateTime){
   $origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
   [Int]($dateTime-$origin).TotalSeconds
}

UTC云々の部分はあえて省いてます。offsetをどうするか、という問題でしかないので、
それはこの関数を通す前後で処理しておいてください。
この考えが間違っていたら指摘をお願いします。

[参考]
Convert Unix time with PowerShell
http://stackoverflow.com/questions/10781697/convert-unix-time-with-powershell

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11