Solved

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

Posted on 2004-10-10
9
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:justinY
ID: 12282970
Thanks Alex,
then my question is how can I get the latest timestamp from 2000 records ?
0
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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