static int
iso8601wknum(const struct tm *timeptr)
{
	/*
	 * From 1003.2:
	 *	If the week (Monday to Sunday) containing January 1
	 *	has four or more days in the new year, then it is week 1;
	 *	otherwise it is the highest numbered week of the previous
	 *	year (52 or 53), and the next week is week 1.
	 *
	 * ADR: This means if Jan 1 was Monday through Thursday,
	 *	it was week 1, otherwise week 52 or 53.
	 *
	 * XPG4 erroneously included POSIX.2 rationale text in the
	 * main body of the standard. Thus it requires week 53.
	 */

	int weeknum, jan1day, diff;

	/* get week number, Monday as first day of the week */
	weeknum = weeknumber(timeptr, 1);

	/*
	 * With thanks and tip of the hatlo to [email protected]
	 *
	 * What day of the week does January 1 fall on?
	 * We know that
	 *	(timeptr->tm_yday - jan1.tm_yday) MOD 7 ==
	 *		(timeptr->tm_wday - jan1.tm_wday) MOD 7
	 * and that
	 * 	jan1.tm_yday == 0
	 * and that
	 * 	timeptr->tm_wday MOD 7 == timeptr->tm_wday
	 * from which it follows that. . .
 	 */
	jan1day = timeptr->tm_wday - (timeptr->tm_yday % 7);
	if (jan1day < 0)
		jan1day += 7;

	/*
	 * If Jan 1 was a Monday through Thursday, it was in
	 * week 1.  Otherwise it was last year's highest week, which is
	 * this year's week 0.
	 *
	 * What does that mean?
	 * If Jan 1 was Monday, the week number is exactly right, it can
	 *	never be 0.
	 * If it was Tuesday through Thursday, the weeknumber is one
	 *	less than it should be, so we add one.
	 * Otherwise, Friday, Saturday or Sunday, the week number is
	 * OK, but if it is 0, it needs to be 52 or 53.
	 */
	switch (jan1day) {
	case 1:		/* Monday */
		break;
	case 2:		/* Tuesday */
	case 3:		/* Wednesday */
	case 4:		/* Thursday */
		weeknum++;
		break;
	case 5:		/* Friday */
	case 6:		/* Saturday */
	case 0:		/* Sunday */
		if (weeknum == 0) {
#ifdef USE_BROKEN_XPG4
			/* XPG4 (as of March 1994) says 53 unconditionally */
			weeknum = 53;
#else
			/* get week number of last week of last year */
			struct tm dec31ly;	/* 12/31 last year */
			dec31ly = *timeptr;
			dec31ly.tm_year--;
			dec31ly.tm_mon = 11;
			dec31ly.tm_mday = 31;
			dec31ly.tm_wday = (jan1day == 0) ? 6 : jan1day - 1;
			dec31ly.tm_yday = 364 + isleap(dec31ly.tm_year + 1900);
			weeknum = iso8601wknum(& dec31ly);
#endif
		}
		break;
	}

	if (timeptr->tm_mon == 11) {
		/*
		 * The last week of the year
		 * can be in week 1 of next year.
		 * Sigh.
		 *
		 * This can only happen if
		 *	M   T  W
		 *	29  30 31
		 *	30  31
		 *	31
		 */
		int wday, mday;

		wday = timeptr->tm_wday;
		mday = timeptr->tm_mday;
		if (   (wday == 1 && (mday >= 29 && mday <= 31))
		    || (wday == 2 && (mday == 30 || mday == 31))
		    || (wday == 3 &&  mday == 31))
			weeknum = 1;
	}

	return weeknum;
}
示例#2
0
size_t
strftime(char *str, size_t maxsize, const char *fmt, const struct tm *ts)
{
        long	num = 0;
        long    len;
	int n;
	int year;
        char    q;
        char    buf[BIG_LEN], *putstr, *s;

        for(;;) {
                if (num >= maxsize) return 0;
                if ((q = *fmt++) == 0)  break;
                if (q != '%') {
                        *str++ = q;
                        num++;
                        continue;
                }
                if ((q = *fmt++) == 0) break;       /* get format command */

/* assume that sprintf will be used, with a variable length */
/* this is the most common case, so it saves us some coding to do it here */
                putstr = buf;

                switch(q) {
                  case 'A':
                  case 'a':
		  /*
		   * cases 'a' and 'b' are still wrong -
		   * in some locale short names do not have to be
		   * 3 characters wide - still works in North America.
		   */
	                if (ts->tm_wday < 0 || ts->tm_wday > 6)
			        putstr = "?";
			else
			        if ( 'A' == q)
				        putstr = _LC_Day_name[ts->tm_wday];
				else
				        sprintf(buf, "%-.3s",
						_LC_Day_name[ts->tm_wday]);
                        break;
                  case 'b':
                  case 'B':
                  case 'h':	/* same as 'b' */
	                if (ts->tm_mon < 0 || ts->tm_mon > 11)
			        putstr = "?";
			else
			        if ( 'B' == q)
				        putstr = _LC_Mth_name[ts->tm_mon];
				else
				        sprintf(buf, "%-.3s",
						_LC_Mth_name[ts->tm_mon]);
                        break;
                  case 'c':
			/* this format should be set in setlocale.c */
                        strftime(buf, sizeof buf, "%a %b %d %X %Y", ts);
                        break;
		  case 'd':
                        sprintf(buf, "%02d", ts->tm_mday);
                        break;
		  case 'D':	/* date as %m/%d/%y */
 		  case 'x':     /* locale preferred date; for C locale same as 'D' */
			strftime(buf, sizeof buf, "%m/%d/%y", ts);
			break;
		  case 'e':	/* day of month, blank padded */
			sprintf(buf, "%2d", ts->tm_mday);
			break;
		  case 'F':
		        sprintf(buf, "%04d-%02d-%02d", 1900+ts->tm_year, ts->tm_mon+1, ts->tm_mday);
			break;
		  case 'g':
		  case 'G':
		        isoweeknumber(ts, &year);
		        if (q == 'g') year = year % 100;
		        sprintf(buf, "%02d", year);
			break;
                  case 'H':
                        sprintf(buf, "%02d", ts->tm_hour);
                        break;
                  case 'I':
                        n = ts->tm_hour;
                        if (n == 0) n = 12;
			else if (n > 12) n -= 12;
                        sprintf(buf, "%02d", n);
                        break;
                  case 'j':
                        sprintf(buf, "%03d", ts->tm_yday + 1);
                        break;
                  case 'm':
                        sprintf(buf, "%02d", ts->tm_mon + 1);
                        break;
                  case 'M':
                        sprintf(buf, "%02d", ts->tm_min);
                        break;
		   case 'n':	/* same as \n */
			putstr = "\n";
			break;
                  case 'p':
			/*
			 * this is wrong - strings "AM', "PM" are
			 * locale dependent
			 */
                        putstr = (ts->tm_hour < 12) ? "AM" : "PM";
                        break;
		  case 'r':	/* time as %I:%M:%S %p */
			strftime(buf, sizeof buf, "%I:%M:%S %p", ts);
			break;
		  case 'R':	/* time as %H:%M */
			strftime(buf, sizeof buf, "%H:%M", ts);
			break;
                  case 'S':
                        sprintf(buf, "%02d", ts->tm_sec);
                        break;
		  case 't':	/* same as \t */
			putstr = "\t";
			break;
                  case 'T':
		  case 'X': /* locale dependent time form; for now, fix to C locale  */
                        sprintf(buf, "%02d:%02d:%02d", ts->tm_hour,
                                ts->tm_min, ts->tm_sec);
                        break;
		  case 'u':
		    	sprintf(buf, "%d", (ts->tm_wday == 0) ? 7 : ts->tm_wday);
                        break;
                  case 'U':  /* week of year - starting Sunday */
			sprintf(buf, "%02d", weeknumber(ts, 0));
                        break;
		  case 'V':
		        sprintf(buf, "%02d", isoweeknumber(ts, &year));
                        break;
		  case 'w':
		    	sprintf(buf, "%d", ts->tm_wday);
                        break;
                  case 'W': /* week of year - starting Monday */
			sprintf(buf, "%02d", weeknumber(ts, 1));
                        break;
                  case 'y':
                        sprintf(buf, "%02d", ts->tm_year % 100);
                        break;
		  case 'Y':
                        sprintf(buf, "%d", ts->tm_year + 1900);
                        break;
		  case 'z':
                        if (NULL != (s = getenv("TZ")))
			  {
			    int hh, mm;
			    char c='+';
			    _tzset();
			    hh = -_timezone;
			    if (hh < 0) {
			      c = '-';
			      hh = -hh;
			    }
			    mm = (hh/60)%60;
			    hh = hh/3600;
			    sprintf(buf, "%c%02d%02d", c, hh, mm);
			  }
			else
			  buf[0] = '\0'; /* empty string */
                        break;
                  case 'Z':
                        if (NULL != (s = getenv("TZ")))
			  {
			    _tzset();
			    if (ts->tm_isdst) {
			      strcpy(buf, _tzname[1]);
			    } else {
			      strcpy(buf, _tzname[0]);
			    }
			  }
			else
			  buf[0] = '\0'; /* empty string */
                        break;
                   case '%':
                        putstr = "%";
                        break;
		   default:
			buf[0] = q;
			buf[1] = 0;
			break;
                }

                if (num + (len = strlen(putstr)) >= maxsize)
                        return 0;

                num += len;
                while (--len >= 0)
                        *str++ = *putstr++;
        }
        *str = 0;
        return (size_t) num;
}
size_t
strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr)
{
	char *endp = s + maxsize;
	char *start = s;
	auto char tbuf[100];
	long off;
	int i, w, y;
	static short first = 1;
#ifdef POSIX_SEMANTICS
	static char *savetz = NULL;
	static int savetzlen = 0;
	char *tz;
#endif /* POSIX_SEMANTICS */
#ifndef HAVE_TM_ZONE
#ifndef HAVE_TM_NAME
#ifndef HAVE_TZNAME
	extern char *timezone();
	struct timeval tv;
	struct timezone zone;
#endif /* HAVE_TZNAME */
#endif /* HAVE_TM_NAME */
#endif /* HAVE_TM_ZONE */

	/* various tables, useful in North America */
	static const char *days_a[] = {
		"Sun", "Mon", "Tue", "Wed",
		"Thu", "Fri", "Sat",
	};
	static const char *days_l[] = {
		"Sunday", "Monday", "Tuesday", "Wednesday",
		"Thursday", "Friday", "Saturday",
	};
	static const char *months_a[] = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
	};
	static const char *months_l[] = {
		"January", "February", "March", "April",
		"May", "June", "July", "August", "September",
		"October", "November", "December",
	};
	static const char *ampm[] = { "AM", "PM", };

	if (s == NULL || format == NULL || timeptr == NULL || maxsize == 0)
		return 0;

	/* quick check if we even need to bother */
	if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize)
		return 0;

#ifndef POSIX_SEMANTICS
	if (first) {
		tzset();
		first = 0;
	}
#else	/* POSIX_SEMANTICS */
#if defined (SHELL)
	tz = get_string_value ("TZ");
#else
	tz = getenv("TZ");
#endif
	if (first) {
		if (tz != NULL) {
			int tzlen = strlen(tz);

			savetz = (char *) malloc(tzlen + 1);
			if (savetz != NULL) {
				savetzlen = tzlen + 1;
				strcpy(savetz, tz);
			}
		}
		tzset();
		first = 0;
	}
	/* if we have a saved TZ, and it is different, recapture and reset */
	if (tz && savetz && (tz[0] != savetz[0] || strcmp(tz, savetz) != 0)) {
		i = strlen(tz) + 1;
		if (i > savetzlen) {
			savetz = (char *) realloc(savetz, i);
			if (savetz) {
				savetzlen = i;
				strcpy(savetz, tz);
			}
		} else
			strcpy(savetz, tz);
		tzset();
	}
#endif	/* POSIX_SEMANTICS */

	for (; *format && s < endp - 1; format++) {
		tbuf[0] = '\0';
		if (*format != '%') {
			*s++ = *format;
			continue;
		}
	again:
		switch (*++format) {
		case '\0':
			*s++ = '%';
			goto out;

		case '%':
			*s++ = '%';
			continue;

		case 'a':	/* abbreviated weekday name */
			if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
				strcpy(tbuf, "?");
			else
				strcpy(tbuf, days_a[timeptr->tm_wday]);
			break;

		case 'A':	/* full weekday name */
			if (timeptr->tm_wday < 0 || timeptr->tm_wday > 6)
				strcpy(tbuf, "?");
			else
				strcpy(tbuf, days_l[timeptr->tm_wday]);
			break;

		case 'b':	/* abbreviated month name */
		short_month:
			if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
				strcpy(tbuf, "?");
			else
				strcpy(tbuf, months_a[timeptr->tm_mon]);
			break;

		case 'B':	/* full month name */
			if (timeptr->tm_mon < 0 || timeptr->tm_mon > 11)
				strcpy(tbuf, "?");
			else
				strcpy(tbuf, months_l[timeptr->tm_mon]);
			break;

		case 'c':	/* appropriate date and time representation */
			/*
			 * This used to be:
			 *
			 * strftime(tbuf, sizeof tbuf, "%a %b %e %H:%M:%S %Y", timeptr);
			 *
			 * Now, per the ISO 1999 C standard, it this:
			 */
			strftime(tbuf, sizeof tbuf, "%A %B %d %T %Y", timeptr);
			break;

		case 'C':
		century:
			sprintf(tbuf, "%02d", (timeptr->tm_year + 1900) / 100);
			break;

		case 'd':	/* day of the month, 01 - 31 */
			i = range(1, timeptr->tm_mday, 31);
			sprintf(tbuf, "%02d", i);
			break;

		case 'D':	/* date as %m/%d/%y */
			strftime(tbuf, sizeof tbuf, "%m/%d/%y", timeptr);
			break;

		case 'e':	/* day of month, blank padded */
			sprintf(tbuf, "%2d", range(1, timeptr->tm_mday, 31));
			break;

		case 'E':
			/* POSIX (now C99) locale extensions, ignored for now */
			goto again;

		case 'F':	/* ISO 8601 date representation */
			strftime(tbuf, sizeof tbuf, "%Y-%m-%d", timeptr);
			break;

		case 'g':
		case 'G':
			/*
			 * Year of ISO week.
			 *
			 * If it's December but the ISO week number is one,
			 * that week is in next year.
			 * If it's January but the ISO week number is 52 or
			 * 53, that week is in last year.
			 * Otherwise, it's this year.
			 */
			w = iso8601wknum(timeptr);
			if (timeptr->tm_mon == 11 && w == 1)
				y = 1900 + timeptr->tm_year + 1;
			else if (timeptr->tm_mon == 0 && w >= 52)
				y = 1900 + timeptr->tm_year - 1;
			else
				y = 1900 + timeptr->tm_year;

			if (*format == 'G')
				sprintf(tbuf, "%d", y);
			else
				sprintf(tbuf, "%02d", y % 100);
			break;

		case 'h':	/* abbreviated month name */
			goto short_month;

		case 'H':	/* hour, 24-hour clock, 00 - 23 */
			i = range(0, timeptr->tm_hour, 23);
			sprintf(tbuf, "%02d", i);
			break;

		case 'I':	/* hour, 12-hour clock, 01 - 12 */
			i = range(0, timeptr->tm_hour, 23);
			if (i == 0)
				i = 12;
			else if (i > 12)
				i -= 12;
			sprintf(tbuf, "%02d", i);
			break;

		case 'j':	/* day of the year, 001 - 366 */
			sprintf(tbuf, "%03d", timeptr->tm_yday + 1);
			break;

		case 'm':	/* month, 01 - 12 */
			i = range(0, timeptr->tm_mon, 11);
			sprintf(tbuf, "%02d", i + 1);
			break;

		case 'M':	/* minute, 00 - 59 */
			i = range(0, timeptr->tm_min, 59);
			sprintf(tbuf, "%02d", i);
			break;

		case 'n':	/* same as \n */
			tbuf[0] = '\n';
			tbuf[1] = '\0';
			break;

		case 'O':
			/* POSIX (now C99) locale extensions, ignored for now */
			goto again;

		case 'p':	/* am or pm based on 12-hour clock */
			i = range(0, timeptr->tm_hour, 23);
			if (i < 12)
				strcpy(tbuf, ampm[0]);
			else
				strcpy(tbuf, ampm[1]);
			break;

		case 'r':	/* time as %I:%M:%S %p */
			strftime(tbuf, sizeof tbuf, "%I:%M:%S %p", timeptr);
			break;

		case 'R':	/* time as %H:%M */
			strftime(tbuf, sizeof tbuf, "%H:%M", timeptr);
			break;

#if defined(HAVE_MKTIME) || defined(GAWK)
		case 's':	/* time as seconds since the Epoch */
		{
			struct tm non_const_timeptr;

			non_const_timeptr = *timeptr;
			sprintf(tbuf, "%ld", mktime(& non_const_timeptr));
			break;
		}
#endif /* defined(HAVE_MKTIME) || defined(GAWK) */

		case 'S':	/* second, 00 - 60 */
			i = range(0, timeptr->tm_sec, 60);
			sprintf(tbuf, "%02d", i);
			break;

		case 't':	/* same as \t */
			tbuf[0] = '\t';
			tbuf[1] = '\0';
			break;

		case 'T':	/* time as %H:%M:%S */
		the_time:
			strftime(tbuf, sizeof tbuf, "%H:%M:%S", timeptr);
			break;

		case 'u':
		/* ISO 8601: Weekday as a decimal number [1 (Monday) - 7] */
			sprintf(tbuf, "%d", timeptr->tm_wday == 0 ? 7 :
					timeptr->tm_wday);
			break;

		case 'U':	/* week of year, Sunday is first day of week */
			sprintf(tbuf, "%02d", weeknumber(timeptr, 0));
			break;

		case 'V':	/* week of year according ISO 8601 */
			sprintf(tbuf, "%02d", iso8601wknum(timeptr));
			break;

		case 'w':	/* weekday, Sunday == 0, 0 - 6 */
			i = range(0, timeptr->tm_wday, 6);
			sprintf(tbuf, "%d", i);
			break;

		case 'W':	/* week of year, Monday is first day of week */
			sprintf(tbuf, "%02d", weeknumber(timeptr, 1));
			break;

		case 'x':	/* appropriate date representation */
			strftime(tbuf, sizeof tbuf, "%A %B %d %Y", timeptr);
			break;

		case 'X':	/* appropriate time representation */
			goto the_time;
			break;

		case 'y':	/* year without a century, 00 - 99 */
		year:
			i = timeptr->tm_year % 100;
			sprintf(tbuf, "%02d", i);
			break;

		case 'Y':	/* year with century */
		fullyear:
			sprintf(tbuf, "%d", 1900 + timeptr->tm_year);
			break;

		/*
		 * From: Chip Rosenthal <*****@*****.**>
		 * Date: Sun, 19 Mar 1995 00:33:29 -0600 (CST)
		 * 
		 * Warning: the %z [code] is implemented by inspecting the
		 * timezone name conditional compile settings, and
		 * inferring a method to get timezone offsets. I've tried
		 * this code on a couple of machines, but I don't doubt
		 * there is some system out there that won't like it.
		 * Maybe the easiest thing to do would be to bracket this
		 * with an #ifdef that can turn it off. The %z feature
		 * would be an admittedly obscure one that most folks can
		 * live without, but it would be a great help to those of
		 * us that muck around with various message processors.
		 */
 		case 'z':	/* time zone offset east of GMT e.g. -0600 */
#ifdef HAVE_TM_NAME
			/*
			 * Systems with tm_name probably have tm_tzadj as
			 * secs west of GMT.  Convert to mins east of GMT.
			 */
			off = -timeptr->tm_tzadj / 60;
#else /* !HAVE_TM_NAME */
#ifdef HAVE_TM_ZONE
			/*
			 * Systems with tm_zone probably have tm_gmtoff as
			 * secs east of GMT.  Convert to mins east of GMT.
			 */
			off = timeptr->tm_gmtoff / 60;
#else /* !HAVE_TM_ZONE */
#if HAVE_TZNAME
			/*
			 * Systems with tzname[] probably have timezone as
			 * secs west of GMT.  Convert to mins east of GMT.
			 */
#  ifdef HPUX
			off = -timezone / 60;
#  else
			off = -(daylight ? timezone : altzone) / 60;
#  endif /* !HPUX */
#else /* !HAVE_TZNAME */
			gettimeofday(& tv, & zone);
			off = -zone.tz_minuteswest;
#endif /* !HAVE_TZNAME */
#endif /* !HAVE_TM_ZONE */
#endif /* !HAVE_TM_NAME */
			if (off < 0) {
				tbuf[0] = '-';
				off = -off;
			} else {
				tbuf[0] = '+';
			}
			sprintf(tbuf+1, "%02d%02d", off/60, off%60);
			break;

		case 'Z':	/* time zone name or abbrevation */
#ifdef HAVE_TZNAME
			i = (daylight && timeptr->tm_isdst > 0); /* 0 or 1 */
			strcpy(tbuf, tzname[i]);
#else
#ifdef HAVE_TM_ZONE
			strcpy(tbuf, timeptr->tm_zone);
#else
#ifdef HAVE_TM_NAME
			strcpy(tbuf, timeptr->tm_name);
#else
			gettimeofday(& tv, & zone);
			strcpy(tbuf, timezone(zone.tz_minuteswest,
						timeptr->tm_isdst > 0));
#endif /* HAVE_TM_NAME */
#endif /* HAVE_TM_ZONE */
#endif /* HAVE_TZNAME */
			break;

#ifdef SUNOS_EXT
		case 'k':	/* hour, 24-hour clock, blank pad */
			sprintf(tbuf, "%2d", range(0, timeptr->tm_hour, 23));
			break;

		case 'l':	/* hour, 12-hour clock, 1 - 12, blank pad */
			i = range(0, timeptr->tm_hour, 23);
			if (i == 0)
				i = 12;
			else if (i > 12)
				i -= 12;
			sprintf(tbuf, "%2d", i);
			break;
#endif

#ifdef HPUX_EXT
		case 'N':	/* Emperor/Era name */
			/* this is essentially the same as the century */
			goto century;	/* %C */

		case 'o':	/* Emperor/Era year */
			goto year;	/* %y */
#endif /* HPUX_EXT */


#ifdef VMS_EXT
		case 'v':	/* date as dd-bbb-YYYY */
			sprintf(tbuf, "%2d-%3.3s-%4d",
				range(1, timeptr->tm_mday, 31),
				months_a[range(0, timeptr->tm_mon, 11)],
				timeptr->tm_year + 1900);
			for (i = 3; i < 6; i++)
				if (islower(tbuf[i]))
					tbuf[i] = toupper(tbuf[i]);
			break;
#endif

		default:
			tbuf[0] = '%';
			tbuf[1] = *format;
			tbuf[2] = '\0';
			break;
		}
		i = strlen(tbuf);
		if (i) {
			if (s + i < endp - 1) {
				strcpy(s, tbuf);
				s += i;
			} else
				return 0;
		}
	}
out:
	if (s < endp && *format == '\0') {
		*s = '\0';
		return (s - start);
	} else
		return 0;
}
示例#4
0
//-- Called each minute
static void handle_minute_tick(struct tm *tick_time, TimeUnits units_changed) {

  // Need to be static because they're used by the system later.
  static char time_text[] = "00:00";

  char *time_format;
  static char wday_str[] = " Xxxxxxxx";
  static char daymonth_str[] = "00 Xxxxxxxxxx";
  static char weeknumber_str[] = "Semaine 00";
  //static char battery_str[] = "000%";	

  //-- Refresh week day
  snprintf(wday_str,sizeof(wday_str),"%s",WEEKDAYS[tick_time->tm_wday]);
  text_layer_set_text(text_wday_layer,wday_str);

	//-- Refresh day and month
	if (DAYMONTH_FORMAT==0)
		snprintf(daymonth_str,sizeof(daymonth_str),"%d  %s",(int)tick_time->tm_mday,MONTHS[tick_time->tm_mon]);
	else
		snprintf(daymonth_str,sizeof(daymonth_str),"%s  %d",MONTHS[tick_time->tm_mon],(int)tick_time->tm_mday);
  	text_layer_set_text(text_day_month_layer, daymonth_str);

	//-- Refresh week number
  snprintf(weeknumber_str,sizeof(weeknumber_str),WEEK,weeknumber(tick_time->tm_mday, tick_time->tm_mon+1, tick_time->tm_year+1900));
  text_layer_set_text(text_weeknumber_layer, weeknumber_str);
  
	//-- Refresh battery level
  BatteryChargeState charge_state = battery_state_service_peek();
	gbitmap_destroy(bitmap_battery);
	bitmap_battery=gbitmap_create_with_resource(BATT_IMG[charge_state.charge_percent/10]);
	bitmap_layer_set_bitmap(img_battery_layer,bitmap_battery);
	
	//-- Refresh Time
	if (clock_is_24h_style()) {
		time_format = "%R";
	} else {
		time_format = "%I:%M";
	}
	
	strftime(time_text,sizeof(time_text), time_format, tick_time);

	// Kludge to handle lack of non-padded hour format string
	// for twelve hour clock.
	if (!clock_is_24h_style() && (time_text[0] == '0')) {
		memmove(time_text, &time_text[1], sizeof(time_text) - 1);
	}

  	text_layer_set_text(text_time_layer, time_text);
	
	//-- Bluetooth connection indicator
	/*
	gbitmap_destroy(bitmap_bluetooth);
	
	if (bluetooth_connection_service_peek())
		bitmap_bluetooth=gbitmap_create_with_resource(RESOURCE_ID_IMG_BLUETOOTH);
	else
		bitmap_bluetooth=gbitmap_create_with_resource(RESOURCE_ID_IMG_EMPTY);
	bitmap_layer_set_bitmap(img_bluetooth_layer,bitmap_bluetooth);
	*/
	layer_set_hidden(bitmap_layer_get_layer(img_bluetooth_layer),(!bluetooth_connection_service_peek()));
	
	//-- Moon indicator
	gbitmap_destroy(bitmap_moon);
	
	int mph=moon_phase( julianday(tick_time->tm_mday, tick_time->tm_mon+1, tick_time->tm_year+1900) );
	
	if (mph==0) mph=1; //In some case there is this problem
	
	bitmap_moon=gbitmap_create_with_resource(MOON_IMG[mph-1]);
	bitmap_layer_set_bitmap(img_moon_layer,bitmap_moon);

  //-- Vibrate function - 1 vibrate each 30min / 2 vibrates each hour (from 9h to 21h)
  int h;
  h= (tick_time->tm_hour)*100 + tick_time->tm_min;
  //if ((htick_time->tm_hour>=9) && (tick_time->tm_hour<=21)) {
  if ((h>=900) && (h<=2100)) {
    if ( (tick_time->tm_min==30) && (tick_time->tm_sec==0) )
      vibes_short_pulse();
    if ( (tick_time->tm_min==00) && (tick_time->tm_sec==0) )
      vibes_double_pulse();
  }
}