Example #1
0
/*
  Function to update the time layers.
*/
void update_time() {
  // get a tm structure
  time_t temp = time(NULL);
  struct tm *tick_time = localtime(&temp);
  
  // 12 hour clock
  unsigned short hour = tick_time->tm_hour % 12;

  // converts "0" to "12"
  hour = hour ? hour : 12;

  // only do memory unload/load if necessary
  if (s_loaded_hour != hour) {
    load_hour_image(hour);
  }
  
  // long-lived buffer for the minutes
  static char s_minutes[] = "00";
  
  // set minutes
  strftime(s_minutes, sizeof("00"), "%M", tick_time);
  
  // change minutes layer location
  unsigned short n1s = (s_minutes[0]=='1') + (s_minutes[1]=='1');
  if (hour == 10 || hour == 12) {
    set_minute_layer_location(70 + 3*n1s);
  } else {
    set_minute_layer_location(53 + 3*n1s);
  }
  
  // show minutes
  text_layer_set_text(s_minutes_layer, s_minutes);
}
void display_time(struct tm * tick_time) {

  // 24 hour clock not supported

  char new_date_n_day_text[] = "xxx 00xxx"; // size: 9
  
  strftime(new_date_n_day_text, sizeof(new_date_n_day_text), "%b %e%a", tick_time);
  
  if(strcmp(last_date_n_day_text, new_date_n_day_text)) {
    strcpy(last_date_n_day_text, new_date_n_day_text);
    static char new_date_text[] = "xxx 00";
    static char new_day_text[] = "xxx";
    for(int i = 0; i < 9; i++) {
      if(i < 6) {
        new_date_text[i] = new_date_n_day_text[i];
      } else {
        new_day_text[i-6] = new_date_n_day_text[i];
      }
    }
    text_layer_set_text(dateLayer, new_date_text);
    text_layer_set_text(dayLayer, new_day_text);
  }

  unsigned short hour = tick_time->tm_hour % 12;

  // Converts "0" to "12"
  hour = hour ? hour : 12;

  // Only do memory unload/load if necessary
  if (loaded_hour != hour) {
    unload_digit_image();
    load_digit_image(hour);
  }

  // Show minute
  static char text[] = "00";

  strftime(text, sizeof(text), "%M", tick_time);

  unsigned short n1s = (text[0]=='1') + (text[1]=='1');

  if (hour == 10 || hour == 12) {
    set_minute_layer_location(70 + 3*n1s);
  } else {
    set_minute_layer_location(53 + 3*n1s);
  }

  text_layer_set_text(minuteLayer, text);
}