• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 543
  • Last Modified:

converting date to unix timestamp

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

thanks!
0
Vlearns
Asked:
Vlearns
  • 3
  • 2
1 Solution
 
VlearnsAuthor Commented:
\
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
0
 
phoffricCommented:
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

0
 
VlearnsAuthor Commented:
hi thanks for your help

would appreciate any example doing the parsing and populating the structure

0
 
phoffricCommented:
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.
http://www.cplusplus.com/reference/clibrary/cstring/strtok/

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

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

0
 
VlearnsAuthor Commented:
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now