メインコンテンツ

MISRA C:2012 Rule 21.10

The Standard Library time and date functions shall not be used

説明

ルール定義

The Standard Library time and date functions shall not be used 1 .

根拠

これらの関数を使用すると、未指定、未定義、処理系定義の動作が発生する可能性があります。

Polyspace 実装

Polyspace® は、標準ライブラリに含まれるさまざまな時間関数と日付関数および関連する型の使用に対して違反を報告します。フラグが設定される関数と型には以下が含まれます。

  • ヘッダー time.h に含まれる型、定数、関数

  • wchar.h に含まれる関数 wcsftime()

関数がマクロであり、コードに展開される場合、このルールに違反します。ルール 21.2 に違反していないことを前提としています。

標準ライブラリ関数のいずれかを関数または関数形式のマクロとして再定義する場合、Polyspace はそのユーザー定義関数または関数形式のマクロに対して違反を報告しません。

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

以下の例では、time.h ヘッダー ファイルに含まれる関数と型の使用が、このルールに対する違反として報告されます。

#include <stdio.h>
#include <wchar.h>
#include <time.h>              /*Noncompliant*/

typedef time_t my_time_t;                      /*Noncompliant*/

typedef const struct tm my_const_tm_t;         /*Noncompliant*/

void foo(void) {
	clock_t begin = clock();   /*Noncompliant*/

	/* Perform some computation or operation here that you want to measure */

	clock_t end = clock();     /*Noncompliant*/

	double time_spent = (double)(end - begin) / CLOCKS_PER_SEC; /*Noncompliant*/

	printf("Time taken for computation: %f seconds\n", time_spent);

}


void bar() {
	time_t current_time;       /*Noncompliant*/
	time(&current_time);       /*Noncompliant*/
	struct tm *local_time =                   /*Noncompliant*/
	                localtime(&current_time);   /*Noncompliant*/
}

チェック情報

グループ: 標準ライブラリ
カテゴリ: 必要
AGC カテゴリ: 必要

バージョン履歴

R2014b で導入

すべて展開する


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.