【问题标题】:convert an ISO8601 timestamp with C code [duplicate]使用 C 代码转换 ISO8601 时间戳 [重复]
【发布时间】:2014-11-14 09:39:53
【问题描述】:

我正在尝试从 ISO8601 时间戳转换时间。

我想从时间戳中删除“T” 例子:

"0001-01-01T17:45:33" --> "0001-01-01 17:45:33"

这种转换对于将时间戳转换为纪元时间很有用

【问题讨论】:

  • 您已经在这里问过这个问题,结构相同,没有任何 C 代码。发布你的代码——你的输入字符串分配在哪里,你想在哪里写输出字符串?
  • 请仔细阅读问题不一样!!!!!!!!!
  • 所以这里的答案在技术上与您的问题一样详细:从左到右扫描输入字符串,并用空格替换每个出现的T 字符或- 字符字符。

标签: c


【解决方案1】:

你看过了吗

 char *strptime(const char *s, const char *format, struct tm *tm);

来自time.h

例如:

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

int main(){
    char newtime[100];
    const char *time="0001-01-01T17:45:33";
    struct tm tm_;

    strptime(time,"%FT%T",&tm_);

    strftime(newtime,100,"%F %T",&tm_);

    printf("%s\n",newtime);
    printf("Epoch time:%d\n",(int)mktime(&tm_));
    return 0;
}

输出:

1-01-01 17:45:33
Epoch time:-1

【讨论】:

  • 当时间戳包含'T'时,会出现问题!
  • mktime 返回-1,因为输入日期在纪元开始之前
【解决方案2】:

我用这段代码测试过,它对我来说很好用

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

int main() {
    char string[] = {"0001-01-01T17:45:33\0"};

    char *temp;

    temp = strchr(string, 'T') ;
    *temp= ' ';
    printf("%s\n", temp);
    printf("%s\n", string);
}

【讨论】:

  • 首先,将其发布在问题中,而不是作为单独的答案。其次,如果它工作正常,那么开始的问题是什么?
  • 第一次我没有回复,当任何人不想回答时,我在其他论坛中搜索
猜你喜欢
  • 2011-01-29
  • 2020-03-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多