-2

I have a string 10131520 that I am trying to convert to Unix Epoch time in C. If the numbers were separated like 10-13-1520 I could use something like strptime() but I'm having trouble because there are no deliminators. I was thinking about perhaps splitting the bits up by reading the first 2 bits and storing them into a month variable, then the next 2 bits would be stored into day, and then the last 4 would be stored into time.

If anyone could point me in the right direction I would greatly appreciate it.

Thanks

esx0001
  • 161
  • 1
  • 7
  • its byte, not bit. Just use one of the string function to extract the value you need and convert it into the form you need for strptime. – Marged Oct 21 '15 at 07:05
  • 1
    Have you tried "%m%d%Y" as a format? I don't think that separators are required by strptime(). It won't work with the year 1520 though. Is the string really in the MMDDYYYY format or is the example just badly chosen? – Martin R Oct 21 '15 at 07:14

2 Answers2

0

This works, there are different ways, this is a simple, easy to understand way.

#include <stdio.h>
#include <string.h>

    // to change  10131520 into  10-13-1520


main(  )
{
  char string[] = "10131520";
  char stringout[11];
  char year_str[5];
  char month_str[3];
  char day_str[3];

  month_str[0] = string[0];
  month_str[1] = string[1];
  month_str[2] = '\0';

  day_str[0] = string[2];
  day_str[1] = string[3];
  day_str[2] = '\0';

  year_str[0] = string[4];
  year_str[1] = string[5];
  year_str[2] = string[6];
  year_str[3] = string[7];
  year_str[4] = '\0';

  strcpy( stringout, month_str );
  strcat( stringout, "-" );
  strcat( stringout, day_str );
  strcat( stringout, "-" );
  strcat( stringout, year_str );

  printf( "\n the date is %s", stringout );
  getchar(  );
}
BobRun
  • 756
  • 5
  • 12
0

First, get year, month and day out of your string:

char my_date="10131520";
int my_date_n=atoi(my_date); // or any better method

int month = (my_date_n/1000000)%100; 
int day   = (my_date_n/  10000)%100;
int year  = (my_date_n/      1)%10000;

(There's a lot of ways of doing this. This might not be the best.)

Then, normally for far away dates you would use julian days: https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_Day_Number

for instance:

double calc_jd(int y, int mo, int d,
               int h, int mi, float s)
{
   // variant using ints
   int A=(14-mo)/12;
   int Y=y+4800-A;
   int M=mo+12*A-3;
   int JD=d + ((153*M+2)/5) + 365*Y + (Y/4) - (Y/100) + (Y/400) - 32045;

   // add time of day at this stage
   return JD + (h-12)/24.0 + mi/1440.0 + s*(1.0/86400.0);
}

Then you convert this one to unix time, which is the inverse of the answer in this question:

Convert unix timestamp to julian

double unix_time_from_jd(double jd)
{
   return (jd-2440587.5)*86400.0;
}

so

double jd = calc_jd(year,month,day,12,0,0); // time of day, timezone?
double unix_time = unix_time_from_jd(jd);

note that you might get way outside any range that you can use with the normal tools that uses this kind of date if we're talking about the year 1520. (That's why I keep using a double here.)

Mirar
  • 96
  • 4