Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-10
9
Medium Priority
?
451 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

730 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