converting date to unix timestamp

Posted on 2009-12-22
Last Modified: 2013-11-22
how can i convert a date in

const char* p="31-Dec-2002 22:36:36 -0800"

to a unix time stamp using standard c/unoix libraries

Question by:Vlearns

    Author Comment

    the input format is something of this syntax

        // date-time: day-date-fixed "-" date-month "-" date-year SP time SP zone
        // day-date-fixed: space-padded, or 2digit, day of month
        // date-month: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
        // date-year: 4 digit year
        // time: 00:00:00, all fields 2 bytes
        // zone: offset in [+-]HHMM

    and the output should be a unix timestamp
    LVL 31

    Expert Comment

    Below is code showing the setup to get the Epoch time. What you have to do is to parse the string (e.g., use strtok(), inserting into the struct tm components.  Then, mktime() does the conversion for you.
    #include <stdio.h>
    #include <time.h>
    int main() {
       char * p = "31-Dec-2002 22:36:36 -0800";
       time_t epoch;
       struct tm date;
       memset( &date, 0, sizeof(date) );
       // Epoch time 0
       date.tm_hour = -8;
       date.tm_mday = 1;
       date.tm_year = 1970 - 1900;
       epoch = mktime( &date);
       printf(" Epoch = %d\n", epoch);
       date.tm_sec = 36;
       date.tm_min = 36;
       date.tm_hour = 22;
       date.tm_mday = 31;
       date.tm_mon = 11; // Dec
       date.tm_year = 2002 - 1900;
       date.tm_hour += -8;
       epoch = mktime( &date);
       printf(p); printf("\n");
       printf(" Epoch = %d\n", epoch);

    Open in new window


    Author Comment

    hi thanks for your help

    would appreciate any example doing the parsing and populating the structure

    LVL 31

    Accepted Solution

    This is the easy part. All you do is use strtok(), or you can just read the string one char at a time; and since the format is rigid, you should have no problem.

    Here is a link for strtok. The code below is taken from that link.

    The output of this little program is (also from this link)
    Splitting string "- This, a sample string." into tokens:

    Of course, you will need to convert some strings into numbers (to fill in the struct tm), and convert the month to a number (here Jan corresponds to 0).

    If you have difficulty doing it either of the above approaches, please submit your code, and we will try to help you with the parsing.

    There is a potential issue with timezones; but let's get through the basics first.

    /* strtok example */
    #include <stdio.h>
    #include <string.h>
    int main ()
      char str[] ="- This, a sample string.";
      char * pch;
      printf ("Splitting string \"%s\" into tokens:\n",str);
      pch = strtok (str," ,.-");
      while (pch != NULL)
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
      return 0;

    Open in new window


    Author Comment

    thanks a lot

    i am trying to use strptime and having problems with time zones

    i will post that as a separate q
    please do help if you can


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (, discussed installing the Solaris Operating S…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now