120

システムの現在時刻を取得したい。そのために、Cで次のコードを使用しています。

time_t now;
struct tm *mytime = localtime(&now); 
if ( strftime(buffer, sizeof buffer, "%X", mytime) )
{
    printf("time1 = \"%s\"\n", buffer);
}

問題は、このコードがランダムな時間を与えていることです。また、ランダムな時間は毎回異なります。システムの現在の時刻が必要です。

4

11 に答える 11

152

ここからコピーして貼り付けます:

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  
  return 0;
}

(これをCで機能させるには、引数リストに追加voidするだけです)main()

于 2011-02-28T12:33:34.037 に答える
78

now変数を初期化します。

time_t now = time(0); // Get the system time

この関数は、渡されlocaltimeた時間値をに変換するために使用されますが、実際にはシステム時間を取得しません。time_tstruct tm

于 2011-02-28T12:31:14.450 に答える
39

簡単な方法:

#include <time.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    time_t mytime = time(NULL);
    char * time_str = ctime(&mytime);
    time_str[strlen(time_str)-1] = '\0';
    printf("Current Time : %s\n", time_str);

    return 0;
}
于 2013-09-05T12:09:58.187 に答える
19

上記の@mingosからの回答を拡張するために、時間を特定の形式([dd mm yyyy hh:mm:ss])にフォーマットする以下の関数を作成しました。

// Store the formatted string of time in the output
void format_time(char *output){
    time_t rawtime;
    struct tm * timeinfo;
    
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    
    sprintf(output, "[%d %d %d %d:%d:%d]", timeinfo->tm_mday,
            timeinfo->tm_mon + 1, timeinfo->tm_year + 1900,
            timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
}

詳細についてstruct tmは、こちらをご覧ください

于 2015-05-29T04:59:34.487 に答える
13
#include<stdio.h>
#include<time.h>

void main()
{
    time_t t;
    time(&t);
    printf("\n current time is : %s",ctime(&t));
}
于 2015-08-10T04:40:27.450 に答える
10

この関数を使用して、現在の現地時間を取得できます。gmtが必要な場合は、のgmtime代わりに関数を使用してくださいlocaltime。乾杯

time_t my_time;
struct tm * timeinfo; 
time (&my_time);
timeinfo = localtime (&my_time);
CCLog("year->%d",timeinfo->tm_year+1900);
CCLog("month->%d",timeinfo->tm_mon+1);
CCLog("date->%d",timeinfo->tm_mday);
CCLog("hour->%d",timeinfo->tm_hour);
CCLog("minutes->%d",timeinfo->tm_min);
CCLog("seconds->%d",timeinfo->tm_sec);
于 2015-07-27T06:19:07.510 に答える
3

日付なしで時間が必要な場合。

  time_t rawtime;
  struct tm * timeinfo;
  time( &rawtime );
  timeinfo = localtime( &rawtime );
  printf("%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, 
    timeinfo->tm_sec);
于 2019-09-13T11:57:29.420 に答える
2
#include <stdio.h>
#include <time.h>

void main()
{
    time_t t;
    time(&t);
    clrscr();

    printf("Today's date and time : %s",ctime(&t));
    getch();
}
于 2015-01-05T07:16:01.840 に答える
1

ロングバージョン

src:https ://en.wikipedia.org/wiki/C_date_and_time_functions

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    time_t current_time;
    char* c_time_string;

    /* Obtain current time. */
    current_time = time(NULL);

    if (current_time == ((time_t)-1))
    {
        (void) fprintf(stderr, "Failure to obtain the current time.\n");
        exit(EXIT_FAILURE);
    }

    /* Convert to local time format. */
    c_time_string = ctime(&current_time);

    if (c_time_string == NULL)
    {
        (void) fprintf(stderr, "Failure to convert the current time.\n");
        exit(EXIT_FAILURE);
    }

    /* Print to stdout. ctime() has already added a terminating newline character. */
    (void) printf("Current time is %s", c_time_string);
    exit(EXIT_SUCCESS);
}

出力は次のとおりです。

Current time is Thu Sep 15 21:18:23 2016

短縮版:

#include <stdio.h>
#include <time.h>

int main(int argc, char *argv[]) {
    time_t current_time;
    time(&current_time);
    printf("%s", ctime(&current_time));

出力は次のとおりです。

Current time is Thu Jan 28 15:22:31 2021
于 2020-11-13T12:07:42.713 に答える
1

localtime_sの代わりに使用するのが一般的localtimeです。これは機能するはずです。

time_t current_raw_time = time(0); // System time: number of seconds since 00:00, Jan 1 1970 UTC
struct tm day_time;
localtime_s(&day_time, &current_raw_time);

day_time次のメンバーが含まれます。

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    int tm_min;   // minutes after the hour - [0, 59]
    int tm_hour;  // hours since midnight - [0, 23]
    int tm_mday;  // day of the month - [1, 31]
    int tm_mon;   // months since January - [0, 11]
    int tm_year;  // years since 1900
    int tm_wday;  // days since Sunday - [0, 6]
    int tm_yday;  // days since January 1 - [0, 365]
    int tm_isdst; // daylight savings time flag
};

localtimeはミリ秒を提供しないことに注意してください。

于 2021-04-23T16:17:39.847 に答える
-10

みんな私はシステム時間を取得する新しい方法を手に入れました。その長くてばかげた仕事でいっぱいですが、このようにしてあなたは整数形式でシステム時間を得ることができます。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char hc1,hc2,mc1,mc2;
    int hi1,hi2,mi1,mi2,hour,minute;
    system("echo %time% >time.txt");
    fp=fopen("time.txt","r");
    if(fp==NULL)
       exit(1) ;
    hc1=fgetc(fp);
    hc2=fgetc(fp);
    fgetc(fp);
    mc1=fgetc(fp);
    mc2=fgetc(fp);
    fclose(fp);
    remove("time.txt");
    hi1=hc1;
    hi2=hc2;
    mi1=mc1;
    mi2=mc2;
    hi1-=48;
    hi2-=48;
    mi1-=48;
    mi2-=48;
    hour=hi1*10+hi2;
    minute=mi1*10+mi2;
    printf("Current time is %d:%d\n",hour,minute);
    return 0;
}
于 2013-10-31T09:51:05.597 に答える