Link to home
Start Free TrialLog in
Avatar of Vlearns
Vlearns

asked on

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!
Avatar of Vlearns
Vlearns

ASKER

\
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
Avatar of phoffric
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

Avatar of Vlearns

ASKER

hi thanks for your help

would appreciate any example doing the parsing and populating the structure

ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vlearns

ASKER

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