Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
struct tm中tm_isdst场的解释_C_Date - Fatal编程技术网

struct tm中tm_isdst场的解释

struct tm中tm_isdst场的解释,c,date,C,Date,时间结构(struct tm)包含一个日历日期和时间,该日期和时间被分解为它的组件。该结构包含九个int类型的成员(按任意顺序),它们是: Member Type Meaning Range tm_sec int seconds after the minute 0-61* tm_min int minutes after the hour 0-59 tm_hour int hours since midnight 0-23 tm_mday int day of the

时间结构(struct tm)包含一个日历日期和时间,该日期和时间被分解为它的组件。该结构包含九个int类型的成员(按任意顺序),它们是:

Member  Type    Meaning Range
tm_sec  int seconds after the minute    0-61*
tm_min  int minutes after the hour  0-59
tm_hour int hours since midnight    0-23
tm_mday int day of the month    1-31
tm_mon  int months since January    0-11
tm_year int years since 1900    
tm_wday int days since Sunday   0-6
tm_yday int days since January 1    0-365
tm_isdst    int Daylight Saving Time flag
如果夏令时生效,则夏令时标志(tm_isdst)大于零;如果夏令时未生效,则为零;如果信息不可用,则小于零

我就tm_isdst提出的具体问题如下:

(1) 对于日期(YYYY-MM-DD)2016-03-16(DST生效时),tm_isdst是否应为1?还是0

// set to 1 to indicate DST
tm.tm_isdst = 1;
// set to 0 to indicate standard time
tm.tm_isdst = 0;
(2) 对于日期(YYYY-MM-DD)2016-01-16(DST未生效时),tm_isdst是否应为1?还是0

// set to 1 to indicate DST
tm.tm_isdst = 1;
// set to 0 to indicate standard time
tm.tm_isdst = 0;
(3) 2016年夏令时(美国)开始于 三月十三日(星期日)。那么在3月13日凌晨1:45,tm_isdst的价值是多少?谁设置此标志值以及如何设置

(4) 有没有办法检查DST何时从某个系统调用生效

  • 它应该是
    1
    (或正值),因为正如您所说,DST有效

  • 它应该是
    0
    ,因为正如您所说,DST无效

  • 它应该是
    0
    ,因为DST当时不生效

  • 您可以使用
    localtime()
    为给定时间填充
    struct tm
    ,然后检查该标志的值。这就是它存在的主要原因


  • 关键问题是,
    struct tm
    表示时间戳,其字段不限于24小时、12个月、60分钟等。DST字段不必与该日期时区中使用的时钟相匹配。当然,对于许多应用程序,设置为非常规值需要更正

    对于本地时间戳,只需设置所有字段(除了
    tm_yday
    tm_yday
    不需要设置)并调用
    mktime()
    。它会将所有字段调整到正常范围,包括将
    tm_isdst
    设置为0或1


  • 对于日期(YYYY-MM-DD)2016-03-16(DST生效时),tm_isdst是否应为1?还是0

    // set to 1 to indicate DST
    tm.tm_isdst = 1;
    
    // set to 0 to indicate standard time
    tm.tm_isdst = 0;
    
  • 对于日期(YYYY-MM-DD)2016-01-16(DST未生效时),tm_isdst是否应为1?还是0

    // set to 1 to indicate DST
    tm.tm_isdst = 1;
    
    // set to 0 to indicate standard time
    tm.tm_isdst = 0;
    
  • 2016年夏令时(美国)于3月13日星期日凌晨2:00开始。那么在3月13日凌晨1:45,tm_isdst的价值是多少?谁设置此标志值以及如何设置

    使用
    tm.tm_isdst=-1调用
    mktime()不确定DST设置时。如果代码知道
    struct tm
    是标准时间,请将
    tm_isdst
    字段设置为0。一年中的什么时候没有区别。与DST的wise类似,如果代码知道它是DST时间戳,则将字段设置为1。调用
    mktime()
    会将字段调整为本地时区的通常设置

    当然,1:45AM(使用
    tm_isdst<0
    )可以以任何方式进行解释,
    mktime()
    通常会选择0或1

    // set to 1 to indicate DST
    // set to 0 to indicate standard time
    // set to -1 to indicate DST is not known
    tm.tm_isdst = -1;
    mktime(&tm);  // This will adjust all the fields including tm_isdst
    
  • 有没有办法检查DST何时从某个系统调用生效

    对。要确定
    struct tm
    是否应将
    tm_isdst
    设置为0或1,请调用
    mktime()

  • 顺便说一句:健壮代码检查
    mktime()的返回值