English 中文(简体)
如何从structtm中生成一个人类可读的字符串?
原标题:How do I make a human-readable string out of a struct tm?
  • 时间:2010-10-14 20:33:16
  •  标签:
  • c
  • time

我得到了一个structtm,我想将其转换为具有以下特定输出的字符串:

年月日时分

其中除月份(mm)以外的所有内容都是数字,例如:

2010年10月14日10:35

这是我当前的代码:

  struct stat sb;
  if (lstat(path, &sb) == 0) {
    struct tm *pmytm = gmtime(&sb.st_mtime);
    sprintf(array[index]->mtime, "%d-%d-%d %d:%d", pmytm->tm_mday, pmytm->tm_mon, 1900 + pmytm->tm_year, pmytm->tm_hour, pmytm->tm_min);

问题是我不知道如何转移这个pmytm->;tm_mon高效地进入本月。你建议我构建一个月的数组,然后索引到该数组中(在我的sprintf中用%s替换%d),还是有更好的解决方案?

此外,我对时间和分钟有意见。如果低于10(2个数字),则只显示一个数字,例如:10:8而不是10:08。我该怎么修?

非常感谢您的帮助,

编辑:我心目中的解决方案(这很优雅吗?):

  static char *months[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

  struct stat sb;
  if (lstat(path, &sb) == 0) {
    struct tm *pmytm = gmtime(&sb.st_mtime);
    sprintf(array[index]->mtime, "%02d-%s-%d %02d:%02d", pmytm->tm_mday, months[pmytm->tm_mon], 1900 + pmytm->tm_year, pmytm->tm_hour, pmytm->tm_min);

贾里

最佳回答

使用函数strftimefrom time.h

strftime(array[index]->mtime, 20, "%d-%b-%Y %H:%M", pmytm);
问题回答

如果您想更多地控制时间格式,请尝试使用strfime:

struct stat sb;
if (lstat("/etc/passwd", &sb) == 0) {
    char    time_buf[256];
    (void) strftime(time_buf, sizeof (time_buf),
        "%m-%d-%Y %H-%M (mon=%b)", localtime(&sb.st_mtime));
    (void) printf("Time: %s
", time_buf);
}

如果您不想进行时区校正,可以使用gmtime()而不是localtime()。

其输出为:

Time: 08-30-2010 17-13 (mon=Aug)




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签