6 * Copyright 2007-2008 @ xianweizeng@gmail.com
11 * File Coding system: gb2312
12 * set-buffer-file-coding-system
23 #include <sys/types.h>
30 #include <sys/ioctl.h>
32 #include <sys/select.h>
34 #include <minigui/common.h>
35 #include <minigui/minigui.h>
36 #include <minigui/gdi.h>
37 #include <minigui/window.h>
38 #include <minigui/control.h>
43 int tm_sec
; /* Seconds. [0-60] (1 leap second) */
44 int tm_min
; /* Minutes. [0-59] */
45 int tm_hour
; /* Hours. [0-23] */
46 int tm_mday
; /* Day. [1-31] */
47 int tm_mon
; /* Month. [0-11] */
48 int tm_year
; /* Year - 1900. */
49 int tm_wday
; /* Day of week. [0-6] */
54 #define RTC_DEVICE "/dev/rtc"
57 /* RTC ioctl commands macro
58 * See kernel driver source code
60 #define RTC_GETDATETIME 0
63 #define RTC_SETDATETIME 3
68 *get system time from RTC chip
70 static int getSysRtcTime(struct rtc_tm
*tm
)
74 fd
= open(RTC_DEVICE
, O_RDONLY
);
76 perror("error:can not open rtc devicen");
80 ret
= ioctl(fd
, RTC_GETDATETIME
, tm
);
82 perror("Get time error.\n");
92 * Set system time to RTC chip
94 static int setSysRtcTime(struct rtc_tm
*tm
)
98 fd
= open(RTC_DEVICE
, O_RDWR
);
100 perror("error: can not open rtc devicen.");
104 ret
= ioctl(fd
, RTC_SETDATETIME
, tm
);
106 perror("Get time error.\n");
118 * This function is obsoleted by setSysRtcTime()
121 /* set RTC date or time.
123 * flag = 0 time 1 date
125 static int setSysRtcDateTime(struct rtc_tm
*tm
,int flag
)
129 /* printf("Open RTC devices.\n"); */
130 fd
=open(RTC_DEVICE
, O_RDWR
);
132 perror("error:can not open rtc device: %s\n");
136 ret
= ioctl(fd
,flag
,tm
);
138 perror("Set system datetime error.\n");
149 * set rtc time to 2009-01-01 00:00:01
150 * call this when do a new release
152 int init_rtc_time(void)
162 tm
.tm_year
= 2009 - 1900;
165 ret
= setSysRtcTime(&tm
);
167 printf("Set RTC time error: %d \n", ret
);
174 #ifndef CONFIG_DEBUG_ON_PC
176 * update Linux system time by RTC time.
177 * read RTC and then call settimeofday().
179 * This should be done in Linux kernel. But we can do it here
180 * if the RTC driver is not well writen.
182 int update_systime(void)
184 struct rtc_tm rtc_tm
;
189 /* get system time */
190 gettimeofday(&tv
, NULL
);
192 ret
= getSysRtcTime(&rtc_tm
);
194 printf("Read RTC time error.\n");
200 tm
.tm_sec
= rtc_tm
.tm_sec
;
201 tm
.tm_min
= rtc_tm
.tm_min
;
202 tm
.tm_hour
= rtc_tm
.tm_hour
;
203 tm
.tm_mday
= rtc_tm
.tm_mday
;
204 tm
.tm_mon
= rtc_tm
.tm_mon
- 1;
205 tm
.tm_year
= rtc_tm
.tm_year
- 1900;
207 tv
.tv_sec
= mktime(&tm
);
209 if (tv
.tv_sec
== -1) {
210 printf("mktime error\n");
213 /* tv.tv_usec = 0; */
215 ret
= settimeofday(&tv
, NULL
);
218 printf("update system time successful.\n");
220 printf("update system time failed.\n");
228 * update RTC time by system time
230 int update_rtctime(void)
232 struct rtc_tm rtc_tm
;
238 /* get system time */
239 ret
= gettimeofday(&tv
, NULL
);
241 printf("get system time error.\n");
246 tm
= localtime(&curtime
);
248 rtc_tm
.tm_sec
= tm
->tm_sec
;
249 rtc_tm
.tm_min
= tm
->tm_min
;
250 rtc_tm
.tm_hour
= tm
->tm_hour
;
251 rtc_tm
.tm_mday
= tm
->tm_mday
;
252 rtc_tm
.tm_mon
= tm
->tm_mon
+ 1;
253 rtc_tm
.tm_year
= tm
->tm_year
;
255 ret
= setSysRtcTime(&rtc_tm
);
257 printf("set RTC time error.\n");
264 int update_systime(void)
269 int update_rtctime(void)
277 * convert time to string
280 char * convertSysTime(char *dispBuf
)
285 gettimeofday(&tv
, NULL
);
288 strftime(dispBuf
, 30, "%Y-%m-%d %T", localtime(&curtime
));