2 ms·
The speed of sqlite3 is good Something like echo "select datetime(time,'unixepoch'),id from t1"|sqlite3 0.db Sometimes I need to convert dates in text fil
by 1vuio0pswjnm7 1y ago
The speed of sqlite3 is good
Something like
echo "select datetime(time,'unixepoch'),id from t1"|sqlite3 0.db
Sometimes I need to convert dates in text files to or from unixepoch^1
IME, this is at least 2x as slow as converting dates with sqlite3
For example, I have a text file with the following format
domainname ipv4-address # unixepoch
I use a simple filter something like this ("yy094")
/*
strftime<-->strptime
*/
int fileno (FILE *);
FILE *f;
#define jmp (yy_start)=1+2*
#include <time.h>
char *strptime(const char *s, const char *f, struct tm *tm);
struct tm t;
typedef long int time_t;
time_t xt;
char zt[26];
struct tm *yt;
a Mon|Tue|Wed|Thu|Fri|Sat|Sun
d (0[1-9]|[12][0-9]|3[01])
b Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec
m 0[1-9]|1[012]
Y 19[2-9][0-9]|[2-9][0-9]{3}
H [0-9]{2}
M [0-9]{2}
S [0-9]{2}
Z [+-][0-9]{2,4}|[A-EG-KMNPSTUWZh]{2,3}[CTAK]
int xd70f5083A()
{
printf("%ld",mktime(&t));
return 0;
}
int x2146ea7d()
{
xt=atoi(yytext);
if((xt<-2147483646)||(xt>2147483648))
{fwrite(yytext,1,yyleng,yyout);goto s;}
yt=localtime(&xt);
strftime(zt,sizeof(zt),"%d %b %Y %H:%M:%S %Z",yt);
fwrite(zt,sizeof(zt),1,stdout);
return 0;
s:return 1;
}
%option nounput noinput noyywrap
%%
[12][0-9]{9} {
x2146ea7d();
}
{d}[ ]{b}[ ]{Y}[ ]{H}:{M}:{S}[ ]{Z} {
strptime(yytext,"%d %b %Y %H:%M:%S %Z",&t);
xd70f5083A();
}
{d}[ ]{b}[ ]{Y}[ ]{H}:{M}:{S} {
strptime(yytext,"%d %b %Y %H:%M:%S",&t);
xd70f5083A();
}
{a}[ ]{b}[ ]{d}[ ]{H}:{M}:{S}[ ]UTC[ ]{Y} {
strptime(yytext,"%a %b %d %H:%M:%S UTC %Y",&t);
xd70f5083A();
}
{Y}-{m}-{d}T{H}:{M}:{S} {
strptime(yytext,"%Y-%m-%dT%H:%M:%S",&t);
xd70f5083A();
}
{Y}-{m}-{d}[ ]{H}:{M}:{S} {
strptime(yytext,"%Y-%m-%d %H:%M:%S",&t);
xd70f5083A();
}
.|\n+ fwrite(yytext,1,yyleng,yyout);
%%
int main(int argc, char *argv[])
{
if(argc>1)if(argv[1])
if(argv[1][0]==45)
if(argv[1][1]==104)
{
putc(10,stdout);
puts(" usage: yy094 < file");
puts(" zones: UTC only");
puts(" range: 09 Sep 2001 01:46:40 - 19 Jan 2038 03:14:07");
puts(" input formats:");
puts(" 2147483647");
puts(" 09 Sep 2001 01:46:40");
puts(" 09 Sep 2001 01:46:40 UTC");
puts(" 2001 Sep 09 01:46:40");
puts(" 2001-Sep-09T01:46:40");
puts(" 09 Sep 2001 01:46:40 UTC");
puts(" Sun Sep 09 01:46:40 UTC 2001");
puts(" output formats:");
puts(" 19 Jan 2038 03:14:07 UTC");
putc(10,stdout);
goto x;
}
yylex();
x:exit(0);
}
- 1vuio0pswjnm7 1y agoAnother input format, for HTTP response headers: {a}[,][ ]{d}[ ]{b}[ ]{Y}[ ]{H}:{M}:{S}[ ]GMT { strptime(yytext,"%a, %d %b %Y %H:%M:%S GMT",&t); xd70f5083A(); } puts(" Sun, Sep 09 2001 01:46:40 GMT");