ifbtorr.cc File Reference

#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <sys/time.h>
#include <ctime>
#include "WebAPI.h"
#include "ifbeam.h"
#include "BeamData/python/bdp/BeamData.h"
#include "BeamData/python/bdp/BeamData.cc"
#include "OnlineUtil/rototalk.h"
#include <algorithm>
#include <map>

Go to the source code of this file.

Classes

class  ifbConfig
class  ifbProcBase
class  ifbProcRoto

Typedefs

typedef struct timespec timespec_t
typedef struct tm tm_t

Functions

double ParseTimeString (const char *)
std::string UTCTimeString (double)
std::string LocalTimeString (double)
std::vector< std::string > UniqueNames (std::vector< std::string >, int verbose)
void DoubleToSecMSec (double t, int &sec, int &msec)
int main (int argc, char **argv)
bool IsLeapYear (int year)
time_t MktimeFromUTC (tm_t *tmstruct)
void DumpTMStruct (const tm_t &tmstruct)

Variables

const char * exename = "ifbtorr"

Typedef Documentation

typedef struct timespec timespec_t

Definition at line 31 of file ifbtorr.cc.

typedef struct tm tm_t

Definition at line 32 of file ifbtorr.cc.


Function Documentation

void DoubleToSecMSec ( double  t,
int &  sec,
int &  msec 
) [inline]

Definition at line 159 of file ifbtorr.cc.

00160 {
00161   // add 0.5 before truncation on msec so that we round properly
00162   sec = (int)t;  msec = int((t - int(sec))*1.0e3+0.5);
00163 }

void DumpTMStruct ( const tm_t tmstruct  ) 

Definition at line 972 of file ifbtorr.cc.

00973 {
00974    // Print out the "tm" structure:
00975    // tmstruct.tm_year = year;    // years since 1900
00976    // tmstruct.tm_mon  = month-1; // months since Jan [0,11]
00977    // tmstruct.tm_mday = day;     // day of the month [1,31]
00978    // tmstruct.tm_hour = hour;    // hours since midnight [0,23]
00979    // tmstruct.tm_min  = min;     // minutes after the hour [0,59]
00980    // tmstruct.tm_sec  = sec;     // seconds after the minute [0,59]
00981    // tmstruct.tm_wday            // day of week [0,6]
00982    // tmstruct.tm_yday            // days in year [0,365]
00983    // tmstruct.tm_isdst           // DST [-1/0/1]  (unknown,false,true)
00984 
00985    printf(" tm { year %4d, mon   %2d, day   %2d,\n",
00986           tmstruct.tm_year,
00987           tmstruct.tm_mon,
00988           tmstruct.tm_mday);
00989    printf("      hour   %2d, min   %2d, sec   %2d,\n",
00990           tmstruct.tm_hour,
00991           tmstruct.tm_min,
00992           tmstruct.tm_sec);
00993    printf("      wday   %2d, yday %3d, isdst %2d",
00994           tmstruct.tm_wday,
00995           tmstruct.tm_yday,
00996           tmstruct.tm_isdst);
00997 #ifdef linux
00998 //#ifdef __GNUC__
00999 // special GCC extras
01000    printf(",\n      tm_gmtoff %7ld,  tm_zone \"%s\"",
01001 #ifdef __USE_BSD
01002           tmstruct.tm_gmtoff,tmstruct.tm_zone);
01003 #else
01004           tmstruct.__tm_gmtoff,tmstruct.__tm_zone);
01005 #endif
01006 #endif
01007    printf("}\n");
01008 }

bool IsLeapYear ( int  year  ) 

Definition at line 880 of file ifbtorr.cc.

00881 {
00882    // Is the given year a leap year.
00883 
00884 
00885    // The calendar year is 365 days long, unless the year is exactly divisible
00886    // by 4, in which case an extra day is added to February to make the year
00887    // 366 days long. If the year is the last year of a century, eg. 1700, 1800,
00888    // 1900, 2000, then it is only a leap year if it is exactly divisible by
00889    // 400. Therefore, 1900 wasn't a leap year but 2000 was. The reason for
00890    // these rules is to bring the average length of the calendar year into
00891    // line with the length of the Earth's orbit around the Sun, so that the
00892    // seasons always occur during the same months each year.
00893 
00894    if (year%4 != 0) {
00895       return false;
00896    }
00897    else {
00898       if (year%400 == 0) {
00899          return true;
00900       }
00901       else { 
00902          if (year%100 == 0) {
00903             return false;
00904          }
00905          else {
00906             return true;
00907          }
00908       }
00909    }
00910 
00911 }

std::string LocalTimeString ( double  ts  ) 

Definition at line 1091 of file ifbtorr.cc.

01092 {
01093   char bufferloc[80];
01094   time_t loctime = (time_t)ts;
01095   struct tm * timeinfoloc = localtime( &loctime );
01096   strftime (bufferloc,80,"%F %T (local)",timeinfoloc);
01097   return std::string( bufferloc );
01098 }

int main ( int  argc,
char **  argv 
)

Definition at line 166 of file ifbtorr.cc.

References ifbConfig::CountAndQuit, exename, exit(), ifbConfig::ParseArgv(), ifbConfig::PrintAndQuit, ifbConfig::PrintConfig(), ifbProcBase::Run(), and ifbConfig::Verbose.

00167 {
00168   exename = argv[0];
00169 
00170   //
00171   // Get configuration options
00172   //
00173   ifbConfig cfg;
00174   cfg.ParseArgv(argc,argv);
00175   if ( cfg.PrintAndQuit || cfg.Verbose > 0  ) cfg.PrintConfig();
00176   if ( cfg.PrintAndQuit ) exit(0);
00177 
00178   ifbProcBase* ifbp = 0;
00179   if ( cfg.CountAndQuit ) ifbp = new ifbProcBase(cfg);
00180   else                    ifbp = new ifbProcRoto(cfg);
00181 
00182   ifbp->Run();
00183 
00184 } // end of main() program

time_t MktimeFromUTC ( tm_t tmstruct  ) 

Definition at line 914 of file ifbtorr.cc.

References IsLeapYear(), and Munits::year.

00915 {
00916    // Equivalent of standard routine "mktime" but
00917    // using the assumption that tm struct is filled with UTC, not local, time.
00918 
00919    // This version *ISN'T* configured to handle every possible
00920    // weirdness of out-of-range values in the case of normalizing
00921    // the tm struct.
00922 
00923    // This version *DOESN'T* correctly handle values that can't be
00924    // fit into a time_t (i.e. beyond year 2038-01-18 19:14:07, or
00925    // before the start of Epoch).
00926 
00927    const int days[]     = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00928    const int daysLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00929 
00930    int year = tmstruct->tm_year + 1900;
00931    bool isleap = IsLeapYear(year);
00932 
00933    const int *daysInMonth = days;
00934    if (isleap) daysInMonth = daysLeap;
00935 
00936    // fill in tmstruct->tm_yday
00937 
00938    int &ref_tm_mon = tmstruct->tm_mon;
00939    int &ref_tm_mday = tmstruct->tm_mday;
00940    // count days in months past
00941    tmstruct->tm_yday = 0;
00942    for (int imonth = 0; imonth < ref_tm_mon; imonth++) {
00943       tmstruct->tm_yday += daysInMonth[imonth];
00944    }
00945    tmstruct->tm_yday += ref_tm_mday - 1;  // day [1-31] but yday [0-365]
00946 
00947    // adjust if day in this month is more than the month has
00948    while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
00949       ref_tm_mday -= daysInMonth[ref_tm_mon];
00950       ref_tm_mon++;
00951    }
00952 
00953    // *should* calculate tm_wday (0-6) here ...
00954 
00955    // UTC is never DST
00956    tmstruct->tm_isdst = 0;
00957 
00958    // Calculate seconds since the Epoch based on formula in
00959    // POSIX  IEEEE Std 1003.1b-1993 pg 22
00960 
00961    int utc_sec = tmstruct->tm_sec +
00962                  tmstruct->tm_min*60 +
00963                  tmstruct->tm_hour*3600 +
00964                  tmstruct->tm_yday*86400 +
00965                  (tmstruct->tm_year-70)*31536000 +
00966                  ((tmstruct->tm_year-69)/4)*86400;
00967 
00968    return utc_sec;
00969 }

double ParseTimeString ( const char *  s  ) 

Definition at line 1011 of file ifbtorr.cc.

References Munits::day, Munits::hour, min, MktimeFromUTC(), month, and Munits::year.

01012 {
01013    // assume string of the form 'YYYY-MM-DD hh:mm:ss[.nnnnn]'
01014    bool isUTC = true;
01015 
01016    int year=0, month=0, day=0;
01017    int hour=0, min=0, sec=0;
01018    float fltsec=0, frac_sec;
01019    char buff[128] = {0};
01020 
01021    int nitems = sscanf(s,"%d-%d-%d %d:%d:%f%s",
01022                        &year,&month,&day,&hour,&min,&fltsec,buff);
01023    //std::cout << "rest of time string \"" << buff << "\"" << std::endl;
01024 
01025    if ( nitems != 6 ) {
01026      std::cout << "ParseTimeString only got " << nitems << " items" 
01027                << std::endl;
01028    }
01029    // possibility of fractional seconds
01030    sec = (int)fltsec;
01031    frac_sec = ( fltsec - sec );
01032 
01033    // check trailing text
01034    // if anything but "", "Z", "UTC" or "GMT" assume time given was
01035    // in local time ... don't try to parse it as a TZ ...
01036    std::string tzstr(buff);
01037    // trim lead/trailing blanks
01038    if( tzstr.find_first_not_of(" ()[]{}\n") != 0)
01039      tzstr.erase( 0,  tzstr.find_first_not_of(" ()[]{}\n")  );
01040    if( tzstr.find_last_not_of(" ()[]{}\n") != tzstr.length() )
01041      tzstr.erase( tzstr.find_last_not_of(" ()[]{}\n")+1, tzstr.length() );
01042 
01043    isUTC = ( tzstr == "" || tzstr == "Z" || tzstr == "UTC" || tzstr == "GMT" );
01044 
01045    // month & day both use normal 1..12 and 1..31 counting
01046    // hours, min, sec run from 0 to 23, 59, 59 respectively;
01047    // secOffset provides method for adjusting for alternative timezones
01048    //
01049    // "year"  |    0    1 ... 37 | 38...69   |   70 .. 100  101 ..  137
01050    // true    | 2000 2001   2037 | undefined | 1970   2000 2001 .. 2037
01051    //
01052    // "year"  | 138...1969 | 1970 .. 2037 | ...
01053    // true    | undefined  | 1970 .. 2037 | undefined 
01054    //
01055 
01056    // deal with special formats of year
01057    if (year <= 37)                year += 2000;
01058    if (year >= 70 && year <= 137) year += 1900;
01059    // tm.tm_year is years since 1900
01060    if (year >= 1900)              year -= 1900;
01061 
01062    struct tm tmstruct;
01063    tmstruct.tm_year  = year;    // years since 1900
01064    tmstruct.tm_mon   = month-1; // months since Jan [0,11]
01065    tmstruct.tm_mday  = day;     // day of the month [1,31]
01066    tmstruct.tm_hour  = hour;    // hours since midnight [0,23]
01067    tmstruct.tm_min   = min;     // minutes after the hour [0,59]
01068    tmstruct.tm_sec   = sec;     // seconds after the minute [0,59]     
01069    tmstruct.tm_isdst = -1;      // let "mktime" determine DST setting
01070 
01071    //const time_t bad_time_t = (time_t) -1;
01072    // convert tm struct to time_t, if values are given in UTC then
01073    // no standard routine exists and we'll have to use our homegrown routine,
01074    // if values are given in local time then use "mktime"
01075    // which also normalizes the tm struct as a byproduct
01076    time_t utc_sec = (isUTC) ? MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
01077 
01078    double atime = utc_sec + frac_sec;
01079    return atime;
01080 }

std::vector<std::string> UniqueNames ( std::vector< std::string >  names,
int  verbose 
)

Definition at line 511 of file ifbtorr.cc.

References exename.

00513 {
00514   //
00515   // Weed out duplicate names (also print out duplicates)
00516   //
00517   std::map<std::string,int> namecnt;
00518   for ( size_t iname = 0; iname < names.size(); ++iname ) {
00519     namecnt[names[iname]]++;
00520   }
00521   if ( names.size() != namecnt.size() ) {
00522     if ( verbose > 3 ) {
00523       std::cout << exename << ": bfp.GetDeviceList() returned " 
00524                 << names.size() << " devices, " 
00525                 << namecnt.size() << " unique"
00526                 << std::endl;
00527     }
00528     // clear old list
00529     names.clear();
00530     // build new list from sorted map
00531     std::map<std::string,int>::const_iterator ncitr = namecnt.begin();
00532     for ( ; ncitr != namecnt.end(); ++ncitr ) {
00533       names.push_back(ncitr->first);
00534       int cnt = ncitr->second;
00535       if ( cnt > 1 && verbose > 4 ) {
00536         std::cout << "  " << std::left << std::setw(12) 
00537                   << ncitr->first << std::right
00538                   << " was in device list " << cnt 
00539                   << " times." << std::endl;
00540       }
00541     }
00542   }
00543   return names;
00544 }

std::string UTCTimeString ( double  ts  ) 

Definition at line 1082 of file ifbtorr.cc.

01083 {
01084   char buffergmt[80];
01085   time_t gmttime = (time_t)ts;
01086   struct tm * timeinfogmt = gmtime( &gmttime );
01087   strftime (buffergmt,80,"%F %T (UTC)",timeinfogmt);
01088   return std::string( buffergmt );
01089 }


Variable Documentation

const char* exename = "ifbtorr"

Definition at line 46 of file ifbtorr.cc.


Generated on 19 Jan 2018 for loon by  doxygen 1.6.1