digitalmars.D.learn - Effective String to Date conversion?
- atzensepp (55/55) Jan 22 Dear D-gurus,
- Mengu (8/17) Jan 28 If your date conforms to an ISO or extended ISO format, you can
- Steven Schveighoffer (6/14) Jan 31 That's how I would do it also.
Dear D-gurus, being new to D I am trying my first steps and the language is quite intuitive and appealing. When reading a file and creating a hash for the reocrds I want to get only the most recent ones. For this I need to convert Date/Time-Strings to comparable DateTime-Objects. The code below works but looks a bit clumsy. Is there a more efficient (shorter) way to accomplish this? Thanks in advance ```d // Read lines using foreach. void main( string args[]) { auto file = File(args[1]);// Open for reading auto range = file.byLineCopy(); auto iline=0; int idxARE=2,idxDC=3,idxTD=0; string [] records[string]; DateTime getDateTime( string [] row) { int d,m,y,ho,mi,se; row[0].formattedRead!"%d.%d.%d"(d, m, y); row[1].formattedRead!"%d:%d:%d"(ho, mi, se); return DateTime(y,m,d,ho,mi,se); } foreach (line; range) { if (!line.empty) { string [] row = line.split(";"); string key = [row[2], row[3]].join("_"); // unique key if(iline>0) // skip header line { if(key in records) { // do we have a newer one? if( getDateTime(row) > getDateTime(records[key])) { records[key]=row; writeln("UPDATE:",key); } } else { // first one: records[key]=row; } } iline++; } } writeln( records.length); writeln("Lines: ",i); } ```
Jan 22
On Monday, 22 January 2024 at 10:56:04 UTC, atzensepp wrote:Dear D-gurus, being new to D I am trying my first steps and the language is quite intuitive and appealing. When reading a file and creating a hash for the reocrds I want to get only the most recent ones. For this I need to convert Date/Time-Strings to comparable DateTime-Objects. The code below works but looks a bit clumsy. Is there a more efficient (shorter) way to accomplish this? [...]If your date conforms to an ISO or extended ISO format, you can use DateTime.fromISOString [0] or DateTime.fromISOExtString [1] functions. [0] https://dlang.org/phobos/std_datetime_date.html#.Date.fromISOString [1] https://dlang.org/phobos/std_datetime_date.html#.Date.fromISOExtString
Jan 28
On Monday, 22 January 2024 at 10:56:04 UTC, atzensepp wrote:Dear D-gurus, being new to D I am trying my first steps and the language is quite intuitive and appealing. When reading a file and creating a hash for the reocrds I want to get only the most recent ones. For this I need to convert Date/Time-Strings to comparable DateTime-Objects. The code below works but looks a bit clumsy. Is there a more efficient (shorter) way to accomplish this?That's how I would do it also. I would note there also is a library I've used which works pretty well: https://code.dlang.org/packages/dateparser -Steve
Jan 31