Diferencia de tiempo entre marcas de tiempo de matriz

I am building a new timeclock system for my company, and I am having some trouble figuring out how to add time differences between all the time stamps on a particular date...

Here is my view....

<% @punches_days.sort.each do |day, punches|%>

<h3><%= day.strftime('%A %D') %></h3>
<table>
  <tr>

    <th>Status</th>
    <th>Comment</th>
    <th>Time</th>
    <th></th>
    <th></th>
  </tr>

<% for punch in punches %>
  <tr>

    <td><%= punch.status %></td>
    <td><%= punch.comment %></td>
    <td><%= punch.created_at.in_time_zone(punch.user.time_zone)%></td>
    <td><%= link_to 'Show', punch %></td>
    <td><%= link_to 'Edit', edit_punch_path(punch) %></td>
    <td><%= link_to 'Destroy', punch, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>


<% end %>

Which Produces this...

  Sunday 11/06/11

Status  Comment Time        
In      2011-11-06 08:00:00 -0500   Show    Edit    Destroy
Lunch       2011-11-06 12:00:00 -0500   Show    Edit    Destroy
In      2011-11-06 13:00:00 -0500   Show    Edit    Destroy
Out     2011-11-06 16:00:00 -0500   Show    Edit    Destroy

 Tuesday 11/08/11

Status  Comment Time        
In      2011-11-08 08:00:00 -0500   Show    Edit    Destroy
Lunch       2011-11-08 12:15:00 -0500   Show    Edit    Destroy
In      2011-11-08 13:00:00 -0500   Show    Edit    Destroy
Out     2011-11-08 16:41:00 -0500   Show    Edit    Destroy

I What I would like is to be able to tally the time for each day, and then a total for all. I know how to get time-difference between two times, but I can not wrap my head around getting the time difference between an array of times throughout the day....

I have scavenged the internet for an example of what I am trying to accomplish, and have come up empty... Any ideas would be greatly appreciated....

Gracias...

EDIT)

Here is my controller code with part of the solution implemented, and this does give me a total, however, I can not do this on a daily basis because of how I am using an ordered hash...

def timecard
       @punches = Punch.timecard(params[:user])
       @punches_days = @punches.group_by { |t| t.created_at.beginning_of_day}
       @in_out_lengths = @punches.each_slice(2).map { |a| a[1].created_at -            a[0].created_at }
       @total          = ((@in_out_lengths.inject(:+))/1.hour).round


    respond_to do |format|
      format.html # timecard.html.erb
      format.json { render :json => @punches }
    end
  end

preguntado el 08 de noviembre de 11 a las 18:11

I'm a little confused by your data. It looks like someone punched in on Sunday at 12:15 and didn't punch out until Tuesday at 11:10. That's a pretty long shift. -

i guess it would be lol, but this is just sample data, and I have not written validations to catch such things yet... Just trying to get the nuts and bolts together right now.... -

So the real data should always look like [punch-in, punch-out, punch-in, punch-out, ...] ¿luego? -

Thats exactly right, and "lunch" will be equiv to out, in fact I may simplify to just be "in, out, in, out" I fixed my sample data to show a better example above.... -

1 Respuestas

Given an array of objects in punches and that they come in pairs with the first being a punch-in and the second being a punch-out then something like this will give you the durations:

in_out_lengths = punches.each_slice(2).map { |a| a[1].created_at - a[0].created_at }
total          = in_out_lengths.inject(:+)

La in_out_lengths array will have the time span for each punch-in/punch-out pair and total will total time for the day. You could also use first y last if you think the numeric indexing looks ugly:

punches.each_slice(2).map { |a| a.last.created_at - a.first.created_at }

The only trick is using each_slice to chop the day's in/out array into the desired pairs. Once you have that in place, the map y inject are pretty straightforward. All of this does, of course, assume that punches is properly set up and validated.

respondido 18 mar '19, 19:03

This is getting me pretty close... I am running into some issues callling each_slice method on my "day of punches" because of the fact that I am actually getting that from an ordered hash... I can easily get the entire total with this tho, so i can see some light.. I am adding my controller src up top here so you can see how my arrays are laid out.... - Chase Snelgrove

@Chase: So you got it all sorted out? Sorry, I'm just starting my day here. - mu es demasiado corto

I am a ways from having it ALL sorted out, but this has got me running... Now just need to write some validations and refactor everything... Thanks so much, Learned a ton from this little endeavor! - Chase Snelgrove

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.