%{
//===========================================================================
// @(#) $Name: arts++-1-1-a12 $
// @(#) $Id: ArtsTimeIntervalSelectionSet.lex,v 1.2 2004/04/21 23:51:35 kkeys Exp $
//===========================================================================
//  Copyright Notice
//
//  By accessing this software, arts++, you are duly informed
//  of and agree to be bound by the conditions described below in this
//  notice:
//
//  This software product, arts++, is developed by Daniel W. McRobb, and
//  copyrighted(C) 1998 by the University of California, San Diego
//  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
//  NCR-9711092, under which part of this code was developed.
//
//  There is no charge for arts++ software.  You can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License, Version 2.1, February 1999, which is incorporated by
//  reference herein.
//
//  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
//  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
//  of it will not infringe on any third party's intellectual
//  property rights.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with arts++.  Copies can also be obtained from:
//
//    http://www.gnu.org/copyleft/lesser.html
//
//  or by writing to:
//
//  Free Software Foundation, Inc.
//  59 Temple Place, Suite 330
//  Boston, MA 02111-1307
//  USA
//
//  Or contact:
//
//    info@caida.org
//===========================================================================

extern "C" {
#include <stdio.h>
#include <time.h>
  
#include "artslocal.h"
}

#include <string>

#if defined(HAVE_SSTREAM)
  #include <sstream>
#elif defined(HAVE_STRSTREAM)
  #include <strstream>
#else
  #include <strstream.h>
#endif

#include "ArtsTimeIntervalSelectionSet.hh"

using namespace std;

static struct tm    g_Tm;
static time_t       g_timeValue;
%}

%option noyywrap

%x INTEGER
%x DATELEXERROR

%%

  //  Match a date and time of the form 'MM/DD/YY[YY] hh:mm:ss', convert 
  //  to an integer of seconds since UNIX epoch.                        
[0-9]+[/][0-9]+[/][0-9]+[ ]+[0-9]+[\:][0-9][0-9][\:][0-9][0-9]  {
  memset(&g_Tm,0,sizeof(g_Tm));
  if (sscanf(yytext,
             "%d/%d/%d %d:%02d:%02d",&g_Tm.tm_mon,&g_Tm.tm_mday,
             &g_Tm.tm_year,&g_Tm.tm_hour,&g_Tm.tm_min,&g_Tm.tm_sec) != 6)
  {
    cerr << "bad date format: use MM/DD/YYYY HH:MM:SS" << endl;
    return(DATELEXERROR);
  }
  else
  {
    g_Tm.tm_mon --;
    g_Tm.tm_isdst = -1;
    if (g_Tm.tm_year >= 1900) {
      g_Tm.tm_year -= 1900;
    }
    else {
      cerr << "warning: you should use YYYY for year." << endl;
      if (g_Tm.tm_year < 96) {
        g_Tm.tm_year += 100;
      }
      else {
        if (g_Tm.tm_year > 100) {
          cerr << "bad date format: use MM/DD/YYYY HH:MM:SS" << endl;
          return(DATELEXERROR);
        }
      }
      cerr << "assuming year " << g_Tm.tm_year+1900 << endl;
    }

    g_timeValue = mktime(&g_Tm);
    return(INTEGER);
  }
}

%%

//----------------------------------------------------------------------------
//        static time_t LexDateTime(const char *dateTimeExpression) 
//............................................................................
//  
//----------------------------------------------------------------------------
static time_t LexDateTime(const char *dateTimeExpression)
{
#if defined(HAVE_SSTREAM)
  istringstream           inStream(dateTimeExpression);
#else
  istrstream              inStream(dateTimeExpression);
#endif
  TimeIntervalFlexLexer  *lexer = new TimeIntervalFlexLexer(&inStream,0);
  int                     test;
  time_t                  timeValue = (time_t)(-1);
  
  while ((test = lexer->yylex())) {
    switch (test) {
      case INTEGER:
        timeValue = g_timeValue;
        break;
      case DATELEXERROR:
        cerr << "error in date/time expression '" << dateTimeExpression
             << "'" << endl;
        break;
      default:
        break;
    }
  }

  delete(lexer);
  return(timeValue);
}

//----------------------------------------------------------------------------
// void ArtsTimeIntervalSelectionSet::Load(const char *startTimeExpression, 
//                                         const char *endTimeExpression) 
//............................................................................
//  
//----------------------------------------------------------------------------
void ArtsTimeIntervalSelectionSet::Load(const char *startTimeExpression,
                                        const char *endTimeExpression)
{
  time_t  startTime = 0;
  time_t  endTime   = 1;

  endTime <<= ((sizeof(time_t) * 8) - 1);
  endTime = ~endTime;

  if (startTimeExpression) {
    startTime = LexDateTime(startTimeExpression);
  }
  if (endTimeExpression) {
    endTime = LexDateTime(endTimeExpression);
  }

  this->AddRange(startTime,endTime);

  return;
}

  
