Skip to content

Commit

Permalink
hwclock command: use format like the Linux tool does
Browse files Browse the repository at this point in the history
Print three-letter abbreviations of the days and months.

With a fixup by Andrey Smirnov:
| common/date.c: Fix off-by-one error
|
| As per http://pubs.opengroup.org/onlinepubs/007908775/xsh/time.h.html
| 'tm_wday' is zero indexed with zero representing Sunday, this is also
| corroborated by the code in rtc_time_to_tm() which used 4 to represent
| Thursday.

Signed-off-by: Andrey Smirnov <[email protected]>
Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
saschahauer committed Jan 7, 2016
1 parent a9e7e68 commit 6eb5d78
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions commands/hwclock.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ static int do_hwclock(int argc, char *argv[])
snprintf(t, 12, "%lu", time);
setenv(env_name, t);
} else {
printf("%02d:%02d:%02d %02d-%02d-%04d\n",
tm.tm_hour, tm.tm_min, tm.tm_sec,
tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
printf("%s\n", time_str(&tm));
}

return 0;
Expand Down
18 changes: 18 additions & 0 deletions common/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,21 @@ mktime (unsigned int year, unsigned int mon,
)*60 + min /* now have minutes */
)*60 + sec; /* finally seconds */
}

const char *time_str(struct rtc_time *tm)
{
const char *weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
static char buf[128];

sprintf(buf, "%s %02d %s %4d %02d:%02d:%02d",
weekdays[tm->tm_wday],
tm->tm_mday,
months[tm->tm_mon],
tm->tm_year + 1900,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
return buf;
}
2 changes: 2 additions & 0 deletions include/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ unsigned long mktime (unsigned int, unsigned int, unsigned int,

extern struct rtc_device *rtc_lookup(const char *name);

const char *time_str(struct rtc_time *tm);

#endif /* _RTC_H_ */

0 comments on commit 6eb5d78

Please sign in to comment.