C语言中的strtotime函数 [英] strtotime function in C

查看:78
本文介绍了C语言中的strtotime函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在C语言中实现一个通用功能,该功能可以将任何格式的任何服务器日志文件中的日期转换为时间戳,因为我想记录潜在的黑客破解服务器的频率.在PHP中,我可以使用strtotime,但我认为C中不存在这种功能.

I'm trying to make a universal function in C that can convert a date from any server log file in any format into a timestamp since I want to log how frequent potential hackers failed to crack the server. In PHP I can use strtotime but I don't think such functionality exists in C.

如果所有日志文件都使用YYYY-MM-DD HH:MM:SS格式的相同时间格式,那么我可以摆脱这样的代码:

If all log files used the same time format in the form of YYYY-MM-DD HH:MM:SS then I could get away with code like this:

//tmadd = 19 bytes as a char reserved for date/time plus trailing null
long result=0;
struct tm x;
if (sscanf(tmadd,"%i-%i-%i %i:%i:%i",&x.tm_year,&x.tm_mon,&x.tm_mday,&x.tm_hour,&x.tm_min,&x.tm_sec) >= 6){x.tm_year-=1900;result=mktime(&x);}

问题在于某些日志的单词数月,而我还没有找到捕获等价数字的函数.

The problem is some logs have months as words and I have yet to find a function to capture the numeric equivalent.

我希望能够在一个函数中完成整个转换.

I want to be able to do the whole conversion within one function.

是否有一个简单的函数可以用来以任何常用格式从任何指定日期获取原始时间戳?我基本上是在寻找一种方法来推广我自己的PHP的strtotime().

Is there an easy function I can use to get a raw timestamp from any specified date in any common format? I'm basically looking for a way to roll off my own PHP's strtotime().

推荐答案

转换任何时间格式的函数是一个非常雄心勃勃的项目,因为它着眼于PHP strtotime 函数的公认日期格式.节目.

A function to convert any time format is a very ambitious project as a A look at the recognised date formats for the PHP strtotime function shows.

但是,如果您的服务器上使用了几种已知的时间戳格式,则可以单独检查它们:

But if you have a few known timestamp formats in use on your servers, you can check them separately:

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

int strtomonth(int *mm, const char *str)
{
    static const char *sname[] = {
        "jan", "feb", "mar", "apr", "may", "jun",
        "jul", "aug", "sep", "oct", "nov", "dec",
        NULL
    };    
    int i;

    for (i = 0; sname[i]; i++) {
        if (strcmp(sname[i], str) == 0) {
            *mm = i + 1;
            return 1;
        }
    }

    return 0;
}

int strtotime(time_t *time, const char *s)
{
    struct tm tm;
    int dd, mm, yy;
    int hrs, min, sec;
    char buf[20];

    if (sscanf(s, "%u-%u-%u %u:%u:%u",
        &yy, &mm, &dd, &hrs, &min, &sec) == 6) goto okay;

    if (sscanf(s, "%u-%19[a-zA-Z]-%u %u:%u:%u",
        &yy, buf, &dd, &hrs, &min, &sec) == 6
        && strtomonth(&mm, buf)) goto okay;

    return 0;

  okay:
    // Do some sanity checking to rule out 2015-mar-35 and such

    tm.tm_sec = sec;
    tm.tm_min = min;
    tm.tm_hour = hrs;
    tm.tm_mday = dd;
    tm.tm_mon = mm - 1;
    tm.tm_year = yy - 1900;

    *time = mktime(&tm);
    return 1;
}

这段相当长但仍然很粗糙的代码,不会检查日期和时间是否合理,可以识别两种格式"2015-05-30 18:13:04" "2015-may-30 18:13:04" .您可以在遇到它们时添加更多内容.

This rather long but still very rough code, which doesn't check whether the dates and times are reasonable, recognises the two formats "2015-05-30 18:13:04"and "2015-may-30 18:13:04". You can add more as you encounter them.

goto 可能很难看,但是我认为这是最后获得通用代码的好方法.月查询非常粗糙,但对于短字符串来说应该可以.

The goto may be ugly, but I think it is a good way to get to the common code at the end. The month lookup is very crude, but should be okay for short strings.

这篇关于C语言中的strtotime函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆