Organizing Dates/Times

Hi all,

I'm trying to think of a way to organize time stamps. Basically I'm reading in a CSV file and need to chronologically sort the data by the parameter in the first column (ie, the date). The format of the date is as follows:

dd-mm-yyyy hh:mm:ss.fff (where f is a fraction of a second)

I've got ideas as to what I can do, but they're mostly tedious and involve string parsing in order to compare integer values. If there are any thoughts about simpler methods, I would appreciate hearing them. Thanks!
You'll have to parse the string. It isn't that hard.
In this case, how would you recommend going about it? I feel like a set would be most help, but I'm a little unsure how to add the string to the set. On the one hand, I can't compare the entire string, but on the other hand, I can't use a specific attribute (ie, month, day, etc) as the key.
If the time stamp doesn't need to be any particular type, just roll your own:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <sstream>
#include <string>

struct date_time_t
  {
  unsigned year;
  unsigned month;
  unsigned day;
  double   time_in_seconds;
  };

std::istream& operator >> ( std::istream& ins, date_time_t& dt )
  {
  // Hey, this performs NO error checking!

  ins >> dt.day;    ins.get();  // skip the '-' or '/' or whatever
  ins >> dt.month;  ins.get();
  ins >> dt.year;

  unsigned hour, minute;
  ins >> hour;    ins.get();  // skip the ':'
  ins >> minute;  ins.get();
  ins >> dt.time_in_seconds;

  dt.time_in_seconds += minute * 60.0;
  dt.time_in_seconds += hour   * 60.0 * 60.0;

  return ins;
  }

bool operator < ( const date_time_t& lhs, const date_time_t& rhs )
  {
  return (lhs.year            < rhs.year)
      || (lhs.month           < rhs.month)
      || (lhs.day             < rhs.day)
      || (lhs.time_in_seconds < rhs.time_in_seconds);
  }

int main()
  {
  using namespace std;

  date_time_t d1;
  date_time_t d2;

  istringstream ss1( "12-03-1998 13:29:33.12" );
  istringstream ss2( "12-03-1998 09:00:17" );

  ss1 >> d1;
  ss2 >> d2;

  cout << ss1.str()
       << ((d1 < d2) ? " < " : " >= ")
       << ss2.str()
       << endl;

  return 0;
  }

Fill in other stuff as you need it.

Hope this helps.
Topic archived. No new replies allowed.