[C言語] ミリ秒を日時に変換する方法
C言語でミリ秒を日時に変換するには、まずミリ秒を秒に変換し、次に標準ライブラリのtime_t型を使用して日時を計算します。
この際、time_t型は秒単位で時間を表現するため、ミリ秒を1000で割って秒に変換します。
その後、gmtime()やlocaltime()関数を用いてstruct tm型に変換し、strftime()関数を使ってフォーマットされた日時文字列を取得します。
これにより、ミリ秒から人間が理解できる日時形式に変換することが可能です。
ミリ秒を日時に変換する手順
C言語でミリ秒を日時に変換するには、いくつかのステップを踏む必要があります。
以下では、その手順を詳しく解説します。
ミリ秒から秒への変換
まず、ミリ秒を秒に変換します。
ミリ秒は1秒の1/1000であるため、ミリ秒を1000で割ることで秒に変換できます。
#include <stdio.h>
int main() {
    long milliseconds = 1609459200000; // 例:2021年1月1日0時0分0秒のミリ秒
    long seconds = milliseconds / 1000; // ミリ秒を秒に変換
    printf("秒: %ld\n", seconds);
    return 0;
}秒: 1609459200このコードは、指定したミリ秒を秒に変換し、結果を表示します。
time_t型への変換
次に、秒をtime_t型に変換します。
time_tは、C言語で時間を扱うための標準的なデータ型です。
#include <stdio.h>
#include <time.h>
int main() {
    long seconds = 1609459200; // 先ほどの秒
    time_t timeValue = (time_t)seconds; // 秒をtime_t型に変換
    printf("time_t型の値: %ld\n", (long)timeValue);
    return 0;
}time_t型の値: 1609459200このコードは、秒をtime_t型に変換し、その値を表示します。
struct tmへの変換
time_t型の値をstruct tmに変換することで、年、月、日、時、分、秒といった詳細な日時情報を取得できます。
localtime関数を使用します。
#include <stdio.h>
#include <time.h>
int main() {
    time_t timeValue = 1719459200;               // time_t型の値
    struct tm *timeInfo = localtime(&timeValue); // time_tをstruct tmに変換
    printf("年: %d\n", timeInfo->tm_year + 1900);
    printf("月: %d\n", timeInfo->tm_mon + 1);
    printf("日: %d\n", timeInfo->tm_mday);
    printf("時: %d\n", timeInfo->tm_hour);
    printf("分: %d\n", timeInfo->tm_min);
    printf("秒: %d\n", timeInfo->tm_sec);
    return 0;
}年: 2024
月: 6
日: 27
時: 12
分: 33
秒: 20このコードは、time_t型の値をstruct tmに変換し、各日時要素を表示します。
日時のフォーマット化
最後に、strftime関数を使用して、struct tmの情報をフォーマット化された文字列として出力します。
#include <stdio.h>
#include <time.h>
int main() {
    time_t timeValue = 1719459200;               // time_t型の値
    struct tm *timeInfo = localtime(&timeValue); // time_tをstruct tmに変換
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S",
             timeInfo); // 日時をフォーマット化
    printf("フォーマット化された日時: %s\n", buffer);
    return 0;
}フォーマット化された日時: 2024-06-27 12:33:20このコードは、struct tmの情報を指定したフォーマットで文字列に変換し、表示します。
strftimeを使うことで、様々なフォーマットで日時を出力することが可能です。
応用例
ミリ秒を日時に変換する技術は、さまざまな場面で応用できます。
以下に、具体的な応用例を紹介します。
タイムスタンプの解析
タイムスタンプは、データの記録時刻を示すために使用されます。
これを解析することで、データの発生時刻を人間が理解できる形式に変換できます。
#include <stdio.h>
#include <time.h>
void parseTimestamp(long milliseconds) {
    time_t seconds = milliseconds / 1000; // ミリ秒を秒に変換
    struct tm *timeInfo = localtime(&seconds); // 秒をstruct tmに変換
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); // 日時をフォーマット化
    printf("解析されたタイムスタンプ: %s\n", buffer);
}
int main() {
    long timestamp = 1609459200000; // 例:タイムスタンプ
    parseTimestamp(timestamp);
    return 0;
}解析されたタイムスタンプ: 2021-01-01 00:00:00このコードは、タイムスタンプを解析し、フォーマット化された日時として出力します。
ログファイルの日時変換
ログファイルには、通常、イベントの発生時刻が記録されています。
これを人間が読みやすい形式に変換することで、ログの解析が容易になります。
#include <stdio.h>
#include <time.h>
void convertLogTimestamp(long milliseconds) {
    time_t seconds = milliseconds / 1000; // ミリ秒を秒に変換
    struct tm *timeInfo = localtime(&seconds); // 秒をstruct tmに変換
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); // 日時をフォーマット化
    printf("ログの日時: %s\n", buffer);
}
int main() {
    long logTimestamp = 1612137600000; // 例:ログファイルのタイムスタンプ
    convertLogTimestamp(logTimestamp);
    return 0;
}ログの日時: 2021-02-01 00:00:00このコードは、ログファイルのタイムスタンプを解析し、フォーマット化された日時として出力します。
時間差の計算
2つのタイムスタンプ間の時間差を計算することで、イベント間の経過時間を求めることができます。
#include <stdio.h>
#include <time.h>
void calculateTimeDifference(long startMilliseconds, long endMilliseconds) {
    time_t startSeconds = startMilliseconds / 1000; // 開始時刻のミリ秒を秒に変換
    time_t endSeconds = endMilliseconds / 1000; // 終了時刻のミリ秒を秒に変換
    double difference = difftime(endSeconds, startSeconds); // 時間差を計算
    printf("時間差: %.0f秒\n", difference);
}
int main() {
    long startTimestamp = 1609459200000; // 例:開始時刻のタイムスタンプ
    long endTimestamp = 1609462800000; // 例:終了時刻のタイムスタンプ
    calculateTimeDifference(startTimestamp, endTimestamp);
    return 0;
}時間差: 3600秒このコードは、2つのタイムスタンプ間の時間差を計算し、秒単位で出力します。
これにより、イベント間の経過時間を簡単に把握できます。
まとめ
ミリ秒を日時に変換する方法は、C言語を用いて簡単に実現できます。
この記事では、ミリ秒を秒に変換し、time_t型やstruct tmを用いて日時を取得する手順を解説しました。
また、応用例としてタイムスタンプの解析やログファイルの日時変換、時間差の計算についても紹介しました。
これらの知識を活用して、時間に関するプログラムをより効果的に開発してみてください。
 
![[C言語] 年月日から曜日を求める方法を解説](https://af-e.net/wp-content/uploads/2024/08/thumbnail-4747.png)
![[C言語] 現在時刻をマイクロ秒で取得する方法【Windows/UNIX】](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2583.png)
![[C言語] ミリ秒の数値を時間に変換する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2589.png)
![[C言語] 現在時刻を文字列として取得する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2588.png)
![[C言語] 現在時刻を画面に表示する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2587.png)
![[C言語] 現在時刻を取得する方法【Windows/UNIX】](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2586.png)
![[C言語] Windows環境で現在時刻をミリ秒単位で取得する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2585.png)
![[C言語] 現在時刻をミリ秒単位で取得する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2584.png)
![[C言語] 日時を扱う方法をわかりやすく解説](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2597.png)
![[C言語] 時間を時分秒に変換する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2596.png)
![[C言語] 2つの時間の大小を比較する方法](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2595.png)
![[C言語] 時間を足し算したあとの時刻を取得する](https://af-e.net/wp-content/uploads/2024/08/thumbnail-2594.png)