Solved

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

Posted on 2004-10-10
9
414 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 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