1. UTC转周秒
#define UNIX_GPS_DIF 315964800 // utc time base 1970.1.1; gps time base 1980.1.6, 3657 * 24 * 3600
#define GPS_TIME_LEAPSEC 18
#define SECONDS_PER_WEEK 604800//7*24*3600
**//传入的utc_time_ms (例如:1697684882843750 ,可以精确到纳秒)**
int ync_utc_to_gps_time(unsigned long long utc_time_ms,unsigned int *Week, double *Timesec)
{
unsigned int msec_time;
long totalSeconds;
unsigned int sec;
long gpsSeconds;
unsigned int week;
msec_time = (unsigned int)((utc_time_ms)%1000000);
totalSeconds = (long)((utc_time_ms) / 1000000);
gpsSeconds = totalSeconds + GPS_TIME_LEAPSEC - UNIX_GPS_DIF;
week = (unsigned int)(gpsSeconds/SECONDS_PER_WEEK);
sec = (unsigned int)(gpsSeconds - week*SECONDS_PER_WEEK);
*Week = week;
*Timesec = sec + (double)msec_time/1000000;
printf("week:%lu,msec_time:%u,totalSeconds:%ld,gpsSeconds:%ld,sec:%lu\r\n",week,msec_time,totalSeconds,gpsSeconds,sec);
return 0;
}
2. UTC转年月日时分秒-毫秒
typedef struct _time_info_{
uint16_t year;
uint16_t month;
uint16_t day;
uint16_t hour;
uint16_t minute;
uint16_t second;
uint16_t msec;
}time_info_t;
#define UTC_BASE_YEAR 1970
#define MONTH_PER_YEAR 12
#define DAY_PER_YEAR 365
#define SEC_PER_DAY 86400
#define SEC_PER_HOUR 3600
#define SEC_PER_MIN 60
const unsigned char g_day_per_mon[MONTH_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static unsigned char applib_dt_is_leap_year(unsigned short year)
{
if ((year % 400) == 0) {
return 1;
} else if ((year % 100) == 0) {
return 0;
} else if ((year % 4) == 0) {
return 1;
} else {
return 0;
}
}
static unsigned char applib_dt_last_day_of_mon(unsigned char month, unsigned short year)
{
if ((month == 0) || (month > 12)) {
return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}
if (month != 2) {
return g_day_per_mon[month - 1];
} else {
return g_day_per_mon[1] + applib_dt_is_leap_year(year);
}
}
static void convert_utc_to_time(long msec, ync_time_info_t *time_info)
{
//将时间戳值转化成天数。通过天数可以比较方便地算出年、月、日。
// int days = ts / SEC_PER_DAY;
//这个时间戳值的年。
int yearTmp = 0;
int dayTmp = 0;
int days = 0;
char buf[6] = {0};
long ts = msec/1000000;
time_info->msec = ((long)(msec%1000000))/1000;
time_info->nsec = ((long)(msec%1000000))%1000;
printf("#####mesc:%d,%d#\n",((long)(msec%1000000))/1000,sizeof(unsigned short));
memset(buf,0,sizeof(buf));
sprintf(buf,"%d%d", time_info->msec,time_info->nsec);
days = ts / SEC_PER_DAY;
//使用夹逼法计算 days 天中包含的年数。
for (yearTmp = UTC_BASE_YEAR; days > 0; yearTmp++) {
dayTmp = (DAY_PER_YEAR + applib_dt_is_leap_year(yearTmp)); //这一年有多少天?
if (days >= dayTmp) //条件成立,则 yearTmp 即是这个时间戳值所代表的年数。
{
days -= dayTmp;
}
else
{
break;
}
}
time_info->year = yearTmp;
//这个时间戳值的月
int monthTmp = 0;
for (monthTmp = 1; monthTmp < MONTH_PER_YEAR; monthTmp++) {
dayTmp = applib_dt_last_day_of_mon(monthTmp, time_info->year);
if (days >= dayTmp) {
days -= dayTmp;
}
else
{
break;
}
}
time_info->month = monthTmp;
time_info->day = days + 1;
//转化成秒。
int secs = ts % SEC_PER_DAY;
//这个时间戳值的小时数。
time_info->hour = secs / SEC_PER_HOUR;
//这个时间戳值的分钟数。
secs %= SEC_PER_HOUR;
time_info->minute = secs / SEC_PER_MIN;
//这个时间戳的秒钟数。
time_info->second = secs % SEC_PER_MIN;
printf("@@ %d-%d-%d %d:%d:%d,msec:%d,nsec:%d,%s\n\n", time_info->year, time_info->month, time_info->day, time_info->hour, time_info->minute, time_info->second,time_info->msec,time_info->nsec,buf);
}