📄 rvtime.c
字号:
static void rvSetClock(void)
{
RvTm timetm;
RvTime epochsecs;
RvTimespec epochtime;
char linebuf[81];
memset(&timetm, 0, sizeof(timetm)); /* zero out time structure */
for(;;) {
printf("Enter today's date (mm/dd/yyyy): ");
fflush(stdout);
fflush(stdin);
fgets(linebuf, sizeof(linebuf), stdin);
if(sscanf(linebuf, "%d / %d / %d", &timetm.tm_mon, &timetm.tm_mday, &timetm.tm_year) != 3) {
printf("\nInvalid date format, try again.\n");
continue;
}
if((timetm.tm_mon < 1) || (timetm.tm_mon > 12)) {
printf("Invalid Month.\n");
continue;
}
if((timetm.tm_mday < 1) || (timetm.tm_mday > 31)) {
printf("Invalid Day.\n");
continue;
}
if((timetm.tm_year < 0) || (timetm.tm_year > 2037) ||
((timetm.tm_year > 99) && (timetm.tm_year < 1900))){
printf("Invalid Year.\n");
continue;
}
timetm.tm_mon -= 1; /* month starts at 0 */
break;
}
/* tm_year format is # of years since 1900 */
if(timetm.tm_year >= 1900) {
timetm.tm_year -= 1900;
} else {
if(timetm.tm_year < 70)
timetm.tm_year += 100;
printf("Assuming year to be %d.\n", (1900 + timetm.tm_year));
}
for(;;) {
printf("Enter the current time (hh:mm:ss): ");
fflush(stdout);
fflush(stdin);
fgets(linebuf, sizeof(linebuf), stdin);
if(sscanf(linebuf, "%d : %d : %d", &timetm.tm_hour, &timetm.tm_min, &timetm.tm_sec) != 3) {
printf("Invalid time format, try again.\n");
continue;
}
if((timetm.tm_hour < 0) || (timetm.tm_hour > 23)) {
printf("Invalid Hour.\n");
continue;
}
if((timetm.tm_min < 0) || (timetm.tm_min > 59)) {
printf("Invalid Minutes.\n");
continue;
}
if((timetm.tm_sec < 0) || (timetm.tm_sec > 59)) {
printf("Invalid Seconds.\n");
continue;
}
break;
}
epochsecs = rvTimeConvertTm(&timetm);
if(epochsecs < 0) {
printf("Error converting time and date to seconds since epoch.\n");
printf("Time not set.\n");
} else {
epochtime.tv_sec = epochsecs;
epochtime.tv_nsec = 0;
if(rvTimeSetEpochTime(&epochtime) != rvTrue) {
printf("Error setting time with rvTimeSetEpochTime. Time not set.\n");
} else {
rvTimeAsctime(&timetm, linebuf, sizeof(linebuf));
printf("Time set to: %s\n", linebuf);
}
}
}
/* Deal with difference and bugs in printing 64 bit numbers. */
/* Used RvHrTime type to have one declaration for all OS's. */
/* Just output signed 64 bit number as a decimal to stdout. */
static void rvPrint64(RvHrtime num64)
{
#if defined(RV_OS_SOLARIS) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_REDHAT)
printf("%lld", num64);
#endif
#if defined(RV_OS_WIN32)
printf("%I64d", num64);
#endif
#if defined(RV_OS_VXWORKS) || defined(RV_OS_PSOS) || defined(RV_OS_OSE) || defined(RV_OS_NUCLEUS)
/* printf is broken, have to do it manually */
char *cptr, buf[22]; /* maximum length of 64 bit number */
int numsign, temp;
if(num64 < 0LL) {
numsign = -1;
num64 *= -1LL;
} else numsign = 1;
buf[21] = '\0';
cptr = &buf[20];
do{
temp = (int)(num64 % 10LL);
num64 /= 10LL;
*cptr = '0' + (char)temp;
cptr -= 1;
} while(num64 > 0);
if(numsign < 0) {
*cptr = '-';
} else *cptr = ' ';
printf("%s",cptr);
#endif
}
/* Deal with difference and bugs in printing 64 bit numbers. */
/* Used RvNtpTime64 type to have one declaration for all OS's. */
/* Just output unsigned 64 bit number ain hex to stdout. */
static void rvPrint64Hex(RvNtpTime64 num64)
{
#if defined(RV_OS_SOLARIS) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_REDHAT)
printf("0x%llx", num64);
#endif
#if defined(RV_OS_WIN32)
printf("0x%I64x", num64);
#endif
#if defined(RV_OS_VXWORKS) || defined(RV_OS_PSOS) || defined(RV_OS_OSE) || defined(RV_OS_NUCLEUS)
/* printf is broken, have to do it in pieces */
unsigned long msb, lsb;
int shiftnum;
shiftnum = 32; /* compiler workaround */
msb = (unsigned long)(num64 >> shiftnum);
lsb = (unsigned long)(num64);
printf("0x%lx%08lx", msb, lsb);
#endif
}
void rvTimeTest(void)
{
RvBool initcheck;
RvTime epochsecs, temptime, epochsecs2, deltatime, epochsecs3;
RvTimespec epochtime, hirestime, temptimespec, epochtime2, deltatimespec, epochtime3, hirestime2, deltahirestime;
RvHrtime nanosecs, temphrtime, nanosecs2, deltananosecs, nanosecs3;
RvTm epochtm, *temptm;
RvNtpTime epochntp, epochntp2, epochntp3, deltantp, tempntp, *tempntp_ptr;
RvNtpTime64 epochntp64;
char strbuf[RV_TIME_ASCTIME_BUFSIZE], *bufptr, inputbuf[81], outputbuf[81];
int num;
#if defined(RV_OS_VXWORKS)
/* We need to set stdio to the serial channel */
int serialport;
serialport = open("/tyCo/0", O_RDWR, 0);
ioTaskStdSet(0, STD_IN, serialport);
ioTaskStdSet(0, STD_OUT, serialport);
ioTaskStdSet(0, STD_ERR, serialport);
#endif
/* Test to see if everything works. Assumes printf is valid. */
printf("Starting test of rvtime.c :\n");
initcheck = rvTimeInit();
if(initcheck == rvFalse) {
printf("rvTimeInit failed, not much else to do.\n");
return;
}
printf("rvTimeInit successfull.\n");
#if defined(RV_OS_REDHAT) || defined(RV_OS_WIN32) || defined(RV_OS_VXWORKS) || defined(RV_OS_PSOS) || defined(RV_OS_NUCLEUS)
printf("tick_hz = ");
rvPrint64(tick_hz);
printf("\n");
#endif
#if defined(RV_OS_OSE)
printf("tick_period = ");
rvPrint64(tick_period);
printf("\n");
#endif
#if (defined(RV_OS_VXWORKS) && !defined(RV_VXWORKS_USETIMESTAMP)) || defined(RV_OS_OSE) || defined(RV_OS_NUCLEUS)
printf("wrap_count = %ld\n", wrap_count);
printf("last_tick = %lu\n", last_tick);
printf("WRAP_TICKS = 0x%X\n", WRAP_TICKS);
#endif
#if defined(RV_OS_VXWORKS) && defined(RV_VXWORKS_USETIMESTAMP)
printf("sysTimestampFreq = %lu\n", sysTimestampFreq());
printf("sysTimestampPeriod = %lu\n", sysTimestampPeriod());
#endif
#if defined(RV_OS_OSE)
printf("WRAP_MS_MAX = %lu\n", WRAP_MS_MAX);
printf("wrapcheck_ms = %lu\n", wrapcheck_ms);
#endif
rvTimeGetTimeRes(&epochtime);
printf("Wall clock resolution:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", epochtime.tv_sec, epochtime.tv_nsec);
rvTimeGetHrtimeRes(&hirestime);
printf("Hi resolution clock resolution:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", hirestime.tv_sec, hirestime.tv_nsec);
rvSetClock();
#if defined(RV_OS_NUCLEUS)
printf("timezero = %ld\n", timezero);
#endif
/* Get inital time values and check conversions */
epochsecs = rvTimeGetEpochTime(&epochtime);
printf("Current time from rvTimeGetEpochTime:\n");
printf(" seconds only = %ld\n", epochsecs);
printf(" seconds = %ld, nanoseconds = %ld\n", epochtime.tv_sec, epochtime.tv_nsec);
temptime = rvTimeConvertsecs(&epochtime);
printf("Using rvTimeConvertsecs to get seconds = %ld\n", temptime);
nanosecs = rvTimeConvertHrtime(&epochtime);
printf("Using rvTimeConvertHrtime to get total nanoseconds = ");
rvPrint64(nanosecs);
printf("\n");
if(rvTimespecCreate(&temptimespec, temptime, (long)(nanosecs % 1000000000L)) != NULL) {
printf("Using rvTimepecCreate to get timespec:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", temptimespec.tv_sec, temptimespec.tv_nsec);
} else printf("Error using rvTimespecCreate.\n");
#if defined(RV_OS_WIN32)
tempntp_ptr = rvNtpTimeCreate(&epochntp, (unsigned long)epochtime.tv_sec + NTPDELTA,
(unsigned long)(((ULONGLONG)epochtime.tv_nsec * 0x100000000Ui64) / 1000000000Ui64));
#else
tempntp_ptr = rvNtpTimeCreate(&epochntp, (unsigned long)epochtime.tv_sec + NTPDELTA,
(unsigned long)(((unsigned long long)epochtime.tv_nsec * 0x100000000ULL) / 1000000000ULL));
#endif
if(tempntp_ptr != NULL) {
printf("Creating NTP time with rvNtpTimeCreate using current time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", epochntp.secs, epochntp.fraction);
} else printf("Error creating NTP time with rvNtpTimeCreate.\n");
if(rvNtpTimeChop(&tempntp, &epochntp, 32, 4) != NULL) {
printf("Chopping NTP time down to 32:4 with rvNtpTimeChop:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
epochntp64 = rvNtpTimeChop64(&epochntp, 32, 4);
printf("64 bit NTP chop = ");
rvPrint64Hex(epochntp64);
printf("\n");
} else printf("Error chopping NTP down to 32:4.\n");
if(rvTimeConvertNTP(&epochtime, &epochntp, RV_NTP_TIME_ABSOLUTE) != NULL) {
printf("Using rvTimeConvertNTP to get NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", epochntp.secs, epochntp.fraction);
if(rvNtpTimeConvert(&epochntp, &temptimespec, RV_NTP_TIME_ABSOLUTE) != NULL) {
printf("Using rvNtpTimeConvert to get back standard time:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", temptimespec.tv_sec, temptimespec.tv_nsec);
} else printf("Error getting timspec back from rvNtpTimeConvert.\n");
epochntp64 = rvNtpTimeChop64(&epochntp, 32, 32);
printf("64 bit NTP time value = ");
rvPrint64Hex(epochntp64);
printf("\n");
if(rvNtpTimeBuild64(&tempntp, epochntp64, 32) != NULL) {
printf("Using rvNtpTimeBuild64 get back to NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
} else printf("Error converting from 64 bit NTP back to standarad NTP.\n");
if(rvNtpTimeChop(&tempntp, &epochntp, 16, 16) != NULL) {
printf("Chopping NTP time down to 16:16 with rvNtpTimeChop:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
epochntp64 = rvNtpTimeChop64(&epochntp, 16, 16);
printf("64 bit NTP chop = ");
rvPrint64Hex(epochntp64);
printf("\n");
if(rvNtpTimeBuild64(&tempntp, epochntp64, 16) != NULL) {
printf("Using rvNtpTimeBuild64 get back to NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
} else printf("Error converting from 64 bit NTP back to standarad NTP.\n");
} else printf("Error chopping NTP down to 16:16.\n");
} else printf("Error getting NTP time from rvTimeConvertNTP.\n");
temptm = rvTimeUTC(epochsecs, &epochtm);
if(temptm != NULL) {
printf("UTC time information from rvTimeUTC:\n");
printf(" seconds = %d\n", epochtm.tm_sec);
printf(" minutes = %d\n", epochtm.tm_min);
printf(" hours = %d\n", epochtm.tm_hour);
printf(" day of month = %d\n", epochtm.tm_mday);
printf(" month = %d\n", epochtm.tm_mon);
printf(" year = %d\n", epochtm.tm_year);
printf(" weekday = %d\n", epochtm.tm_wday);
printf(" day of year = %d\n", epochtm.tm_yday);
printf(" daylight savings = %d\n", epochtm.tm_isdst);
} else printf("Error getting UTC time from rvTimeUTC.\n");
bufptr = rvTimeAsctime(&epochtm, strbuf, sizeof(strbuf));
if(bufptr != NULL) {
printf("UTC time output from rvTimeAsctime: %s", strbuf);
num = rvTimeStrTm(outputbuf, 81, "%A %B %d %Y %I:%M:%S%p", &epochtm);
printf("Using rvTimeStrTm for string. Value = %d result:\n %s\n", num, outputbuf);
} else printf("Error getting time string from rvTimeAsctime.\n");
temptm = rvTimeLocal(epochsecs, &epochtm);
if(temptm != NULL) {
bufptr = rvTimeAsctime(&epochtm, strbuf, sizeof(strbuf));
if(bufptr != NULL) {
printf("Local time output from rvTimeAsctime: %s", strbuf);
} else printf("Error getting time string from rvTimeAsctime.\n");
} else printf("Error getting Local time from rvTimeLocal.\n");
temptime = rvTimeConvertTm(&epochtm);
printf("Using rvTimeConvertTm to get seconds since 1970 = %ld\n", temptime);
/* Get and track a seperate hires time */
rvTimeGetHires(&hirestime);
printf("High resolution time from rvTimeGetHires:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", hirestime.tv_sec, hirestime.tv_nsec);
temphrtime = rvTimeConvertHrtime(&hirestime);
printf("High resolution time convert to hrtime = ");
rvPrint64(temphrtime);
printf("\n");
temphrtime = rvTimeGetHrtime();
printf("High resolution time direct with rvTimeGetHrtime = ");
rvPrint64(temphrtime);
printf("\n");
/* Let user wait for some time to pass */
printf("\nWait a few seconds and then press ENTER (optionally change system clock): ");
fflush(stdout);
fflush(stdin);
fgets(inputbuf, sizeof(inputbuf), stdin);
printf("\n");
#if (defined(RV_OS_VXWORKS) && !defined(RV_VXWORKS_USETIMESTAMP)) || defined(RV_OS_OSE) || defined(RV_OS_NUCLEUS)
printf("wrap_count = %ld\n", wrap_count);
printf("last_tick = %lu\n", last_tick);
#endif
#if defined(RV_OS_VXWORKS) && defined(RV_VXWORKS_USETIMESTAMP)
printf("hires_count = ");
rvPrint64(hires_count);
printf("\n");
#endif
/* Get new time and convert to various formats. */
epochsecs2 = rvTimeGetEpochTime(&epochtime2);
printf("New time from rvTimeGetEpochTime:\n");
printf(" seconds only = %ld\n", epochsecs2);
printf(" seconds = %ld, nanoseconds = %ld\n", epochtime2.tv_sec, epochtime2.tv_nsec);
nanosecs2 = rvTimeConvertHrtime(&epochtime2);
printf("Using rvTimeConvertHrtime to get total nanoseconds = ");
rvPrint64(nanosecs2);
printf("\n");
if(rvTimeConvertNTP(&epochtime2, &epochntp2, RV_NTP_TIME_ABSOLUTE) != NULL) {
printf("Using rvTimeConvertNTP to get NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", epochntp2.secs, epochntp2.fraction);
if(rvNtpTimeChop(&tempntp, &epochntp2, 16, 8) != NULL) {
printf("Chopping NTP time down to 16:8 with rvNtpTimeChop:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
epochntp64 = rvNtpTimeChop64(&epochntp2, 16, 8);
printf("64 bit NTP chop = ");
rvPrint64Hex(epochntp64);
printf("\n");
if(rvNtpTimeBuild64(&tempntp, epochntp64, 8) != NULL) {
printf("Using rvNtpTimeBuild64 get back to NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
} else printf("Error converting from 64 bit NTP back to standarad NTP.\n");
} else printf("Error chopping NTP down to 16:8.\n");
} else printf(" rvTimeConvertNTP failed.\n");
/* Calculate time deltas using subtract in various formats. */
deltatime = rvTimeSubtractSecs(epochsecs2, epochsecs);
printf("Using rvTimeSubtractSecs to get delta seconds = %ld\n", deltatime);
if(rvTimeSubtract(&deltatimespec, &epochtime2, &epochtime) != NULL) {
printf("Using rvTimeSubtract to get delta time:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", deltatimespec.tv_sec, deltatimespec.tv_nsec);
} else printf("Error in rvTimeSubtract.\n");
if(rvTimeConvertNTP(&deltatimespec, &tempntp, RV_NTP_TIME_RELATIVE) != NULL) {
printf("Using rvTimeConvertNTP to get relative NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
} else printf(" rvTimeConvertNTP relative failed.\n");
deltananosecs = rvTimeHrSubtract(nanosecs2, nanosecs);
printf("Using rvTimeHrSubtract to get delta nanoseconds = ");
rvPrint64(deltananosecs);
printf("\n");
if(rvNtpTimeSubtract(&deltantp, &epochntp2, &epochntp) != NULL) {
printf("Using rvNtpTimeSubtract to get delta NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", deltantp.secs, deltantp.fraction);
rvNtpTimeConvert(&deltantp, &temptimespec, RV_NTP_TIME_RELATIVE);
printf(" Converted NTP delta to timespec:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", temptimespec.tv_sec, temptimespec.tv_nsec);
} else printf(" Error in rvNtpTimeSubtract.\n");
/* Add time delas back to original times to check add functions. */
printf("Now adding differences back to orignals:\n");
epochsecs3 = rvTimeAddSecs(deltatime, epochsecs);
printf(" seconds = %ld\n", epochsecs3);
if(rvTimeAdd(&epochtime3, &deltatimespec, &epochtime) != NULL) {
printf(" seconds = %ld, nanoseconds = %ld\n", epochtime3.tv_sec, epochtime3.tv_nsec);
} else printf(" Error in rvTimeAdd.\n");
nanosecs3 = rvTimeHrAdd(deltananosecs, nanosecs);
printf(" nanoseconds = ");
rvPrint64(nanosecs3);
printf("\n");
if(rvNtpTimeAdd(&epochntp3, &deltantp, &epochntp) != NULL) {
printf(" seconds = %lu, fraction = 0x%lx\n", epochntp3.secs, epochntp3.fraction);
rvNtpTimeConvert(&epochntp3, &temptimespec, RV_NTP_TIME_ABSOLUTE);
printf(" seconds = %ld, nanoseconds = %ld\n", temptimespec.tv_sec, temptimespec.tv_nsec);
if(rvNtpTimeChop(&tempntp, &epochntp, 24, 32) != NULL) {
printf("Chopping NTP time down to 24:32 with rvNtpTimeChop:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
epochntp64 = rvNtpTimeChop64(&epochntp, 24, 32);
printf("64 bit NTP chop = ");
rvPrint64Hex(epochntp64);
printf("\n");
if(rvNtpTimeBuild64(&tempntp, epochntp64, 32) != NULL) {
printf("Using rvNtpTimeBuild64 get back to NTP time:\n");
printf(" seconds = %lu, fraction = 0x%lx\n", tempntp.secs, tempntp.fraction);
} else printf("Error converting from 64 bit NTP back to standarad NTP.\n");
} else printf("Error chopping NTP down to 24:32.\n");
} else printf(" Error in rvNtpTimeAdd.\n");
/* Do one last hires time to check basic linear hires time-clock */
rvTimeGetHires(&hirestime2);
printf("New high resolution time from rvTimeGetHires:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", hirestime2.tv_sec, hirestime2.tv_nsec);
if(rvTimeSubtract(&deltahirestime, &hirestime2, &hirestime) != NULL) {
printf("Using rvTimeSubtract to get delta hires time:\n");
printf(" seconds = %ld, nanoseconds = %ld\n", deltahirestime.tv_sec, deltahirestime.tv_nsec);
} else printf("Error in rvTimeSubtract\n");
rvTimeEnd();
printf("\nTest of rvtime.c complete.\n");
}
#if defined(RV_OS_SOLARIS) || defined(RV_OS_REDHAT) || defined(RV_OS_TRU64) || defined(RV_OS_HPUX) || defined(RV_OS_WIN32)
main()
{
rvTimeTest();
}
#endif
#endif /* RV_TEST_CODE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -