📄 timetol.c
字号:
/*===========================================================================*//* SEED reader | timetol | utility *//*===========================================================================*//* Name: timetol Purpose: compare two times int struct time format Usage: int timetol (); struct time time1; struct time time2; int result; result = timetol (time1, time2); Input: time1 = 1st time in struct time format (see Notes) time2 = 2nd time in struct time format (see Notes) Output: result = 0 if time1 == time2 = -1 if time1 < time2 = 1 if time1 > time2 Externals: none Warnings: none Errors: none Called by: anything Calls to: none Algorithm: check each element of the time structure; if an inequality is found, return the appropriate value. If no inequalities are found, times are equal. Checking is done in the order fractional seconds, seconds, . . . , year due to decreasing likelihood of inequality. Notes: The time structure looks like: struct time { int year; int day; int hour; int minute; int second; int fracsec; }; Problems: none known References: none Language: C, hopefully ANSI standard Author: Dennis O'Neill Revisions: Date Revised by Comment 07/15/88 Dennis O'Neill Initial preliminary release 0.9 11/11/88 Dennis O'Neill added tolerance to fractional second comparison to allow for roundoff 11/21/88 Dennis O'Neill Production release 1.0*/#include "rdseed.h"/* #define isaleap(year) (((year%100 != 0) && (year%4 == 0)) || (year%400 == 0)) */print_t (t) struct time t;{printf ("time = %04d, %03d, %02d:%02d:%02d.%04d\n", t.year, t.day, t.hour, t.minute, t.second, t.fracsec);}double epochtime (t) struct time t; { double etime; int i; etime = ((double)t.fracsec)/10000.0; etime += t.second; etime += t.minute * 60; etime += t.hour * 3600; etime += (t.day-1) * 3600 * 24; for (i = 1971; i <= t.year; i++) { if (isaleap(i - 1)) etime += 366*24*3600; else etime += 365*24*3600; } return (etime);}int timetol (time1, time2, nsamples, samplesPerSec)struct time time1;struct time time2;int nsamples;float samplesPerSec;{ double diff_sec; double sec1, sec2; /* check year */ sec1 = epochtime(time1); sec2 = epochtime(time2); diff_sec = ((double) sec1) - ((double)sec2); diff_sec += (time1.fracsec - time2.fracsec)/10000.0; // if (diff_sec < 0) // diff_sec = -diff_sec; /* check fractional second */ if (current_channel != NULL) { if (current_channel->clock_tolerance > 0) { if ((diff_sec > -(nsamples * current_channel->clock_tolerance * adj_tolerance)) && (diff_sec < (nsamples * current_channel->clock_tolerance * adj_tolerance))) return (0); else { if (diff_sec < 0) return -1; if (diff_sec > 0) return 1; } } else { if (diff_sec > -samplesPerSec && diff_sec < samplesPerSec) return 0; if (diff_sec < 0) return -1; if (diff_sec > 0) return 1; } } else { if (diff_sec < -(.0001)) return (-1); if (diff_sec > .0001 ) return (1); } /* if I got this far, times are equal */ return (0);}int timetolold (time1, time2, nsamples, samplesPerSec)struct time time1;struct time time2;int nsamples;float samplesPerSec;{ double diff_sec; unsigned int sec1, sec2; /* check year */ sec1 = time1.second; sec1 += time1.minute*60; /* how many secs in a minute */ sec1 += time1.hour*3600; /* # of secs in an hour */ sec1 += time1.day*3600*24; /* num = #secs in hour * 24 */ if isaleap(time1.year) sec1 += ((time1.year-1980)*3600)*24*366; else sec1 += ((time1.year-1980)*3600)*24*365; sec2 = time2.second; sec2 += time2.minute*60; sec2 += time2.hour*3600; sec2 += time2.day*3600*24; /* add in the years */ if isaleap(time1.year) sec2 += ((time2.year-1980)*3600)*24*366; else sec2 += ((time2.year-1980)*3600)*24*365; diff_sec = ((double) sec1) - ((double)sec2); diff_sec += (time1.fracsec - time2.fracsec)/10000.0; // if (diff_sec < 0) // diff_sec = -diff_sec; /* check fractional second */ if (current_channel != NULL) { if (current_channel->clock_tolerance > 0) { if ((diff_sec > -(nsamples * current_channel->clock_tolerance * adj_tolerance)) && (diff_sec < (nsamples * current_channel->clock_tolerance * adj_tolerance))) return (0); else { if (diff_sec < 0) return -1; if (diff_sec > 0) return 1; } } else { if (diff_sec > -samplesPerSec && diff_sec < samplesPerSec) return 0; if (diff_sec < 0) return -1; if (diff_sec > 0) return 1; } } else { if (diff_sec < -(.0001)) return (-1); if (diff_sec > .0001 ) return (1); } /* if I got this far, times are equal */ return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -