Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:59:46+00:00 2026-05-31T19:59:46+00:00

I’m looking for a plain C counterpart for date.js date.parse() . That is, something

  • 0

I’m looking for a plain C counterpart for date.js date.parse().

That is, something that understands “week ago” or “yesterday” as input. English-only is OK.

Note: a library should not be licensed under GPL, so Git’s date.c or parser for GNU date -d wouldn’t do. BTW, if you wonder why wouldn’t I just sit down and code this, go and look at the source of mentioned libraries…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:59:48+00:00Added an answer on May 31, 2026 at 7:59 pm

    The following solution is not exactly what you’ve asked for but I hope that despite not being a plain C answer it will cover your needs. Reinventing the wheel isn’t a way to go so let’s use date.js in C by running it with SpiderMonkey, the Mozilla JavaScript engine.

    Here’s how I did it. I’ve begun with downloading date.js and translating it into a const char* named code defined in date.js.h.

    ( \
      echo 'const char *code =' ; \
      curl https://datejs.googlecode.com/files/date.js | \
        sed -e 's/\\/\\\\/g; s/"/\\"/g; s/^/"/; s/\r\?$/\\n"/'; \
      echo ';' \
    ) > date.js.h
    

    Then I took the JSAPI’s Hello, World! as a starting point.

    #include "jsapi.h"
    #include "date.js.h"
    
    static JSClass global_class = { "global", JSCLASS_GLOBAL_FLAGS,
      JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
      JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
      JSCLASS_NO_OPTIONAL_MEMBERS };
    
    void reportError(JSContext *cx, const char *message, JSErrorReport *report) {
      fprintf(stderr, "%s:%u:%s\n",
          report->filename ? report->filename : "<no filename>",
          (unsigned int) report->lineno, message);
    }
    
    int main(int argc, const char *argv[]) {
      JSRuntime *rt;
      JSContext *cx;
      JSObject *global;
      rt = JS_NewRuntime(8L * 1024L * 1024L);
      if (rt == NULL) return 1;
      cx = JS_NewContext(rt, 8192);
      if (cx == NULL) return 1;
      JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
      JS_SetVersion(cx, JSVERSION_LATEST);
      JS_SetErrorReporter(cx, reportError);
      global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
      if (global == NULL) return 1;
      if (!JS_InitStandardClasses(cx, global)) return 1;
    
      /* Here's where the interesting stuff is starting to take place.
       * Begin by evaluating sources of date.js */
    
      jsval out;
      if (!JS_EvaluateScript(cx, global, code, strlen(code), "code", 1, &out))
        return 1;
    
      /* Now create a call to Date.parse and evaluate it. The return value should
       * be a timestamp of a given date. If no errors occur convert the timestamp
       * to a double and print it. */
    
      const int buflen = 1024;
      char parse[buflen + 1];
      snprintf(parse, buflen, "Date.parse(\"%s\").getTime();", argv[1]);
    
      if (!JS_EvaluateScript(cx, global, parse, strlen(parse), "parse", 1, &out))
        return 1;
    
      double val;
      JS_ValueToNumber(cx, out, &val);
      printf("%i\n", (int) (val / 1000));
    
      /* Finally, clean everything up. */
    
      JS_DestroyContext(cx);
      JS_DestroyRuntime(rt);
      JS_ShutDown();
      return 0;
    }
    

    Here’s how it works in practice.

    $ time ./parse "week ago"
    1331938800
    0.01user 0.00system 0:00.02elapsed 92%CPU (0avgtext+0avgdata 6168maxresident)k
    0inputs+0outputs (0major+1651minor)pagefaults 0swaps
    $ time ./parse yesterday
    1332457200
    0.01user 0.00system 0:00.02elapsed 84%CPU (0avgtext+0avgdata 6168maxresident)k
    0inputs+0outputs (0major+1653minor)pagefaults 0swaps
    

    As you can see it’s quite fast and you could significantly increase its performance by reusing the initially created context for all subsequent calls to Date.parse.

    Speaking of licensing issues, date.js is available under terms of MIT and SpiderMonkey is available under MPL 1.1, GPL 2.0 or LGPL 2.1. Linking it dynamically satisfies the non-GPL requirement.

    TL;DR: git clone https://gist.github.com/2180739.git && cd 2180739 && make && ./parse yesterday

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.