草庐IT

关于 c:tm struct time.h 未规范化

codeneng 2023-03-28 原文

tm struct time.h not normalizing

我正在向我的 tm 结构的时间(小时、分钟、秒)成员添加值,即使我正在使用 mktime(),它们也没有更新/规范化这是代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
struct tm timeStruct;
char buffer[80];

timeStruct.tm_year = 2016 - 1900;
timeStruct.tm_mon = 3;
timeStruct.tm_mday = 32;
timeStruct.tm_hour = 23;
timeStruct.tm_min = 59;
timeStruct.tm_sec = 59;
timeStruct.tm_isdst = -1;

printf("Date before adding interval: \
"
);
mktime(&timeStruct);
strftime(buffer, sizeof(buffer),"%c", &timeStruct);
printf(buffer);

printf("\
the year is %d\
"
, timeStruct.tm_year );
printf("the month is %d\
"
, timeStruct.tm_mon );
printf("the day is %d\
"
, timeStruct.tm_mday );
printf("the hours are %d\
"
, timeStruct.tm_hour );
printf("the minutes are %d\
"
, timeStruct.tm_min );
printf("the seconds are %d\
"
, timeStruct.tm_sec );

/*
 * Add intervals to time
 */

timeStruct.tm_sec += 2;
timeStruct.tm_min += 2;
timeStruct.tm_hour += 5;

printf("Date after adding interval: \
"
);
strftime(buffer, sizeof(buffer),"%c", &timeStruct);
printf(buffer);

printf("\
the year is %d\
"
, timeStruct.tm_year );
printf("the month is %d\
"
, timeStruct.tm_mon );
printf("the day is %d\
"
, timeStruct.tm_mday );
printf("the hours are %d\
"
, timeStruct.tm_hour );
printf("the minutes are %d\
"
, timeStruct.tm_min );
printf("the seconds are %d\
"
, timeStruct.tm_sec );

控制台输出:1

这是控制台输出的打印输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Date before adding interval:
Mon May  2 23:59:59 2016
the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding interval:
Mon May  2 28:61:61 2016
the year is 116
the month is 4
the day is 2
the hours are 28
the minutes are 61
the seconds are 61

我在 Windows 7 机器上使用 Eclipse,用 Cygwin 编译。

  • 您可以从阅读 strftime() 的手册页开始,这将表明您使用的格式字符串已知存在问题。提供一个有效的格式字符串,事情应该会好得多。请注意,要使 %c 起作用,必须正确设置本地语言环境。
  • 这种对 printf() 的调用将导致编译器发出警告消息:printf(buffer); 建议使用:printf("%s\
    ", buffer);


在您的代码中,仅在添加间隔之前调用 mktime()。您需要在添加间隔后调用它(以便它可以标准化更新的 timeStruct):

1
2
3
4
printf("Date after adding interval: \
"
);
mktime(&timeStruct);    // <--- here
strftime(buffer, sizeof(buffer),"%c", &timeStruct);

输出:

1
2
3
4
5
6
7
Tue May  3 05:02:01 2016
the year is 116
the month is 4
the day is 3
the hours are 5
the minutes are 2
the seconds are 1


这里是更正后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <time.h>

struct tm timeStruct;
char buffer[80];

int main( void )
{
    timeStruct.tm_year = 2016 - 1900;
    timeStruct.tm_mon = 3;
    timeStruct.tm_mday = 32;
    timeStruct.tm_hour = 23;
    timeStruct.tm_min = 59;
    timeStruct.tm_sec = 59;
    timeStruct.tm_isdst = -1;

    printf("Date before adding interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("first call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("first call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );

    /*
     * Add intervals to time
     */

    timeStruct.tm_sec += 2;
    timeStruct.tm_min += 2;
    timeStruct.tm_hour += 5;
    timeStruct.tm_year = 2016 - 1900;
    timeStruct.tm_mon = 3;
    timeStruct.tm_mday = 32;
    timeStruct.tm_hour = 23;
    timeStruct.tm_min = 59;
    timeStruct.tm_sec = 59;
    timeStruct.tm_isdst = -1;

    printf("Date after adding first interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("second call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("second call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );

    /*
     * Add intervals to time
     */

    timeStruct.tm_sec += 2;
    timeStruct.tm_min += 2;
    timeStruct.tm_hour += 5;

    printf("Date after adding second interval: \
"
);
    if( (time_t)-1 == mktime(&timeStruct) )
    {
        perror("third call to mktime failed due to:");
    }

    if( 0 == strftime(buffer, sizeof(buffer),"%c", &timeStruct) )
    {
        perror("third call to strftime failed due to:");
    }

    printf("\
\
%s\
"
, buffer);

    printf("\
the year is %d\
"
, timeStruct.tm_year );
    printf("the month is %d\
"
, timeStruct.tm_mon );
    printf("the day is %d\
"
, timeStruct.tm_mday );
    printf("the hours are %d\
"
, timeStruct.tm_hour );
    printf("the minutes are %d\
"
, timeStruct.tm_min );
    printf("the seconds are %d\
"
, timeStruct.tm_sec );
}

这是当前/更正后的代码输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Date before adding interval:


Mon May  2 23:59:59 2016

the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding first interval:


Mon May  2 23:59:59 2016

the year is 116
the month is 4
the day is 2
the hours are 23
the minutes are 59
the seconds are 59
Date after adding second interval:


Tue May  3 05:02:01 2016

the year is 116
the month is 4
the day is 3
the hours are 5
the minutes are 2
the seconds are 1

有关关于 c:tm struct time.h 未规范化的更多相关文章

  1. ruby - gem 规范失败 - 2

    我正在为毕业设计开发GEM,TravisCI构建不断失败。这是我在Travis上的链接:https://travis-ci.org/ricardobond/perpetuus/builds/8709218构建错误是:$bundleexecrakerakeaborted!Don'tknowhowtobuildtask'default'/home/travis/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in`eval'/home/travis/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_

  2. ruby-on-rails - 关于 Ruby 的一般问题 - 2

    我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia

  3. ruby - 我怎样才能更好地了解/了解更多关于 Ruby 的知识? - 2

    按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我最近开始学习Ruby,这是我的第一门编程语言。我对语法感到满意,并且我已经完成了许多只教授相同基础知识的教程。我已经写了一些小程序(包括我自己的数组排序方法,在有人告诉我谷歌“冒泡排序”之前我认为它非常聪明),但我觉得我需要尝试更大更难的东西来理解更多关于Ruby.关于如何执行此操作的任何想法?

  4. ruby - 关于 Ruby 中 Dir[] 和 File.join() 的混淆 - 2

    我在Ruby中遇到了一个关于Dir[]和File.join()的简单程序,blobs_dir='/path/to/dir'Dir[File.join(blobs_dir,"**","*")].eachdo|file|FileUtils.rm_rf(file)ifFile.symlink?(file)我有两个困惑:首先,File.join(@blobs_dir,"**","*")中的第二个和第三个参数是什么意思?其次,Dir[]在Ruby中有什么用?我只知道它等价于Dir.glob(),但是,我对Dir.glob()确实不是很清楚。 最佳答案

  5. elasticsearch源码关于TransportSearchAction【阶段三】 - 2

    1.回顾.TransportServicepublicclassTransportServiceextendsAbstractLifecycleComponentTransportService:方法:1publicfinalTextendsTransportResponse>voidsendRequest(finalTransport.Connectionconnection,finalStringaction,finalTransportRequestrequest,finalTransportRequestOptionsoptions,TransportResponseHandlerT>

  6. 关于Qt程序打包后运行库依赖的常见问题分析及解决方法 - 2

    目录一.大致如下常见问题:(1)找不到程序所依赖的Qt库version`Qt_5'notfound(requiredby(2)CouldnotLoadtheQtplatformplugin"xcb"in""eventhoughitwasfound(3)打包到在不同的linux系统下,或者打包到高版本的相同系统下,运行程序时,直接提示段错误即segmentationfault,或者Illegalinstruction(coredumped)非法指令(4)ldd应用程序或者库,查看运行所依赖的库时,直接报段错误二.问题逐个分析,得出解决方法:(1)找不到程序所依赖的Qt库version`Qt_5'

  7. ruby - 如何禁止在 RSpec 中显示挂起(跳过)的规范? - 2

    我有几个跳过的规范。Pending:(Failureslistedhereareexpectedanddonotaffectyoursuite'sstatus)1)...#Notyetimplemented#./spec/requests/request_spec.rb:22如何抑制未决规范的输出? 最佳答案 您可以添加以下配置选项以从运行中过滤掉所有待处理的规范:RSpec.configuredo|config|config.filter_run_excludingskip:trueend此外,here是一个更详细的抑制输出的建议

  8. ruby-on-rails - 从帮助器规范中 stub 一个帮助器方法 - 2

    我正在构建Rails应用程序并使用RSpec制定测试。我为我正在创建的名为current_link_to的方法编写了测试。此方法应该检查当前页面是否对应于我传递给它的路径,并将current类添加到生成的链接中,以防它匹配。这是规范:require"spec_helper"describeApplicationHelperdodescribe"#current_link_to"dolet(:name){"Products"}let(:path){products_path}let(:rendered){current_link_to(name,path)}context"whenthe

  9. ruby-on-rails - 在 RSpec 中编写请求规范的正确方法是什么? - 2

    tl;dr:跳到最后一段最近一直在尝试使用RSpec的requestspecs做一些更有针对性的测试。我的测试主要是这样的:通用cucumber功能规范,即用户转到带有评论的帖子,对评论点赞,作者获得积分modelspecs当模型实际上具有某些功能时,即User#upvote(comment)controllerspecs我在其中stub了大部分内容,只是试图确保代码按照我期望的方式运行viewspecs当View中有一些复杂的东西时,例如仅在用户尚未投票时呈现upvote链接,这些被stub为好吧问题是当我有一些导致错误的特定场景时,一切似乎都在我无法重现它的模型/View层中工作。

  10. ruby-on-rails - 加速 RSpec 请求规范的方法 - 2

    我有33个规范以大约5秒的速度运行,以这种速度运行会导致测试套件变慢。我追踪到请求规范(4秒以上),因为模型规范只用了一小部分时间。我已经检查过,我的请求规范没有任何过于复杂或不必要的东西,所以我不知道该去哪里让它们更快,而不是只在推送代码之前运行它们以确保一切正常.加快请求规范的最佳方法是什么? 最佳答案 我使用Spork来加速我的测试。它保持整个环境加载以赢得时间。看看这个博客:http://ykyuen.wordpress.com/2010/12/14/rails-running-rspec-with-spork-test-s

随机推荐