-2

Im trying to convert

static char bufferTR[] = "1645";

to something like this...

struct tm *tick_time = localtime(&temp);

to use in this function...

difftime(time_t end, time_t beginning)

which would give me the time between the two in seconds, as a double.

Any help would greatly be appreciated.

updated code...

#include <stdio.h>
#include <time.h>

static void update_time() {
      // Get a tm structure
      time_t temp = time(NULL);


  struct tm *tick_time = localtime(&temp);

  // Create a long-lived buffer
  //static char buffer[] = "00";
      char buffer[80];

  static char buffer2[] = "00";
  static char buffer3[] = "Xxx Xxx 00"; //Day of Week, Month, Date

  strftime(buffer3, sizeof(buffer3), "%a %b %e", tick_time);

  // Write the current hours and minutes into the buffer
  if(clock_is_24h_style() == true) {
    // Use 24 hour format
    strftime(buffer, sizeof("00"), "%M", tick_time);

    strftime(buffer2, sizeof("00"), "%H", tick_time);

  } 
  else {
    // Use 12 hour format
    strftime(buffer, sizeof("00"), "%M", tick_time);

    strftime(buffer2, sizeof("00"), "%I", tick_time);
  }

  //https://sourceware.org/newlib/libc.html#Timefns

    static char bufferTR[6] = "1645";
    time_t beginning;
    struct tm info;

    int hh;
    int mm;

    int minutediff = 1;

    if (sscanf(bufferTR, "%2d%2d", &hh, &mm) == 2) {

        printf("hh %d mm %d\n", hh, mm);

        info.tm_year = 2001 - 1900;
        info.tm_mon = 7 - 1;
        info.tm_mday = 4;
        info.tm_hour = hh;
        info.tm_min = mm;
        info.tm_sec = 1;
        info.tm_isdst = -1;

        beginning = mktime(&info);
        if( beginning == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer, sizeof(buffer), "%c", &info );
        }

        time_t end;
        struct tm info2;
        char buffer2[80];

        info2.tm_year = 2001 - 1900;
        info2.tm_mon = 7 - 1;
        info2.tm_mday = 4;
        info2.tm_hour = hh;
        info2.tm_min = mm+minutediff;
        info2.tm_sec = 1;
        info2.tm_isdst = -1;

        end = mktime(&info2);
        if( end == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer2, sizeof(buffer2), "%c", &info2 );
        }

        printf("%f", difftime(end, beginning));

    }

  // Display this time on the TextLayer
  text_layer_set_text(s_time_layer, buffer);
  text_layer_set_text(s_time_layer2, buffer2);
  text_layer_set_text(s_time_layer3, buffer3); 

  text_layer_set_text(s_temp_layer, bufferTR);
  text_layer_set_text(s_temp_layer2, buffer2);
}
OscarTheGrouch
  • 2,374
  • 4
  • 28
  • 39
  • 1
    Please make your question clearer. It's not precisely clear how each line of code shown relates to the other lines. `bufferTR` is a `char` buffer yet the next line of code does not reference any char buffers. `localtime` returns a `struct tm *` yet the next line of code does not accept any `struct tm *` parameters. – kaylum May 20 '16 at 04:36
  • 1
    POSIX function [`strptime()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html) and standard C function [`mktime()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mktime.html) may help. If that is meant to be `hhmm` (hour, minute) format, though, they're overkill. You can use `if (sscanf(bufferTR, "%2d%2d", &hh, &mm) == 2) { …OK… }` to convert the string to hours and minutes, and then multiply and add will give you the time in seconds, without having to invent a year, month, day to work with. – Jonathan Leffler May 20 '16 at 04:42

1 Answers1

1

I assume you want the difference between 16:45 and 16:45 + x.

Say the diff is x=1 minute int minutediff = 1;. Then we can just convert between the different types.

#include <stdio.h>
#include <time.h>

int main ()
{

    static char bufferTR[5] = "1645";
    time_t beginning;
    struct tm info;
    char buffer[80];

    int hh;
    int mm;

    int minutediff = 1;

    if (sscanf(bufferTR, "%2d%2d", &hh, &mm) == 2) {

        printf("hh %d mm %d\n", hh, mm);

        info.tm_year = 2001 - 1900;
        info.tm_mon = 7 - 1;
        info.tm_mday = 4;
        info.tm_hour = hh;
        info.tm_min = mm;
        info.tm_sec = 1;
        info.tm_isdst = -1;

        beginning = mktime(&info);
        if( beginning == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer, sizeof(buffer), "%c", &info );
        }

        time_t end;
        struct tm info2;
        char buffer2[80];

        info2.tm_year = 2001 - 1900;
        info2.tm_mon = 7 - 1;
        info2.tm_mday = 4;
        info2.tm_hour = hh;
        info2.tm_min = mm+minutediff;
        info2.tm_sec = 1;
        info2.tm_isdst = -1;

        end = mktime(&info2);
        if( end == -1 )
        {
            printf("Error: unable to make time using mktime\n");
        }
        else
        {
            strftime(buffer2, sizeof(buffer2), "%c", &info2 );
        }

        printf("difftime %f ", difftime(end, beginning));

    }

    return(0);
}

Output

hh 16 mm 45
difftime 60.000000 
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • Im trying to get the difference between whatever time it is now and 16:45. I added the updateTime function I have so far, it fails to build. ../src/main.c: In function 'update_time': ../src/main.c:121:3: error: 'return' with a value, in function returning void [-Werror] – OscarTheGrouch May 21 '16 at 03:38