Hacker News new | ask | show | jobs
by 1vuio0pswjnm7 517 days ago
If I use "UTC" it works. For example,

date.l:

    int fileno (FILE *);
    FILE *f;
    int printf(const char *__restrict, ...);
    #include <time.h>
    char *strptime(const char *s, const char *f, struct tm *tm);
    struct tm t;
   a (Mon|Tue|Wed|Thu|Fri|Sat|Sun)
   b (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
   d [0-2][0-9]|3[01]
   H [0-2][0-9]
   M [0-5][0-9]
   S [0-5][0-9]
   Y [1-9][0-9][0-9][0-9] 
   %option nounput noinput noyywrap
   %%
   {a}[ ]{b}[ ]{d}[ ]{H}:{M}:{S}[ ]UTC[ ]{Y} {
    strptime(yytext,"%a %b %d %H:%M:%S UTC %Y",&t);
    printf("%ld\n",mktime(&t));
    }
   .|\n
   %%
    int main(){yylex();exit(0);}
   
    flex -8Cem date.l
    cc -O3 -std=c89 -W -Wall -pipe lex.yy.c -static -s -o yydate

    date|yydate
This works for me. No need for timegm().

But if I substitute %Z or %z for "UTC" in strptime() above then this does not work.

Fun fact: strptime() can make timestamps for dates that do not exist on any calandar.

     echo "Thu Jun 31 01:59:26 UTC 2024"|yydate