Created
October 14, 2015 06:08
-
-
Save ishisaka/bc7076aa83ef37b4f832 to your computer and use it in GitHub Desktop.
.NET(C#)のDateTime/DateTimeOffsetとUNIX時間との相互変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [Program.cs] | |
// Copyright (C) 2015 Tadahiro Ishisaka All rights reserved. | |
// This software is released under the MIT License. | |
using System; | |
namespace DateTimeSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//現在時刻を取得するよ | |
Console.WriteLine("現在時刻"); | |
var _now = DateTime.Now; | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now); | |
//現在時刻からTimeStampを取得するよ | |
Console.WriteLine("現在時刻をタイムスタンプにする"); | |
//1970/1/1からの経過時間をTicksプロパティを使い100ns単位で計算し、秒数に変換する。 | |
uint _nowTimestamp = (uint)((DateTime.UtcNow.Ticks - DateTime.Parse("1970-01-01 00:00:00").Ticks) / 10000000); | |
Console.WriteLine("TIMESTAMP = {0}", _nowTimestamp); | |
//TimeStampから時刻に戻すよ | |
Console.WriteLine("タイムスタンプを現在時刻に戻すよ"); | |
var unixOriginTime = new DateTime(1970, 1, 1, 0, 0, 0); | |
var _now2 = unixOriginTime.AddSeconds(_nowTimestamp).ToLocalTime(); | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now2); | |
// これ以下は.NET 4.0迄の方法 | |
Console.WriteLine("\nおまけご要望によりDateTimeOffsetを使ってみる(が、たいして変わらないよ)"); | |
Console.WriteLine("現在時刻"); | |
var _now3 = DateTimeOffset.Now; | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now); | |
//現在時刻からTimeStampを取得するよ | |
Console.WriteLine("現在時刻をタイムスタンプにする"); | |
//1970/1/1からの経過時間をTicksプロパティを使い100ns単位で計算し、秒数に変換する。 | |
uint _nowTimestamp2 = (uint)((DateTimeOffset.UtcNow.Ticks - DateTimeOffset.Parse("1970-01-01 00:00:00").Ticks) / 10000000); | |
Console.WriteLine("TIMESTAMP = {0}", _nowTimestamp2); | |
//TimeStampから時刻に戻すよ | |
Console.WriteLine("タイムスタンプを現在時刻に戻すよ"); | |
var unixOriginTime2 = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan(0)); //ここだけちょっと違う感じに。UTCなのでオフセット(時差)は0 | |
var _now4 = unixOriginTime2.AddSeconds(_nowTimestamp).ToLocalTime(); | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now4); | |
// これ以下は.NET 4.6からの方法 | |
Console.WriteLine("\nおまけご要望によりDateTimeOffsetを使ってみる(.NET 4.6)"); | |
Console.WriteLine("現在時刻"); | |
var _now5 = DateTimeOffset.Now; | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now); | |
//現在時刻からTimeStampを取得するよ | |
Console.WriteLine("現在時刻をタイムスタンプにする"); | |
long _nowTimeStamp3 = _now5.ToUnixTimeSeconds(); | |
Console.WriteLine("TIMESTAMP = {0}", _nowTimeStamp3); | |
//TimeStampから時刻に戻すよ | |
Console.WriteLine("タイムスタンプを現在時刻に戻すよ"); | |
var _now6 = DateTimeOffset.FromUnixTimeSeconds(_nowTimeStamp3).ToLocalTime(); | |
Console.WriteLine("DATETIME = {0:yyyy-MM-dd hh:mm:ss}", _now6); | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment