Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

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

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
justinY
Asked:
justinY
  • 5
  • 3
1 Solution
 
AxterCommented:
You can use difftime function to determine the latest time.
0
 
itsmeandnobodyelseCommented:
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
 
itsmeandnobodyelseCommented:
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.

 
justinYAuthor Commented:
Thanks Alex,
then my question is how can I get the latest timestamp from 2000 records ?
0
 
itsmeandnobodyelseCommented:
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
 
itsmeandnobodyelseCommented:
Correction:   if ((pos == string::npos) is wrong, remove the first opening bracket.


                 
0
 
justinYAuthor Commented:
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
 
itsmeandnobodyelseCommented:
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
 
justinYAuthor Commented:
Thank you very much Alex.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now