【问题标题】:Get the current time in C [duplicate]获取C中的当前时间[重复]
【发布时间】:2011-07-05 17:53:24
【问题描述】:

我想获取系统的当前时间。为此,我在 C 中使用以下代码:

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

问题是这段代码给出了一些随机时间。而且,随机时间每次都不一样。我想要我系统的当前时间。

【问题讨论】:

    标签: c time time-t localtime


    【解决方案1】:

    here复制粘贴:

    /* 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;
    }
    

    (只需将 void 添加到 main() 参数列表中,以便在 C 中工作)

    【讨论】:

    • 知道如何反其道而行之吗?字符串到 tm* ?
    • 我知道这可能有点晚了,您现在可能已经弄清楚了,但是您可以使用time.h 中的strptime 函数将char * 转换为struct tm跨度>
    • 请注意 asctime() 将在字符串末尾留下一个 \n。
    • 以上代码是多余的。 asctime(localtime(&rawtime)) 等价于单个 ctime(&rawtime) 函数调用。
    • 仅供参考 - 您不需要 time_t 对象作为 time() 的参数。将NULL0 等作为参数可以返回当前时间。
    【解决方案2】:

    初始化您的 now 变量。

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

    localtime函数用于将传递的time_t中的时间值转换为struct tm,它实际上并不检索系统时间。

    【讨论】:

      【解决方案3】:

      简单的方法:

      #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;
      }
      

      【讨论】:

        【解决方案4】:

        为了扩展上面@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的信息可以在here找到。

        【讨论】:

          【解决方案5】:
          #include<stdio.h>
          #include<time.h>
          
          void main()
          {
              time_t t;
              time(&t);
              printf("\n current time is : %s",ctime(&t));
          }
          

          【讨论】:

            【解决方案6】:

            您可以使用此功能获取当前本地时间。如果你想要 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);
            

            【讨论】:

              【解决方案7】:

              如果您只需要没有日期的时间。

                time_t rawtime;
                struct tm * timeinfo;
                time( &rawtime );
                timeinfo = localtime( &rawtime );
                printf("%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, 
                  timeinfo->tm_sec);
              

              【讨论】:

                【解决方案8】:
                #include <stdio.h>
                #include <time.h>
                
                void main()
                {
                    time_t t;
                    time(&t);
                    clrscr();
                
                    printf("Today's date and time : %s",ctime(&t));
                    getch();
                }
                

                【讨论】:

                • 您能用纯文本详细说明您的解决方案吗?
                • void main() 应该是 int main(void)
                【解决方案9】:

                建议使用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 不提供毫秒数。

                【讨论】:

                  【解决方案10】:

                  长版

                  源代码: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
                  

                  【讨论】:

                  • 如果要将接收到的值存储在变量中,请将短版本第 7 行更改为: char *date = ctime(&current_time);
                  【解决方案11】:

                  伙计们,我有一种获取系统时间的新方法。虽然它很长并且充满了愚蠢的作品,但是通过这种方式您可以获得整数格式的系统时间。

                  #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;
                  }
                  

                  【讨论】:

                  • Linux 中的时间库和系统调用并不令人吃惊,但即便如此,您还是希望使用它们,而不是使用 shell 命令并尝试解析它。有关 time 和 ctime 和 rtc 的信息,请参见手册页。
                  猜你喜欢
                  • 2016-04-23
                  • 2016-07-30
                  • 2011-02-18
                  • 2015-06-18
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多