时间转换为 HH:MM:SS 格式的字符串(C 编程)

发布于 2024-08-07 03:48:50 字数 536 浏览 6 评论 0原文

我需要将“HH:MM:SS”格式的当前时间转换为字符数组(字符串),以便稍后可以使用 printf("%s", timeString);printf("%s", timeString);输出结果 顺便说一句,

我对 timevaltime_t 类型感到非常困惑,所以任何解释都会很棒:)

编辑: 所以我尝试了 strftime 等,它有点工作。这是我的代码:

time_t current_time;
struct tm * time_info;
char timeString[8];

time(&current_time);
time_info = localtime(&current_time);

strftime(timeString, 8, "%H:%M:%S", time_info);
puts(timeString);

但输出是这样的:“13:49:53a??J`aS?”

最后的“a??J`aS?”是怎么回事?

I need to get the current time in a "HH:MM:SS"-format into a character array (string) so I can output the result later simply with a printf("%s", timeString);

I'm pretty confused on the timeval and time_t types btw, so any explanation would be awesome:)

EDIT:
So I tried with strftime etc, and it kinda worked. Here is my code:

time_t current_time;
struct tm * time_info;
char timeString[8];

time(¤t_time);
time_info = localtime(¤t_time);

strftime(timeString, 8, "%H:%M:%S", time_info);
puts(timeString);

But the output is this: "13:49:53a??J`aS?"

What is going on with the "a??J`aS?" at the end?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-08-14 03:48:50

您从这段代码中得到了垃圾:

time_t current_time;
struct tm * time_info;
char timeString[8];

time(¤t_time);
time_info = localtime(¤t_time);

strftime(timeString, 8, "%H:%M:%S", time_info);
puts(timeString);

因为您不允许在字符串上留有空终止符(\0)的空间,所以当它打印字符串时,它不知道结尾在哪里,并且会解释字符串中的随机垃圾。下一位内存作为字符串的一部分。

将其更改为:

time_t current_time;
struct tm * time_info;
char timeString[9];  // space for "HH:MM:SS\0"

time(¤t_time);
time_info = localtime(¤t_time);

strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info);
puts(timeString);

它将正常工作,因为 strftime() 将有足够的空间来添加 \0。请注意,我使用 sizeof(array) 来避免忘记更改两个位置的数字的风险。

You're getting garbage from this code:

time_t current_time;
struct tm * time_info;
char timeString[8];

time(¤t_time);
time_info = localtime(¤t_time);

strftime(timeString, 8, "%H:%M:%S", time_info);
puts(timeString);

Because you're not allowing space for a null terminator (\0) on the string, so when the string it printed, it doesn't know where the end is and inteprets random garbage in the next bit of memory as part of the string.

Change it to this:

time_t current_time;
struct tm * time_info;
char timeString[9];  // space for "HH:MM:SS\0"

time(¤t_time);
time_info = localtime(¤t_time);

strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info);
puts(timeString);

And it'll work correctly because strftime() will have enough space to add a \0. Note that I'm using sizeof(array) to avoid the risk forgetting to change the number in both places.

意中人 2024-08-14 03:48:50

看一下 strftime 函数,它允许您将时间写入具有您选择的格式的字符数组。

Take a look at the strftime function, which allows you to write the time into a char array with a format of your choice.

满身野味 2024-08-14 03:48:50
#include <stdio.h>
#include <time.h>

/* get seconds since the Epoch */
time_t secs = time(0);

/* convert to localtime */
struct tm *local = localtime(&secs);

/* and set the string */
sprintf(timeString, "%02d:%02d:%02d", local->tm_hour, local->tm_min, local->tm_sec);

处理时间的重要类型(时间的挂钟类型,而不是进程/线程时间)是 time_tstruct tm
对于某些工作,您可以在其中一种时间和另一种时间之间进行转换,但您必须注意本地时间与 UTC 时间。

仔细阅读 的说明,尝试那里的函数,直到你在 C 中理解时间。

再次,注意 UTC 时间和本地时间。

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

/* get seconds since the Epoch */
time_t secs = time(0);

/* convert to localtime */
struct tm *local = localtime(&secs);

/* and set the string */
sprintf(timeString, "%02d:%02d:%02d", local->tm_hour, local->tm_min, local->tm_sec);

The important types for dealing with time (the wall-clock type of time, not process/thread time) are time_t and struct tm.
With some work you can convert between one and the other, but you have to pay attention to local time versus UTC time.

Peruse the description of <time.h>, try the functions there until you grok time in C.

Again, pay attention to UTC time and local time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文