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