Ctime,C 中的 mktime

问题描述 投票:0回答:1

我无法正确打印

time_in
,您能显示我的代码哪里出了问题吗?谢谢您,希望您度过愉快的一天!

输入:

55 17:30 11-06-2023 07:15 15-07-2023
,第一个数字是租自行车的价格(不是必须的),后面的数字是租借时间和还车时间,格式为:HH:MM DD-MM-YY,使用
long int
mktime
形式打印出租赁时间和还车时间,并使用
Sat Jul 15 07:15:01 2023
 以标准形式(例如 
ctime

)打印出来

这是我的代码:



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

int main(){
    int price;
    scanf("%d", &price);

    struct tm input_time;
    struct tm output_time;
    time_t time_in, time_out;

    scanf("%d:%d %d-%d-%d", &input_time.tm_hour, &input_time.tm_min, &input_time.tm_mday, &input_time.tm_mon, &input_time.tm_year);
    scanf("%d:%d %d-%d-%d", &output_time.tm_hour, &output_time.tm_min, &output_time.tm_mday, &output_time.tm_mon, &output_time.tm_year);

    input_time.tm_mon -= 1;
    output_time.tm_mon -= 1;
    input_time.tm_year -= 1900;
    output_time.tm_year -= 1900;

// printing out every component to check if i failed to input them
    printf("%d\n", price);
    printf("%ld:", input_time.tm_hour );
    printf("%ld ", input_time.tm_min );
    printf("%ld-", input_time.tm_mday );
    printf("%ld-", input_time.tm_mon );
    printf("%ld\n", input_time.tm_year );

    printf("%ld:", output_time.tm_hour );
    printf("%ld ", output_time.tm_min );
    printf("%ld-", output_time.tm_mday );
    printf("%ld-", output_time.tm_mon );
    printf("%ld\n", output_time.tm_year );
    
//using mktime
    time_in = mktime(&input_time);
    time_out = mktime(&output_time);

// print out the time in seconds form
    printf("%ld\n", time_in);
    printf("%ld\n", time_out);

// print out the time in the 'Sat Jul 15 07:15:01 2023' form
    printf(ctime(&time_in));
    printf(ctime(&time_out));
    
}

这是我的输出,我打印了正确的返回时间,但是租金时间全乱了,我每次运行代码时都在改变

第一次运行:

550000        
17:30 11-5-123
7:15 15-6-123 
1592440040    
1689380101    
Thu Jun 18 07:27:20 2020
Sat Jul 15 07:15:01 2023

第二次运行:

550000        
17:30 11-5-123
7:15 15-6-123 
-2085243656    
1689380101    
Mon Jan 09 19:07:20 2040
Sat Jul 15 07:15:01 2023
c time.h
1个回答
0
投票

你有:

struct tm input_time;
struct tm output_time;

这些都在堆栈上。因此,您未显式设置的结构部分可以具有随机值,因为这是 UB(未定义行为)。

要解决此问题:

struct tm input_time = { 0 };
struct tm output_time = { 0 };

在我的系统上,UB 总是产生一致的结果。因此,为了模拟随机行为,我重构了代码以使用

rand
来预先播种结构。

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

int opt_z;

void
timeget(struct tm *tm)
{

    unsigned char *bp = (unsigned char *) tm;
    for (int idx = 0;  idx < sizeof(struct tm);  ++idx) {
        if (opt_z)
            bp[idx] = 0;
        else
            bp[idx] = rand();
    }

    scanf("%d:%d %d-%d-%d",
        &tm->tm_hour, &tm->tm_min, &tm->tm_mday, &tm->tm_mon, &tm->tm_year);

    tm->tm_mon -= 1;
    tm->tm_year -= 1900;
}

void
timeout(const struct tm *tm)
{

    printf("%ld:", tm->tm_hour);
    printf("%ld ", tm->tm_min);
    printf("%ld-", tm->tm_mday);
    printf("%ld-", tm->tm_mon);
    printf("%ld\n", tm->tm_year);
}

int
main(int argc,char **argv)
{
    int price;

    struct timespec ts;
    clock_gettime(CLOCK_REALTIME,&ts);
    srand(ts.tv_sec + ts.tv_nsec);

    --argc;
    ++argv;

    for (;  argc > 0;  --argc, ++argv) {
        char *cp = *argv;
        if (*cp != '-')
            break;

        cp += 2;
        switch (cp[-1]) {
        case 'z':
            opt_z = ! opt_z;
            break;
        }
    }

    scanf("%d", &price);

    struct tm input_time;
    struct tm output_time;
    time_t time_in, time_out;

    timeget(&input_time);
    timeget(&output_time);

// printing out every component to check if i failed to input them
    printf("%d\n", price);
    timeout(&input_time);
    timeout(&output_time);

//using mktime
    time_in = mktime(&input_time);
    time_out = mktime(&output_time);

// print out the time in seconds form
    printf("%ld\n", time_in);
    printf("%ld\n", time_out);

// print out the time in the 'Sat Jul 15 07:15:01 2023' form
    printf(ctime(&time_in));
    printf(ctime(&time_out));

}

这是 5 次运行的“坏”输出:


55
17:30 11-5-123
7:15 15-6-123
2733672778
802174487
Wed Aug 16 13:32:58 2056
Sat Jun  3 06:14:47 1995

55
17:30 11-5-123
7:15 15-6-123
3203774680
466772950
Fri Jul 10 13:24:40 2071
Tue Oct 16 07:09:10 1984

55
17:30 11-5-123
7:15 15-6-123
842460146
2612896015
Wed Sep 11 12:42:26 1996
Fri Oct 18 16:26:55 2052

55
17:30 11-5-123
7:15 15-6-123
3272253702
2303913152
Sun Sep 10 03:21:42 2073
Sat Jan  3 10:52:32 2043

55
17:30 11-5-123
7:15 15-6-123
1981040292
3831648313
Sun Oct 10 12:58:12 2032
Sat Jun  2 14:45:13 2091

使用

-z
运行程序几次:


55
17:30 11-5-123
7:15 15-6-123
1686522600
1689423300
Sun Jun 11 18:30:00 2023
Sat Jul 15 08:15:00 2023

55
17:30 11-5-123
7:15 15-6-123
1686522600
1689423300
Sun Jun 11 18:30:00 2023
Sat Jul 15 08:15:00 2023

55
17:30 11-5-123
7:15 15-6-123
1686522600
1689423300
Sun Jun 11 18:30:00 2023
Sat Jul 15 08:15:00 2023

55
17:30 11-5-123
7:15 15-6-123
1686522600
1689423300
Sun Jun 11 18:30:00 2023
Sat Jul 15 08:15:00 2023

55
17:30 11-5-123
7:15 15-6-123
1686522600
1689423300
Sun Jun 11 18:30:00 2023
Sat Jul 15 08:15:00 2023
© www.soinside.com 2019 - 2024. All rights reserved.