summaryrefslogtreecommitdiff
path: root/avr/cmd_date.c
diff options
context:
space:
mode:
Diffstat (limited to 'avr/cmd_date.c')
-rw-r--r--avr/cmd_date.c136
1 files changed, 65 insertions, 71 deletions
diff --git a/avr/cmd_date.c b/avr/cmd_date.c
index ad0d3ac..efba858 100644
--- a/avr/cmd_date.c
+++ b/avr/cmd_date.c
@@ -1,4 +1,6 @@
/*
+ * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
+ *
* (C) Copyright 2001
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
@@ -8,66 +10,12 @@
/*
* RTC, Date & Time support: get and set date & time
*/
-#include <common.h>
+#include "common.h"
#include <string.h>
-#include <command.h>
-#include <rtc.h>
-#include <i2c.h>
-
-
-static const char * const weekdays[] = {
- "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
-};
-
-int mk_date (const char *, struct rtc_time *);
-
-command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
-{
- struct rtc_time tm;
- int rcode = CMD_RET_SUCCESS;
-
- (void) cmdtp; (void) flag;
-
- switch (argc) {
- case 2: /* set date & time */
- /* initialize tm with current time */
- rcode = rtc_get (&tm);
-
- if(!rcode) {
- /* insert new date & time */
- if (mk_date (argv[1], &tm) != 0) {
- my_puts_P(PSTR("## Bad date format\n"));
- break;
- }
- /* and write to RTC */
- rcode = rtc_set (&tm);
- if(rcode)
- my_puts_P(PSTR("## Set date failed\n"));
- } else {
- my_puts_P(PSTR("## Get date failed\n"));
- }
- /* FALL TROUGH */
- case 1: /* get date & time */
- rcode = rtc_get (&tm);
+#include "time.h"
+#include "rtc.h"
+#include "command.h"
- if (rcode) {
- my_puts_P(PSTR("## Get date failed\n"));
- break;
- }
- /* TODO: put weekdays[] in flash */
- printf_P(PSTR("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n"),
- tm.tm_year, tm.tm_mon, tm.tm_mday,
- (tm.tm_wday<0 || tm.tm_wday>6) ?
- "unknown " : weekdays[tm.tm_wday],
- tm.tm_hour, tm.tm_min, tm.tm_sec);
-
- break;
- default:
- rcode = CMD_RET_USAGE;
- }
-
- return rcode;
-}
/*
* simple conversion of two-digit string with error checking
@@ -97,7 +45,7 @@ static int cnvrt2 (const char *str, int *valp)
* Some basic checking for valid values is done, but this will not catch
* all possible error conditions.
*/
-int mk_date (const char *datestr, struct rtc_time *tmp)
+int mk_date (const char *datestr, struct tm *tmp)
{
int len, val;
char *ptr;
@@ -123,36 +71,36 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
tmp->tm_sec = 0;
}
- if (len == 12) { /* MMDDhhmmCCYY */
+ if (len == 12) { /* MMDDhhmmCCYY */
int year, century;
if (cnvrt2 (datestr+ 8, &century) ||
cnvrt2 (datestr+10, &year) ) {
return (-1);
}
- tmp->tm_year = 100 * century + year;
+ tmp->tm_year = 100 * century + year - 1900;
} else if (len == 10) { /* MMDDhhmmYY */
int year, century;
- century = tmp->tm_year / 100;
+ century = (tmp->tm_year + 1900) / 100;
if (cnvrt2 (datestr+ 8, &year))
return (-1);
- tmp->tm_year = 100 * century + year;
+ tmp->tm_year = 100 * century + year -1900;
}
switch (len) {
- case 8: /* MMDDhhmm */
+ case 8: /* MMDDhhmm */
/* fall thru */
- case 10: /* MMDDhhmmYY */
+ case 10: /* MMDDhhmmYY */
/* fall thru */
- case 12: /* MMDDhhmmCCYY */
+ case 12: /* MMDDhhmmCCYY */
if (cnvrt2 (datestr+0, &val) ||
val > 12) {
break;
}
- tmp->tm_mon = val;
+ tmp->tm_mon = val - 1;
if (cnvrt2 (datestr+2, &val) ||
- val > ((tmp->tm_mon==2) ? 29 : 31)) {
+ val > ((tmp->tm_mon==2-1) ? 29 : 31)) {
break;
}
tmp->tm_mday = val;
@@ -169,9 +117,6 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
}
tmp->tm_min = val;
- /* calculate day of week */
- GregorianDay (tmp);
-
return (0);
default:
break;
@@ -180,3 +125,52 @@ int mk_date (const char *datestr, struct rtc_time *tmp)
return (-1);
}
+command_ret_t do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+ struct tm t;
+ char buf[30];
+ int rcode = CMD_RET_SUCCESS;
+
+ (void) cmdtp; (void) flag;
+
+ switch (argc) {
+ case 2: /* set date & time */
+ /* initialize t with current time */
+ rcode = rtc_get (&t);
+
+ if(!rcode) {
+ /* insert new date & time */
+ if (mk_date (argv[1], &t) != 0) {
+ my_puts_P(PSTR("## Bad date format\n"));
+ break;
+ }
+
+ time_t time;
+ time = mk_gmtime(&t);
+ gmtime_r(&time, &t);
+
+ /* and write to RTC */
+ rcode = rtc_set (&t);
+ if(rcode)
+ my_puts_P(PSTR("## Set date failed\n"));
+ } else {
+ my_puts_P(PSTR("## Get date failed\n"));
+ }
+ /* FALL TROUGH */
+ case 1: /* get date & time */
+ rcode = rtc_get (&t);
+
+ if (rcode) {
+ my_puts_P(PSTR("## Get date failed\n"));
+ break;
+ }
+ asctime_r(&t, buf);
+ printf_P(PSTR("%s\n"), buf);
+ break;
+
+ default:
+ rcode = CMD_RET_USAGE;
+ }
+
+ return rcode;
+}