Solved

how to compare the time stamps, get the latest time stamp and save  it ???

Posted on 2004-10-10
9
402 Views
Last Modified: 2012-05-05
Hi experts,

I have a column containing time stamps, I know column number, time stamps like the followings:

10/1/2004 17:09
10/2/2004 12:03
10/6/2004 16:07
10/10/2004 4:06
..
..
..
..
more time stamps are added each day.
My questions are:
1. how can I get the latest time and save(write) it to somewhere (a file) for next time comparing ?
2. how can I use this saved time stamp to compare with new time stamps and save this new latest time stamp for next time comparing ?

Thanks
0
Comment
Question by:justinY
  • 5
  • 3
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12273829
You can use difftime function to determine the latest time.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12274297
Define a class like that:

class Date
{
    int date;  // hold date as yyyymmdd
public:
    Date(const string& date);
    bool operator < (const Date& dt) { return date < dt.date; }
};

Convert your string date using the constructor. Compare dates by using '<', e. g.

   Date d1("10/06/2004");
   Date d2("04/04/2004");

   Date minDate = d1;
   if (d2 < d1)
        minDate = d2;

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12274307
And for timestamps:

class DateTime
{
    int date;  // hold date as yyyymmdd
    int time;  // hold time as hhmmss
public:
    DateTime(const string& datetime);
    bool operator < (const DateTime& dt)
      { return (date < dt.date) || (date == dt.date && time < dt.time); }
};

Convert your string datetime using the constructor. Compare timestamps by using '<', e. g.

   DateTime dt1("10/06/2004 10:03:20");
   DateTime dt2("04/04/2004 12:04:33");

   DateTime lastStamp = dt1;
   if (dt1 < dt2)
        lastStamp = dt2;


Regards, Alex

0
 

Author Comment

by:justinY
ID: 12282970
Thanks Alex,
then my question is how can I get the latest timestamp from 2000 records ?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12285676
Have a loop reading all records to a string and/or struct (depending on text or binary format of your input data), extract the date from the record, build a DateTime object and compare it to the last DateTime object til now. If the current DateTime is greater store it as new maximum.

       ifstream ifs("myfile.txt");
       string line;
       DateTime lastDt("01/01/1900 00:00:00");  
       while (ifs)
       {
             getline(ifs, line);
             if (ifs.fail()) break;
             // assume date/time is first two columns separated by ' '
             size_t pos = line.find(' ');
             if ((pos == string::npos)
                  return 1; // error
             pos = line.find(' ', pos+1);
             if ((pos == string::npos)
                  return 1; // error
             DateTime curDt(line.substr(0, pos));
             if (lastDt < curDt)
                 lastDt = curDt;
       }

Tell me if you need help with DateTime::DateTime(const string& dt).

Regards, Alex
     
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12285689
Correction:   if ((pos == string::npos) is wrong, remove the first opening bracket.


                 
0
 

Author Comment

by:justinY
ID: 12290693
Thanks Alex, but two questions come up:

1. I need to convert the date and time format to yyyymmddhhmm, how do i do that ?
2. I saved last latest timestamp to a file, then when I use to to compare this time, how do I call this file?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 12296736
To 1)

Here is a constructor that takes a string containing both a date and time
in a format  similar to MM/DD/YYYY hh:mm:ss.

It takes any delimiter (but only one character) and single digits for day/month/hour. You may omit seconds, minutes, ....

class DateTime
{
    int date;  // hold date as yyyymmdd
    int time;  // hold time as hhmmss
public:
    DateTime(const string& datetime);
    bool operator < (const DateTime& dt)
      { return (date < dt.date) || (date == dt.date && time < dt.time); }
    friend ostream& operator<<(ostream& os, const DateTime& dt)
    {
        int y = dt.date/10000;
        int m = (dt.date%10000)/100;
        int d = dt.date%100;
        int h = dt.time/10000;
        int mi= (dt.time%10000)/100;
        int s = dt.time%100;
        os << m << '/' << d << '/' << y << ' ' << h;
        if (mi < 10)
            os << ":0" << mi;
        else
            os << ':' << mi;
        if (s > 0)
        {
            if (s < 10)
               os << ":0" << s;
            else
               os << ':' << s;
        }
        return os;
    }
};

DateTime::DateTime(const string& strdt)
{
    // expect something like "7/31/2000 21:05"
    int d   = 0;
    int m  = 0;
    int y   = 0;
    int h   = 0;
    int mi = 0;
    int s    = 0;
    enum { month, day, year, hour, minute, second, maxunits };
    int* dtu[maxunits] = { &m, &d, &y, &h, &mi, &s };
    int  idx = month;
    char c;
   
    for (int i = 0; i < strdt.length(); ++i)
    {
        c = strdt[i];
        if (isdigit(c))
            *dtu[idx] = (*dtu[idx]) * 10 + (c - '0');
        else
            idx++;
    }
    date = y*10000 + m*100 +  d;
    time = h*10000 + mi*100+ s;
}



To 2)
       string strdt;
       ifstream ifd("lastdate.txt");
       if (!ifd)
           return;  // error
       getline(ifd, strdt);      
       if (!ifd)
           return;  // error
       ifd.close();

       ifstream ifs("myfile.txt");
       string line;
       DateTime lastDt(strdt);  
       while (ifs)
       {
             getline(ifs, line);
             if (ifs.fail()) break;
             // assume date/time is first two columns separated by ' '
             size_t pos = line.find(' ');
             if ((pos == string::npos)
                  return 1; // error
             pos = line.find(' ', pos+1);
             if ((pos == string::npos)
                  return 1; // error
             DateTime curDt(line.substr(0, pos));
             if (lastDt < curDt)
                 lastDt = curDt;
       }

       ofstream ofs("lastdate.txt");
       ofs << lastDt << endl;
       ofs.close();

Regards, Alex

0
 

Author Comment

by:justinY
ID: 12302942
Thank you very much Alex.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now