| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "gtime.h"
- #include <time.h>
- #include "os/co_task.h"
- //one while will use 4 ins
- /* below function is dumy here, will overide by xxx_rtc.c */
- __weak void rtc_set_time_with_utc_second(uint32_t s){
- }
- __weak uint32_t rtc_get_timestamp(void){
- return 0;
- }
- /* set rtc time, used utc timestamp, can not include time zone*/
- uint32_t calendar_to_utc(time_val *time){
- struct tm utc_time;
- struct tm my_time;
- memset(&utc_time, 0, sizeof (struct tm));
- memset(&my_time, 0, sizeof (struct tm));
-
- my_time.tm_mday = time->day;
- my_time.tm_mon = time->month- 1;
- my_time.tm_year = time->year - 1900;
- my_time.tm_hour = time->hour;
- my_time.tm_min = time->minute;
- my_time.tm_sec = time->second;
- utc_time.tm_mday = 1;
- utc_time.tm_mon = 1 - 1;
- utc_time.tm_year = 1970 - 1900;
- utc_time.tm_hour = 0;
- utc_time.tm_min = 0;
- utc_time.tm_sec = 0;
- return mktime(&my_time) - mktime(&utc_time);
- }
- void set_utc_time(time_val *time){
- uint32_t second = calendar_to_utc(time);
- rtc_set_time_with_utc_second(second);
- }
- /* get the utc readable time */
- void utc_to_calendar(uint32_t second, time_val *out){
- struct tm *tm;
- tm = localtime((time_t *)&second);
- out->year = tm->tm_year + 1900;
- out->month = tm->tm_mon + 1;
- out->day = tm->tm_mday;
- out->hour = tm->tm_hour;
- out->minute = tm->tm_min;
- out->second = tm->tm_sec;
- }
- /*tz quarter-hour ,15min ,-48~+48*/
- uint32_t get_timestamp_with_time_zone(int8_t tz){
- return (rtc_get_timestamp()+ tz*15*60);
- }
- uint32_t get_timestamp(void){
- return rtc_get_timestamp();
- }
- void get_local_time(time_val *out, int8_t tz){
- utc_to_calendar(get_timestamp_with_time_zone(tz), out);
- }
|