c - 如何在结构中存储当前系统日期?

标签 c

我查找当前系统日期和时间的程序是

#include<stdio.h>
#include<time.h>
struct date{
int day,month,year;
};
main()
{
    time_t t;
    time(&t);
    printf("Today's date and time is %s",ctime(&t));


}

我想将当前日期存储到结构中请给我一个建议。

最佳答案

标准库已经有了像你这样的结构:struct tm<time.h> .

       int tm_sec;     /* seconds (0 - 60) */
       int tm_min;     /* minutes (0 - 59) */
       int tm_hour;    /* hours (0 - 23) */
       int tm_mday;    /* day of month (1 - 31) */
       int tm_mon;     /* month of year (0 - 11) */
       int tm_year;    /* year - 1900 */
       int tm_wday;    /* day of week (Sunday = 0) */
       int tm_yday;    /* day of year (0 - 365) */
       int tm_isdst;   /* is summer time in effect? */
       char *tm_zone;  /* abbreviation of timezone name */
       long tm_gmtoff; /* offset from UTC in seconds */

库提供了一个类型为struct tm的全局变量由函数 localtime 填充(针对您的时区)和 gmtime (格林威治标准时间)。

C11 还指定了 localtime_sgmtime_s这避免了与全局变量相关的问题,但我不知道它们得到了多广泛的支持。 POSIX 还指定了类似的 gmtime_rlocaltime_r .

关于c - 如何在结构中存储当前系统日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42360992/

相关文章:

c - 如何优雅地处理 accept() 给 EMFILE 并关闭连接?

c - 进程共享内存不共享(c,linux)

c - 如何编写一个 C 程序,在调用时运行另一个给定程序并尝试控制子进程的行为

Code::Blocks 中的 C DLL

c - 二进制搜索单词词典

c++ - 在 c/c++ 中通过函数在内存中的地址调用函数

c - VS2005+WinSock : warning C4018: '<' : signed/unsigned mismatch

c - 从文件中写入和读取并按升序对它们进行排序

C Shell 打开文件并返回命令提示符

c - 无法通过 unix 系统调用使用 c 写入文件