/* * Time compare function returning -1/0/+1 depending * which parameter presents time before the other. * Zero means equals. */ int tv_timercmp(struct timeval * const a, struct timeval * const b) { // if (debug>3) { // int dt_sec = a->tv_sec - b->tv_sec; // int dt_usec = a->tv_usec - b->tv_usec; // printf("tv_timercmp(%d.%06d <=> %d.%06d) dt=%d:%06d ret= ", // a->tv_sec, a->tv_usec, b->tv_sec, b->tv_usec, dt_sec, dt_usec); // } // Time delta calculation to avoid year 2038 issue const int dt = timecmp(a->tv_sec, b->tv_sec); if (dt != 0) { // if (debug>3) printf("%ds\n", dt); return dt; } // tv_usec is always in range 0 .. 999 999 if (a->tv_usec < b->tv_usec) { // if (debug>3) printf("-1u\n"); return -1; } if (a->tv_usec > b->tv_usec) { // if (debug>3) printf("1u\n"); return 1; } // if (debug>3) printf("0\n"); return 0; // equals! }
gint mbb_time_ebcmp(time_t ta, time_t tb) { if (ta == 0 || tb == 0) return 1; return timecmp(ta, tb); }
void push_event(TimeoutEvent *event, Queue *queue) { TimeoutEvent *stepper = queue->last; event->prev = event->next = NULL; oma_debug_print("Pushing to queue...\n"); fflush(stdout); if (stepper == NULL) { queue->first = queue->last = event; } else { while (stepper != NULL && timecmp(stepper->time, event->time) <= 0) { stepper = stepper->prev; } if (stepper == NULL) { queue->first->prev = event; event->next = queue->first; queue->first = event; } else { insque(event, stepper); if (event->next == NULL) { queue->last = event; } } } queue->size++; }
static void _flush ( struct logz_file_def *filedef, int wd, int *prev_wd) { struct stat stats; char const *name = filedef->name; if (filedef->fd == -1) return; if (fstat (filedef->fd, &stats) != 0) { filedef->errnum = errno; logz_close_fd (filedef->fd, name); filedef->fd = -1; return; } if (S_ISREG (filedef->mode) && stats.st_size < filedef->size) { LOGGER_ERROR("%s: file truncated", name); *prev_wd = wd; lseek (filedef->fd, stats.st_size, SEEK_SET); filedef->size = stats.st_size; } else if (S_ISREG (filedef->mode) && stats.st_size == filedef->size && timecmp (filedef->mtime, mtime_to_spec(&stats)) == 0) return; if (wd != *prev_wd) { *prev_wd = wd; } trigger_writer (name, filedef); }
gint mbb_time_eecmp(time_t ta, time_t tb) { if (ta == 0 && tb == 0) return 0; if (ta == 0) return 1; if (tb == 0) return -1; return timecmp(ta, tb); }
static int timesub(struct timeval *td, struct timeval *t0, struct timeval *t1) { struct timeval t; if (timecmp(t0, t1) < 0) return -1; t.tv_sec = t0->tv_sec; t.tv_usec = t0->tv_usec; while (t.tv_usec < t1->tv_usec) { t.tv_sec--; t.tv_usec += 1000000; } td->tv_sec = t.tv_sec - t1->tv_sec; td->tv_usec = t.tv_usec - t1->tv_usec; return 0; }
static int oldest_packet(int nics, sem_t* semaphore){ int oldest = -1; while( oldest == -1 ){ // Loop while we havent gotten any pkts. if ( terminateThreads>0 ) { return -1; } struct picotime timeOldest; // timestamp of oldest packet timeOldest.tv_sec = UINT32_MAX; timeOldest.tv_psec = UINT64_MAX; for( int i = 0; i < nics; i++){ const int readpos = _CI[i].readpos; const struct write_header* whead = CI_packet(&_CI[i], readpos); const struct cap_header* head = whead->cp; /* This destination has no packages yet */ if( whead->used == 0 ) { continue; } if( timecmp(&head->ts, &timeOldest) < 0 ){ timeOldest.tv_sec = head->ts.tv_sec; timeOldest.tv_psec = head->ts.tv_psec; oldest = i; } } /* No packages, wait until some arrives */ if ( oldest==-1 ){ int ret = wait_for_capture(semaphore); if ( ret == ETIMEDOUT ){ return -2; } } } return oldest; }
static int branch_compare(const void *l1, const void *l2) { const struct branch *branch1 = ((const struct line *) l1)->data; const struct branch *branch2 = ((const struct line *) l2)->data; if (branch_is_all(branch1)) return -1; else if (branch_is_all(branch2)) return 1; switch (get_sort_field(branch_sort_state)) { case SORT_FIELD_DATE: return sort_order(branch_sort_state, timecmp(&branch1->time, &branch2->time)); case SORT_FIELD_AUTHOR: return sort_order(branch_sort_state, ident_compare(branch1->author, branch2->author)); case SORT_FIELD_NAME: default: return sort_order(branch_sort_state, strcmp(branch1->ref->name, branch2->ref->name)); } }
/* ------------------------------------------------------------------------ */ void time_span_out () { int i; /* counter */ struct data_hdr temp_hdr; /* data header */ struct time end_time; /* block ending time */ unsigned int offset; /* local computations */ double duration; struct type52 *save_channel_ptr = current_channel; struct twindow *twin; /* * if output is intended for a seed volume, update the blockette info 74 * only... * */ if (Seed_flag) { update_type74(data_hdr); data_hdr->nsamples = 0; return; } /* +=======================================+ */ /*================| Look for Time Spans |================*/ /* +=======================================+ */ if (read_summary_flag) { char start[23]; char end[23]; struct time endtime; struct time starttime; duration = ((double)(data_hdr->nsamples)*TIME_PRECISION)/data_hdr->sample_rate; endtime = timeadd_double(data_hdr->time, duration); #if 0 sprintf(start, "%d,%d,%d:%d:%d.%d", data_hdr->time.year, data_hdr->time.day, data_hdr->time.hour, data_hdr->time.minute, data_hdr->time.second, data_hdr->time.fracsec); sprintf(end, "%d,%d,%d:%d:%d.%d", endtime.year, endtime.day, endtime.hour, endtime.minute, endtime.second, endtime.fracsec); if (input_file_type == DISK_DEVICE) /* if from disk drive, already scanned the * block 74s for matching data, just use * what was scanned previously */ twin = get_twin_from_summary(); else /* tape drive seed volume, we must scan the * summary file for "clipping" */ twin = scan_summary_file(data_hdr->station, data_hdr->channel, data_hdr->network, data_hdr->location, data_hdr->time, endtime); if (twin == NULL) { fprintf(stderr, "Error - time_span_out(): Unable to locate timespan in the summary file!\nStation: %s;Channel: %s\n for timespan %s to %s \n", data_hdr->station, data_hdr->channel, start, end); fprintf(stderr, "Ignoring summary file for this timespan.\n"); output_data(data_hdr, 0); data_hdr->nsamples = 0; return; } timecvt(&starttime, twin->time_start); timecvt(&endtime, twin->time_end); start_time_point = &starttime; end_time_point = &endtime; start_time_count = end_time_count = 1; #endif set_event(&(data_hdr->time), &endtime); } /* if user didn't enter any time spans, output all data */ if (start_time_count == 0) { output_data(data_hdr, 0); data_hdr->nsamples = 0; return; } /* look through time span list as entered by the user */ for (i=0;i<start_time_count;i++) { current_channel = save_channel_ptr; /* copy current data header */ temp_hdr = *data_hdr; /* is span start equal to start of data? */ if (timecmp (start_time_point[i],data_hdr->time) == 0) { /* is there a corresponding end time? */ if (i < end_time_count) { /* use current data start time */ /* calc number of samples */ duration = timedif(temp_hdr.time,end_time_point[i]); /* took this out of timedif */ if (duration < 0) duration = 0; temp_hdr.nsamples = (int) (((duration*temp_hdr.sample_rate)/TIME_PRECISION)+.999); /* make sure it's not more than available data */ if (temp_hdr.nsamples > data_hdr->nsamples) temp_hdr.nsamples = data_hdr->nsamples; output_data(&temp_hdr, 0); } else { output_data(data_hdr, 0); } } /* is span start less than start of data? */ else if (timecmp (start_time_point[i],data_hdr->time) < 0) { /* is there a corresponding end time? */ if (i < end_time_count) { if (timecmp (end_time_point[i],data_hdr->time) > 0) { /* use current data start time */ /* calc number of samples */ duration = timedif(temp_hdr.time,end_time_point[i]); /* took this out of timedif */ if (duration < 0) duration = 0; temp_hdr.nsamples = (int) (((duration*temp_hdr.sample_rate)/TIME_PRECISION)+.999); /* make sure it's not more than available data */ if (end_time_point[i].year == 9999) temp_hdr.nsamples = data_hdr->nsamples; if (temp_hdr.nsamples > data_hdr->nsamples) temp_hdr.nsamples = data_hdr->nsamples; output_data(&temp_hdr, 0); } } else { output_data(data_hdr, 0); } } /* span start must be greater than start of data */ else { /* figure out data ending time */ if (data_hdr->sample_rate != 0) duration = (((double)data_hdr->nsamples * TIME_PRECISION) / data_hdr->sample_rate); else duration = 0; end_time = timeadd_double (data_hdr->time, duration); /* is span start before end of data? */ if (timecmp(start_time_point[i],end_time) < 0) { /* calc new start time and sample offset*/ duration = timedif(temp_hdr.time,start_time_point[i]); /* took this out of timedif */ if (duration < 0) duration = 0; offset = (int) ((duration * data_hdr->sample_rate)/TIME_PRECISION); /* changed to float to avoid overflow - CL */ duration = ((float) offset * TIME_PRECISION) / data_hdr->sample_rate; temp_hdr.time = timeadd_double (data_hdr->time, duration); /* calc number of samples */ if (i < end_time_count) { if (end_time_point[i].year != 9999) duration = timedif(temp_hdr.time,end_time_point[i]); else duration = timedif(temp_hdr.time,end_time); } else duration = timedif(temp_hdr.time,end_time); /* took this out of timedif */ if (duration < 0) duration = 0; temp_hdr.nsamples = (int)(((duration*temp_hdr.sample_rate)/TIME_PRECISION)+.999); /* make sure it's not more than available data */ if ((temp_hdr.nsamples+offset) > data_hdr->nsamples) temp_hdr.nsamples = data_hdr->nsamples - offset; output_data(&temp_hdr, offset); } } } data_hdr->nsamples = 0; if (read_summary_flag) /* will avoid unnecessary free()s as these weren't malloced */ start_time_count = end_time_count = 0; /* +=======================================+ */ /*================| clean-up |================*/ /* +=======================================+ */ }