Home | History | Annotate | Download | only in toolbox
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <fcntl.h>
      4 #include <string.h>
      5 #include <errno.h>
      6 #include <time.h>
      7 #include <linux/android_alarm.h>
      8 
      9 static void settime(char *s) {
     10     struct tm tm;
     11     int day = atoi(s);
     12     int hour;
     13     time_t t;
     14     int fd;
     15     struct timespec ts;
     16 
     17     while (*s && *s != '.')
     18         s++;
     19 
     20     if (*s)
     21         s++;
     22 
     23     hour = atoi(s);
     24 
     25     tm.tm_year = day / 10000 - 1900;
     26     tm.tm_mon = (day % 10000) / 100 - 1;
     27     tm.tm_mday = (day % 100);
     28     tm.tm_hour = hour / 10000;
     29     tm.tm_min = (hour % 10000) / 100;
     30     tm.tm_sec = (hour % 100);
     31     tm.tm_isdst = -1;
     32 
     33     t = mktime(&tm);
     34 
     35     fd = open("/dev/alarm", O_RDWR);
     36     ts.tv_sec = t;
     37     ts.tv_nsec = 0;
     38     ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
     39 }
     40 
     41 int date_main(int argc, char *argv[])
     42 {
     43 	int c;
     44     int res;
     45 	struct tm tm;
     46 	time_t t;
     47 	struct timeval tv;
     48     struct timespec ts;
     49 	char strbuf[260];
     50     int fd;
     51 
     52     int useutc = 0;
     53 
     54     tzset();
     55 
     56     do {
     57         c = getopt(argc, argv, "us:");
     58         if (c == EOF)
     59             break;
     60         switch (c) {
     61         case 'u':
     62             useutc = 1;
     63             break;
     64         case 's':
     65             settime(optarg);
     66             break;
     67         case '?':
     68             fprintf(stderr, "%s: invalid option -%c\n",
     69                 argv[0], optopt);
     70             exit(1);
     71         }
     72     } while (1);
     73     if(optind + 2 < argc) {
     74         fprintf(stderr,"%s [-u] [date]\n", argv[0]);
     75         return 1;
     76     }
     77 
     78     int hasfmt = argc == optind + 1 && argv[optind][0] == '+';
     79     if(optind == argc || hasfmt) {
     80         char buf[2000];
     81         time(&t);
     82         if (useutc) {
     83             gmtime_r(&t, &tm);
     84             strftime(strbuf, sizeof(strbuf),
     85                      (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S GMT %Y"),
     86                      &tm);
     87         } else {
     88             localtime_r(&t, &tm);
     89             strftime(strbuf, sizeof(strbuf),
     90                      (hasfmt ? argv[optind] + 1 : "%a %b %e %H:%M:%S %Z %Y"),
     91                      &tm);
     92         }
     93         printf("%s\n", strbuf);
     94     }
     95     else if(optind + 1 == argc) {
     96 #if 0
     97         struct tm *tmptr;
     98         tmptr = getdate(argv[optind]);
     99         if(tmptr == NULL) {
    100             fprintf(stderr,"getdate_r failed\n");
    101             return 1;
    102         }
    103         tm = *tmptr;
    104 #if 0
    105         if(getdate_r(argv[optind], &tm) < 0) {
    106             fprintf(stderr,"getdate_r failed %s\n", strerror(errno));
    107             return 1;
    108         }
    109 #endif
    110 #endif
    111         //strptime(argv[optind], NULL, &tm);
    112         //tv.tv_sec = mktime(&tm);
    113         //tv.tv_usec = 0;
    114         strtotimeval(argv[optind], &tv);
    115         printf("time %s -> %d.%d\n", argv[optind], tv.tv_sec, tv.tv_usec);
    116         fd = open("/dev/alarm", O_RDWR);
    117         ts.tv_sec = tv.tv_sec;
    118         ts.tv_nsec = tv.tv_usec * 1000;
    119         res = ioctl(fd, ANDROID_ALARM_SET_RTC, &ts);
    120         //res = settimeofday(&tv, NULL);
    121         if(res < 0) {
    122             fprintf(stderr,"settimeofday failed %s\n", strerror(errno));
    123             return 1;
    124         }
    125     }
    126     else {
    127         fprintf(stderr,"%s [-s 20070325.123456] [-u] [date]\n", argv[0]);
    128         return 1;
    129     }
    130 
    131     return 0;
    132 }
    133