http://www.perlmonks.org?node_id=1063907

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Looking for help on how to create a CSV file with the following data,

Name,AppointmentID,StartTime,EndTime APP1,A1,2013-10-23T00:00:00.000-0700,2013-10-23T00:15:00.000-0700 APP2,A1,2013-10-23T00:30:00.000-0700,2013-10-23T00:45:00.000-0700 . . . APP50,A1,2013-10-29T17:45:00.000-0700,2013-10-29T18:00:00.000-0700 APP51,A2,2013-10-30T00:00:00.000-0700,2013-10-30T00:15:00.000-0700 APP52,A2,2013-10-30T00:30:00.000-0700,2013-10-30T00:45:00.000-0700
Need to ensure 50 appointments per AppointmentID (A1,A2..) per week.
Have a dummy CSV file (say APP.csv) with all the data for AppointmentID i.e. 100 values (A1,A2,..A100)

Replies are listed 'Best First'.
Re: Newbie: Request help on creating CSV using timestamp data
by hippo (Bishop) on Nov 22, 2013 at 09:58 UTC

    It's good to see that your organisation keeps appointments to the precision of milliseconds!

    You are going to need loops, file operations, maybe Text::CSV and most importantly an algorithm. How are the times assigned? Are they in APP.csv too? What other data is in APP.csv? What governs appointment length? You'll need to know the answers to these sorts of questions to produce a full solution.

      I have got the start and end time using the following code,
      sub starttimestamp() { my $st = localtime; return sprintf( "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $st->year + 1900, $st->mon + 1, $st->mday, $st->hour, $st->min, $st->sec ); } sub endtimestamp() { my $et = localtime; return sprintf( "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $et->year + 1900, $et->mon + 1, $et->mday, $et->hour, $et->min + 15, $et->sec ); }
      Now, i need a logic where i can keep incrementing the start and end timestamps by 15 or 30 or 60 mins. Thus, having unique timestamps. But the criteria is that i need to have only 50 unique values for a week. Later, continue the same 50 timestamps for the next week and so on..

        I'd start with making that easier to extend (and maintain). The snippet uses the default (builtin) localtime function, not the one returning a hashref or an object:

        sub timestamp { my @st = localtime (shift); return sprintf "%04d-%02d-%02dT%02d:%02d:%02d.000-0700", $st[5] + 1900, ++$st[4], @st[3,2,1,0]; } sub starttimestamp { return timestamp (time); } sub endtimestamp { return timestamp (time + (15 * 60)); } sub futuretimestamp { return timestamp (time + shift); } my $fts = futuretimestamp (162 * 60 * 60); # Stamp 162 hours ahead

        This way, there is only one location that defines the required format and several easy ways to use that.


        Enjoy, Have FUN! H.Merijn

        Which version of Perl are you running? localtime returns a string when called in scalar context in the versions I use (<= 5.14). Perhaps you are using Time::Piece but have neglected to mention this?

        Regardless, if you are going to do date/time arithmetic I would heartily recommend using any one of the available modules to do that for you rather than re-invent the wheel (otherwise you will have to start worrying about DST and leap years, etc.). Try one of Date::Manip, DateTime, Time::Piece, etc.