gpt4 book ai didi

C 截断 HH :MM:SS from ctime value, ctime

转载 作者:行者123 更新时间:2023-11-30 14:21:39 25 4
gpt4 key购买 nike

我有这个unix时间戳,使用ctime转换后显示为Thu Mar 26 15:30:26 2007,但我只需要Thu Mar 26 2007

如何更改或截断以消除时间 (HH:MM:SS)?

最佳答案

由于您已经获得了 time_t 值,因此可以使用 localtime()strftime() :

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

int main(void)
{
time_t t = time(0);
struct tm *lt = localtime(&t);
char buffer[20];
strftime(buffer, sizeof(buffer), "%a %b %d %Y", lt);
puts(buffer);
return(0);
}

或者,如果您觉得必须使用ctime(),那么:

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

int main(void)
{
time_t t = time(0);
char buffer[20];
char *str = ctime(&t);
memmove(&buffer[0], &str[0], 11);
memmove(&buffer[11], &str[20], 4);
buffer[15] = '\0';
puts(buffer);
return(0);
}

关于C 截断 HH :MM:SS from ctime value, ctime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14575200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com