OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3143|回复: 10

GPS,UTC和本地时间的显示器

[复制链接]

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
发表于 2018-2-2 21:55:36 | 显示全部楼层 |阅读模式
GPS,UTC和本地时间的显示器

转载来源:GPS,UTC和本地时间的显示器

GitHub仓库:https://github.com/XinLiGitHub/GpsUtcAndLocalTime
PS:博文不再更新,后续更新会在GitHub仓库进行。

      GPS,UTC和本地时间的显示器。程序中涉及到朱利安日期的转换,详细介绍见维基百科[Julian day](https://en.wikipedia.org/wiki/Julian_day)。

1,开发环境
      1,操作系统:Windows 10 专业版
      2,IDE:Visual Studio 2015 专业版

2,程序源码
      DateTime.h文件
[mw_shl_code=c,true]/****************************************************************
* Copyright (C) 2017, XinLi, all right reserved.
* File name:    DateTime.h
* Date:         2017.10.17
* Description:  Date and time module header file.
*****************************************************************/

#ifndef __DATETIME_H
#define __DATETIME_H

/****************************************************************
*                        Header include
*****************************************************************/


/****************************************************************
*                       Macro definition
*****************************************************************/


/****************************************************************
*                       Type definition
*****************************************************************/


/****************************************************************
*                     Structure definition
*****************************************************************/
typedef struct
{
  int year;
  int month;
  int day;
  int hour;
  int minute;
  int second;
}DateTime;

typedef struct
{
  int week;
  int second;
}GpsWeekSecond;


#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */

/****************************************************************
*                     Variable declaration
*****************************************************************/


/****************************************************************
*                     Function declaration
*****************************************************************/
DateTime GregorianCalendarDateAddYear(DateTime time, int year);
DateTime GregorianCalendarDateAddMonth(DateTime time, int month);
DateTime GregorianCalendarDateAddWeek(DateTime time, int week);
DateTime GregorianCalendarDateAddDay(DateTime time, int day);
DateTime GregorianCalendarDateAddHour(DateTime time, int hour);
DateTime GregorianCalendarDateAddMinute(DateTime time, int minute);
DateTime GregorianCalendarDateAddSecond(DateTime time, int second);
GpsWeekSecond GregorianCalendarDateToGpsWeekSecond(DateTime time);
double GregorianCalendarDateToJulianDate(DateTime time);
double GregorianCalendarDateToModifiedJulianDate(DateTime time);
GpsWeekSecond GpsWeekSecondAddYear(GpsWeekSecond time, int year);
GpsWeekSecond GpsWeekSecondAddMonth(GpsWeekSecond time, int month);
GpsWeekSecond GpsWeekSecondAddWeek(GpsWeekSecond time, int week);
GpsWeekSecond GpsWeekSecondAddDay(GpsWeekSecond time, int day);
GpsWeekSecond GpsWeekSecondAddHour(GpsWeekSecond time, int hour);
GpsWeekSecond GpsWeekSecondAddMinute(GpsWeekSecond time, int minute);
GpsWeekSecond GpsWeekSecondAddSecond(GpsWeekSecond time, int second);
DateTime GpsWeekSecondToGregorianCalendarDate(GpsWeekSecond time);
double GpsWeekSecondToJulianDate(GpsWeekSecond time);
double GpsWeekSecondToModifiedJulianDate(GpsWeekSecond time);
double JulianDateAddYear(double jd, int year);
double JulianDateAddMonth(double jd, int month);
double JulianDateAddWeek(double jd, int week);
double JulianDateAddDay(double jd, int day);
double JulianDateAddHour(double jd, int hour);
double JulianDateAddMinute(double jd, int minute);
double JulianDateAddSecond(double jd, int second);
DateTime JulianDateToGregorianCalendarDate(double jd);
GpsWeekSecond JulianDateToGpsWeekSecond(double jd);
double JulianDateToModifiedJulianDate(double jd);
double ModifiedJulianDateAddYear(double mjd, int year);
double ModifiedJulianDateAddMonth(double mjd, int month);
double ModifiedJulianDateAddWeek(double mjd, int week);
double ModifiedJulianDateAddDay(double mjd, int day);
double ModifiedJulianDateAddHour(double mjd, int hour);
double ModifiedJulianDateAddMinute(double mjd, int minute);
double ModifiedJulianDateAddSecond(double mjd, int second);
DateTime ModifiedJulianDateToGregorianCalendarDate(double mjd);
GpsWeekSecond ModifiedJulianDateToGpsWeekSecond(double mjd);
double ModifiedJulianDateToJulianDate(double mjd);

#ifdef __cplusplus
}
#endif  /* __cplusplus */

#endif  /* __DATETIME_H */
[/mw_shl_code]

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-2-2 21:59:08 | 显示全部楼层
      DateTime.c文件
[mw_shl_code=c,true]/****************************************************************
* Copyright (C) 2017, XinLi, all right reserved.
* File name:    DateTime.c
* Date:         2017.10.17
* Description:  Date and time module source file.
*****************************************************************/

/****************************************************************
*                        Header include
*****************************************************************/
#include "DateTime.h"

/****************************************************************
*                       Global variables
*****************************************************************/


/****************************************************************
*                     Function declaration
*****************************************************************/


/****************************************************************
*                     Function definition
*****************************************************************/

/****************************************************************
* Function:    GregorianCalendarDateAddYear
* Description: Gregorian calendar date add year.
* Input:       time: Gregorian calendar date.
*              year: The number of year to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddYear(DateTime time, int year)
{
  time.year += year;

  if(time.month == 2)
  {
    int mday = 0;

    if((((time.year % 4) == 0) && ((time.year % 100) != 0)) || ((time.year % 400) == 0))
    {
      mday = 29;
    }
    else
    {
      mday = 28;
    }

    if(time.day > mday)
    {
      time.month += 1;
      time.day   -= mday;
    }
  }

  return time;
}

/****************************************************************
* Function:    GregorianCalendarDateAddMonth
* Description: Gregorian calendar date add month.
* Input:       time:  Gregorian calendar date.
*              month: The number of month to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddMonth(DateTime time, int month)
{
  time.year  += month / 12;
  time.month += month % 12;

  if(time.month > 12)
  {
    time.year  += 1;
    time.month -= 12;
  }

  int mday = 0;

  if((time.month == 1) || (time.month == 3) || (time.month == 5) || (time.month == 7) ||
     (time.month == 8) || (time.month == 10) || (time.month == 12))
  {
    mday = 31;
  }
  else if((time.month == 4) || (time.month == 6) || (time.month == 9) || (time.month == 11))
  {
    mday = 30;
  }
  else
  {
    if((((time.year % 4) == 0) && ((time.year % 100) != 0)) || ((time.year % 400) == 0))
    {
      mday = 29;
    }
    else
    {
      mday = 28;
    }
  }

  if(time.day > mday)
  {
    time.month += 1;
    time.day   -= mday;

    if(time.month > 12)
    {
      time.year  += 1;
      time.month -= 12;
    }
  }

  return time;
}

/****************************************************************
* Function:    GregorianCalendarDateAddWeek
* Description: Gregorian calendar date add week.
* Input:       time: Gregorian calendar date.
*              week: The number of week to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddWeek(DateTime time, int week)
{
  double jd = GregorianCalendarDateToJulianDate(time) + week * 7.0;

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateAddDay
* Description: Gregorian calendar date add day.
* Input:       time: Gregorian calendar date.
*              day:  The number of day to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddDay(DateTime time, int day)
{
  double jd = GregorianCalendarDateToJulianDate(time) + day;

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateAddHour
* Description: Gregorian calendar date add hour.
* Input:       time: Gregorian calendar date.
*              hour: The number of hour to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddHour(DateTime time, int hour)
{
  time.hour += hour;

  double jd = GregorianCalendarDateToJulianDate(time);

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateAddMinute
* Description: Gregorian calendar date add minute.
* Input:       time:   Gregorian calendar date.
*              minute: The number of minute to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddMinute(DateTime time, int minute)
{
  time.minute += minute;

  double jd = GregorianCalendarDateToJulianDate(time);

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateAddSecond
* Description: Gregorian calendar date add second.
* Input:       time:   Gregorian calendar date.
*              second: The number of seconds to add.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GregorianCalendarDateAddSecond(DateTime time, int second)
{
  time.second += second;

  double jd = GregorianCalendarDateToJulianDate(time);

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateToGpsWeekSecond
* Description: Gregorian calendar date to gps week and second.
* Input:       time: Gregorian calendar date.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GregorianCalendarDateToGpsWeekSecond(DateTime time)
{
  double jd = GregorianCalendarDateToJulianDate(time);

  return JulianDateToGpsWeekSecond(jd);
}

/****************************************************************
* Function:    GregorianCalendarDateToJulianDate
* Description: Gregorian calendar date to julian date.
* Input:       time: Gregorian calendar date.
* Output:
* Return:      Julian date.
*****************************************************************/
double GregorianCalendarDateToJulianDate(DateTime time)
{
  int jdn = (1461 * (time.year + 4800 + (time.month - 14) / 12)) / 4
          + (367 * (time.month - 2 - 12 * ((time.month - 14) / 12))) / 12
          - (3 * ((time.year + 4900 + (time.month - 14) / 12) / 100)) / 4
          + time.day - 32075;

  double jd = jdn + ((time.hour - 12) * 3600.0 + time.minute * 60.0 + time.second) / 86400.0;

  return jd;
}

/****************************************************************
* Function:    GregorianCalendarDateToModifiedJulianDate
* Description: Gregorian calendar date to modified julian date.
* Input:       time: Gregorian calendar date.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double GregorianCalendarDateToModifiedJulianDate(DateTime time)
{
  return GregorianCalendarDateToJulianDate(time) - 2400000.5;
}
[/mw_shl_code]
回复 支持 反对

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-2-2 21:59:46 | 显示全部楼层
本帖最后由 XinLiOV 于 2018-2-2 22:06 编辑

[mw_shl_code=c,true]/****************************************************************
* Function:    GpsWeekSecondAddYear
* Description: Gps week and second add year.
* Input:       time: Gps week and second.
*              year: The number of year to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddYear(GpsWeekSecond time, int year)
{
  DateTime date = GpsWeekSecondToGregorianCalendarDate(time);

  date = GregorianCalendarDateAddYear(date, year);

  return GregorianCalendarDateToGpsWeekSecond(date);
}

/****************************************************************
* Function:    GpsWeekSecondAddMonth
* Description: Gps week and second add month.
* Input:       time:  Gps week and second.
*              month: The number of month to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddMonth(GpsWeekSecond time, int month)
{
  DateTime date = GpsWeekSecondToGregorianCalendarDate(time);

  date = GregorianCalendarDateAddMonth(date, month);

  return GregorianCalendarDateToGpsWeekSecond(date);
}

/****************************************************************
* Function:    GpsWeekSecondAddWeek
* Description: Gps week and second add week.
* Input:       time: Gps week and second.
*              week: The number of week to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddWeek(GpsWeekSecond time, int week)
{
  time.week += week;

  return time;
}

/****************************************************************
* Function:    GpsWeekSecondAddDay
* Description: Gps week and second add day.
* Input:       time: Gps week and second.
*              day:  The number of day to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddDay(GpsWeekSecond time, int day)
{
  time.week   += day / 7;
  time.second += day % 7 * 86400;

  if(time.second > 604799)
  {
    time.week   += 1;
    time.second -= 604800;
  }

  return time;
}

/****************************************************************
* Function:    GpsWeekSecondAddHour
* Description: Gps week and second add hour.
* Input:       time: Gps week and second.
*              hour: The number of hour to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddHour(GpsWeekSecond time, int hour)
{
  time.week   += hour / 168;
  time.second += hour % 168 * 3600;

  if(time.second > 604799)
  {
    time.week   += 1;
    time.second -= 604800;
  }
  
  return time;
}

/****************************************************************
* Function:    GpsWeekSecondAddMinute
* Description: Gps week and second add minute.
* Input:       time:   Gps week and second.
*              minute: The number of minute to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddMinute(GpsWeekSecond time, int minute)
{
  time.week   += minute / 10080;
  time.second += minute % 10080 * 60;
  
  if(time.second > 604799)
  {
    time.week   += 1;
    time.second -= 604800;
  }
  
  return time;
}

/****************************************************************
* Function:    GpsWeekSecondAddSecond
* Description: Gps week and second add second.
* Input:       time:   Gps week and second.
*              second: The number of second to add.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond GpsWeekSecondAddSecond(GpsWeekSecond time, int second)
{
  time.week   += second / 604800;
  time.second += second % 604800;

  if(time.second > 604799)
  {
    time.week   += 1;
    time.second -= 604800;
  }
  
  return time;
}

/****************************************************************
* Function:    GpsWeekSecondToGregorianCalendarDate
* Description: Gps week and second to gregorian calendar date.
* Input:       time: Gps week and second.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime GpsWeekSecondToGregorianCalendarDate(GpsWeekSecond time)
{
  double jd = GpsWeekSecondToJulianDate(time);

  return JulianDateToGregorianCalendarDate(jd);
}

/****************************************************************
* Function:    GpsWeekSecondToJulianDate
* Description: Gps week and second to julian date.
* Input:       time: Gps week and second.
* Output:
* Return:      Julian date.
*****************************************************************/
double GpsWeekSecondToJulianDate(GpsWeekSecond time)
{
  double jd = 2444244.5 + time.week * 7.0 + time.second / 86400.0;

  return jd;
}

/****************************************************************
* Function:    GpsWeekSecondToModifiedJulianDate
* Description: Gps week and second to modified julian date.
* Input:       time: Gps week and second.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double GpsWeekSecondToModifiedJulianDate(GpsWeekSecond time)
{
  return GpsWeekSecondToJulianDate(time) - 2400000.5;
}

/****************************************************************
* Function:    JulianDateAddYear
* Description: Julian date add year.
* Input:       jd:   Julian date.
*              year: The number of year to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddYear(double jd, int year)
{
  DateTime date = JulianDateToGregorianCalendarDate(jd);

  date = GregorianCalendarDateAddYear(date, year);

  return GregorianCalendarDateToJulianDate(date);
}

/****************************************************************
* Function:    JulianDateAddMonth
* Description: Julian date add month.
* Input:       jd:    Julian date.
*              month: The number of month to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddMonth(double jd, int month)
{
  DateTime date = JulianDateToGregorianCalendarDate(jd);

  date = GregorianCalendarDateAddMonth(date, month);

  return GregorianCalendarDateToJulianDate(date);
}

/****************************************************************
* Function:    JulianDateAddWeek
* Description: Julian date add week.
* Input:       jd:   Julian date.
*              week: The number of week to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddWeek(double jd, int week)
{
  jd += week * 7.0;

  return jd;
}

/****************************************************************
* Function:    JulianDateAddDay
* Description: Julian date add day.
* Input:       jd:  Julian date.
*              day: The number of day to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddDay(double jd, int day)
{
  jd += day;

  return jd;
}

/****************************************************************
* Function:    JulianDateAddHour
* Description: Julian date add hour.
* Input:       jd:   Julian date.
*              hour: The number of hour to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddHour(double jd, int hour)
{
  jd += hour / 24.0;

  return jd;
}

/****************************************************************
* Function:    JulianDateAddMinute
* Description: Julian date add minute.
* Input:       jd:     Julian date.
*              minute: The number of minute to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddMinute(double jd, int minute)
{
  jd += minute / 1440.0;

  return jd;
}

/****************************************************************
* Function:    JulianDateAddSecond
* Description: Julian date add second.
* Input:       jd:     Julian date.
*              second: The number of second to add.
* Output:
* Return:      Julian date.
*****************************************************************/
double JulianDateAddSecond(double jd, int second)
{
  jd += second / 86400.0;

  return jd;
}

/****************************************************************
* Function:    JulianDateToGregorianCalendarDate
* Description: Julian date to gregorian calendar date.
* Input:       jd: Julian date.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime JulianDateToGregorianCalendarDate(double jd)
{
  int y = 4716;
  int j = 1401;
  int m = 2;
  int n = 12;
  int r = 4;
  int p = 1461;
  int v = 3;
  int u = 5;
  int s = 153;
  int w = 2;
  int b = 274277;
  int c = -38;

  int jdn = (int)(jd + 0.5);
  int f   = jdn + j + (((4 * jdn + b) / 146097) * 3) / 4 + c;
  int e   = r * f + v;
  int g   = (e % p) / r;
  int h   = u * g + w;

  DateTime time = {0};

  time.day    = (h % s) / u + 1;
  time.month  = (h / s + m) % n + 1;
  time.year   = e / p - y + (n + m - time.month) / n;
  time.hour   = (int)((jd + 0.5 - jdn) * 86400.5) / 3600;
  time.minute = (int)((jd + 0.5 - jdn) * 86400.5 - time.hour * 3600) / 60;
  time.second = (int)((jd + 0.5 - jdn) * 86400.5 - time.hour * 3600 - time.minute * 60);

  return time;
}

/****************************************************************
* Function:    JulianDateToGpsWeekSecond
* Description: Julian date to gps week and second.
* Input:       jd: Julian date.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond JulianDateToGpsWeekSecond(double jd)
{
  GpsWeekSecond time = {0};
  DateTime      date = JulianDateToGregorianCalendarDate(jd);

  time.week   = (int)(jd - 2444244.5) / 7;
  time.second = ((int)(jd - 2444244.5) - time.week * 7) * 86400 + date.hour * 3600 + date.minute * 60 + date.second;

  return time;
}

/****************************************************************
* Function:    JulianDateToModifiedJulianDate
* Description: Julian date to modified julian date.
* Input:       jd: Julian date.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double JulianDateToModifiedJulianDate(double jd)
{
  double mjd = jd - 2400000.5;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateAddYear
* Description: Modified julian date add year.
* Input:       mjd:  Modified julian date.
*              year: The number of year to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddYear(double mjd, int year)
{
  DateTime date = ModifiedJulianDateToGregorianCalendarDate(mjd);

  date = GregorianCalendarDateAddYear(date, year);

  return GregorianCalendarDateToModifiedJulianDate(date);
}

/****************************************************************
* Function:    ModifiedJulianDateAddMonth
* Description: Modified julian date add month.
* Input:       mjd:   Modified julian date.
*              month: The number of month to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddMonth(double mjd, int month)
{
  DateTime date = ModifiedJulianDateToGregorianCalendarDate(mjd);

  date = GregorianCalendarDateAddMonth(date, month);

  return GregorianCalendarDateToModifiedJulianDate(date);
}

/****************************************************************
* Function:    ModifiedJulianDateAddWeek
* Description: Modified julian date add week.
* Input:       mjd:  Modified julian date.
*              week: The number of week to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddWeek(double mjd, int week)
{
  mjd += week * 7.0;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateAddDay
* Description: Modified julian date add day.
* Input:       mjd: Modified julian date.
*              day: The number of day to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddDay(double mjd, int day)
{
  mjd += day;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateAddHour
* Description: Modified julian date add hour.
* Input:       mjd:  Modified julian date.
*              hour: The number of hour to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddHour(double mjd, int hour)
{
  mjd += hour / 24.0;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateAddMinute
* Description: Modified julian date add minute.
* Input:       mjd:    Modified julian date.
*              minute: The number of minute to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddMinute(double mjd, int minute)
{
  mjd += minute / 1440.0;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateAddSecond
* Description: Modified julian date add second.
* Input:       mjd:    Modified julian date.
*              second: The number of second to add.
* Output:
* Return:      Modified julian date.
*****************************************************************/
double ModifiedJulianDateAddSecond(double mjd, int second)
{
  mjd += second / 86400.0;

  return mjd;
}

/****************************************************************
* Function:    ModifiedJulianDateToGregorianCalendarDate
* Description: Modified julian date to gregorian calendar date.
* Input:       mjd: Modified julian date.
* Output:
* Return:      Gregorian calendar date.
*****************************************************************/
DateTime ModifiedJulianDateToGregorianCalendarDate(double mjd)
{
  return JulianDateToGregorianCalendarDate(mjd + 2400000.5);
}

/****************************************************************
* Function:    ModifiedJulianDateToGpsWeekSecond
* Description: Modified julian date to gps week and second.
* Input:       mjd: Modified julian date.
* Output:
* Return:      Gps week and second.
*****************************************************************/
GpsWeekSecond ModifiedJulianDateToGpsWeekSecond(double mjd)
{
  return JulianDateToGpsWeekSecond(mjd + 2400000.5);
}

/****************************************************************
* Function:    ModifiedJulianDateToJulianDate
* Description: Modified julian date to julian date.
* Input:       mjd: Modified julian date.
* Output:
* Return:      Julian date.
*****************************************************************/
double ModifiedJulianDateToJulianDate(double mjd)
{
  double jd = mjd + 2400000.5;

  return jd;
}[/mw_shl_code]
回复 支持 反对

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-2-2 22:07:13 | 显示全部楼层
      main.c文件
[mw_shl_code=c,true]/****************************************************************
* Copyright (C) 2017, XinLi, all right reserved.
* File name:    main.c
* Date:         2017.10.17
* Description:  GPS, UTC and local time displays.
*****************************************************************/

/****************************************************************
*                        Header include
*****************************************************************/
#include "DateTime.h"
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

/****************************************************************
*                       Global variables
*****************************************************************/


/****************************************************************
*                     Function declaration
*****************************************************************/
static void gotoxy(int x, int y);

/****************************************************************
*                     Function definition
*****************************************************************/

/****************************************************************
* Function:    main
* Description: Program entry.
* Input:
* Output:
* Return:
*****************************************************************/
int main(void)
{
  for(;;)
  {
    time_t        times     = 0;
    double        mjd       = 0.0;
    DateTime      utctime   = {.year = 1970, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0};
    DateTime      localtime = {.year = 1970, .month = 1, .day = 1, .hour = 8, .minute = 0, .second = 0};
    DateTime      gpstime   = {.year = 1970, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0};
    GpsWeekSecond gpstimews = {0};

    time(×);

    if(times > INT32_MAX)
    {
      utctime = GregorianCalendarDateAddSecond(utctime, INT32_MAX);
      utctime = GregorianCalendarDateAddSecond(utctime, (int)(times - INT32_MAX));
    }
    else
    {
      utctime = GregorianCalendarDateAddSecond(utctime, (int)times);
    }

    mjd       = GregorianCalendarDateToModifiedJulianDate(utctime);
    localtime = GregorianCalendarDateAddHour(utctime, 8);
    gpstime   = GregorianCalendarDateAddSecond(utctime, 18);
    gpstimews = GregorianCalendarDateToGpsWeekSecond(gpstime);

    gotoxy(0, 0);

    printf("Local | %d-%.2d-%.2d %.2d:%.2d:%.2d | timezone UTC+8\n",
           localtime.year, localtime.month, localtime.day,
           localtime.hour, localtime.minute, localtime.second);

    printf("UTC   | %d-%.2d-%.2d %.2d:%.2d:%.2d | MJD %.5f\n",
           utctime.year, utctime.month, utctime.day,
           utctime.hour, utctime.minute, utctime.second,
           mjd);

    printf("GPS   | %d-%.2d-%.2d %.2d:%.2d:%.2d | week %d %d s\n",
           gpstime.year, gpstime.month, gpstime.day,
           gpstime.hour, gpstime.minute, gpstime.second,
           gpstimews.week, gpstimews.second);

    Sleep(100);
  }
}
[/mw_shl_code]
回复 支持 反对

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-2-2 22:07:38 | 显示全部楼层
[mw_shl_code=c,true]
/****************************************************************
* Function:    gotoxy
* Description: Move the cursor to the specified position on the text screen.
* Input:       x: X axis coordinates.
*              y: Y axis coordinates.
* Output:
* Return:
*****************************************************************/
static void gotoxy(int x, int y)
{
  COORD  pos  = {x, y};
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleCursorPosition(hOut, pos);
}[/mw_shl_code]
回复 支持 反对

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-2-2 22:08:32 | 显示全部楼层
3,运行效果
RunningResult.jpg

回复 支持 反对

使用道具 举报

14

主题

58

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
213
金钱
213
注册时间
2018-2-1
在线时间
40 小时
 楼主| 发表于 2018-3-6 22:44:26 | 显示全部楼层
可以直接移植到嵌入式端,非常方便。
回复 支持 反对

使用道具 举报

1

主题

5

帖子

0

精华

新手入门

积分
12
金钱
12
注册时间
2018-5-24
在线时间
1 小时
发表于 2018-5-24 13:01:42 | 显示全部楼层
楼主好人,有gps解析经纬度和高度的程序吗?
回复 支持 反对

使用道具 举报

9

主题

37

帖子

0

精华

初级会员

Rank: 2

积分
142
金钱
142
注册时间
2018-3-28
在线时间
16 小时
发表于 2018-6-1 13:54:17 | 显示全部楼层
它返回的那个值怎么看啊,要怎么做才能实现通过图形界面显示出位置
回复 支持 反对

使用道具 举报

0

主题

138

帖子

0

精华

初级会员

Rank: 2

积分
190
金钱
190
注册时间
2019-4-2
在线时间
3 小时
发表于 2019-4-9 08:27:08 | 显示全部楼层
有gps解析经纬度和高度的程序吗?
回复 支持 反对

使用道具 举报

0

主题

295

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
335
金钱
335
注册时间
2019-3-26
在线时间
9 小时
发表于 2019-4-10 14:39:34 | 显示全部楼层
谢谢老司机分享!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-25 11:54

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表