【问题标题】:Converting a time_t to a string in a given format in C在 C 中将 time_t 转换为给定格式的字符串
【发布时间】:2017-03-22 12:35:44
【问题描述】:

因此,作为任务的一部分,我们已经完成了这个骨架函数:

char *time2str(time_t time) {
    static char *str_fmt = "%02d/%02d/%4d %02d:%02d";
    char *time_s = ""; // appropriate string allocation here

    return time_s;
}

但是我对 C 非常陌生,并且努力弄清楚我做错了什么(以及指针如何工作)......我一直在尝试使用 localtime 来获得 struct我可以使用它访问我需要以给定格式添加到字符串中的特定值。像这样的:

char *time2str(time_t time) {
    static char *str_fmt = "%02d/%02d/%4d %02d:%02d";

    struct tm *info;
    info = localtime(&time);

    char *time_s = (str_fmt, info->tm_mday, info->tm_mon...etc...);

    return time_s;
}

但是,无论我尝试做什么,我都会在编译时收到警告和错误(删除 str_fmt 并将格式放在 time_s 开头的引号中,使用 info.tm_mday 等而不是 @987654329 @)。但是,似乎没有任何效果,我在网上找不到任何可以帮助我按照我们被要求的方式解决问题的方法。

有人可以帮忙吗?值得一提的是,我们正在使用 MINIX 系统调用在 MINIX 3.2.1 上执行此操作。

【问题讨论】:

  • 有趣,上面的代码块正是它作为“骨架”实现提供给我们的方式,我假设它都是正确的
  • 我想你的老师希望你用malloc分配,然后用sprintf。
  • 如果只有 POSIX 有 asprintf()。唉,它不是 POSIX。 (尽管它在 Linux/GNU/macOS/大多数 *BSD 中可用。)我想知道如果我们使用它而不是 malloc()+snprintf()(或者更糟,sprintf())会避免多少缓冲区溢出。

标签: c string time posix


【解决方案1】:

使用你给我们的,我假设函数应该是:

char *time2str(time_t time) {
    static char *str_fmt = "%02d/%02d/%4d %02d:%02d";

    // Convert to struct tm
    // localtime returns a pointer, so shallow copy to info
    struct tm info = *localtime(&time);

    // Allocate memory for ##/##/#### ##:##
    char *time_s = malloc(17);  // Space for 16 chars and eos
    if (NULL == time_s) {// Handle error.....}
    sprintf(time_s, str_fmt, info.tm_mon + 1, info.tm_mday, 1900 + info.tm_year,
           info.tm_hour, info.tm_min);

    return time_s;
}

【讨论】:

  • 谢谢,我刚刚使用上面的 cmets 实现了同样的东西,并且不再出现编译错误。认为这也帮助我更好地掌握了 C 的一些概念(来自 Java):)
【解决方案2】:

在 C 中管理字符串是一项棘手的任务。多年成功的代码在出现意外情况时可能会失败。

考虑格式"%02d/%02d/%4d %02d:%02d"。这通常需要一个大小为 16 + 1 的数组(对于空字符)。这与为Y2K bugs 制作的编码类型相同,因为它在 9999 年之后需要更多空间。考虑一个允许time_t 表示遥远未来的时间并尝试形成基于日期/时间字符串的系统它的最大值 --> 缓冲区溢出。

相反,允许struct tm 中的所有潜在值,并提供大量的临时缓冲区。然后分配。

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

// Max length needed for a `int` as decimal text 
#define INT_STR_LEN (sizeof(int)*CHAR_BIT/3 + 2)

#define TIME2STR_FMT "%02d/%02d/%04d %02d:%02d"
// Worst case size - and them some
#define TIME2STR_SZ (sizeof TIME2STR_FMT + 5*INT_STR_LEN)

char *time2str(time_t time) {

  struct tm *info = localtime(&time);
  if (info == NULL) return NULL;

  char buf[TIME2STR_SZ];
  int n = snprintf(buf, sizeof buf, TIME2STR_FMT, info->tm_mon + 1, info->tm_mday,
      info->tm_year + 1900, info->tm_hour, info->tm_min);
  if (n < 0 || n >= sizeof buf) return NULL;

  // Allocate and copy
  size_t sz = strlen(buf) + 1;
  char *time_s = malloc(sz);
  if (time_s) {
    memcpy(time_s, buf, sz);
  }

  return time_s;
}

int main(void) {
  time_t t;
  puts(time2str(time(&t)));
  return 0;
}

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2016-09-15
    • 2020-04-06
    • 2015-12-22
    • 2011-07-19
    相关资源
    最近更新 更多