Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have been tasked with building a simple Ruby app that does two things:
Registers new users with a New command
Shows their time difference with Work command
It would be for an office building and the purpose would be to keep track of total hours worked.
I am also supposed to pull this information from a .txt file like so:
New Aaron
New Bertha
New Charles
Work Bertha 06:30 11:12
Work Bertha 12:03 16:17
Work Charles 07:52 17:02
With the time expressed in only minutes and hours. I don't think anyone would be working past midnight. This is the part that really confuses me, as I am having a lot of trouble trying to find the difference in their times for work, as shown in an expected output .txt file:
Aaron: 0 hours 0 minutes
Bertha: 8 hours 52 minutes
Charles: 9 hours 10 minutes
Once again, I am ONLY asking for help with finding the difference in each individual's time for work.
Any and all help would be appreciated.
I'd suggest looking into IO#readlines for reading the content of your text file.
Once you're able to work with each line, you can use String#split to obtain the information that you're looking to grab like so:
"Work Bertha 06:30 11:12".split(' ')
#=> ["Work", "Bertha", "06:30", "11:12"]
Then you need the elapsed time between 2 time points. Ruby has time objects that can parse time from a string, but will set the date as the current date, which shouldn't matter for what you're working on.
require 'time'
employee = ["Work", "Bertha", "06:30", "11:12"]
clock_in = Time.parse(employee[2])
#=> 2016-01-12 06:30:00 -0600
clock_out = Time.parse(employee[3])
#=> 2016-01-12 11:12:00 -0600
Time objects can be subtracted to find the change in time between the two. It'll return the time in seconds. You can add up all the seconds from times someone clocked in/out
clock_out - clock_in
#=> 16920.0
There are many ways to make this more of a human readable time format. I suggest starting here
Related
I'm trying to run a batch script using Robocopy but when I try to put in MinAge days of 10 years ago (for example 3653 days) it doesn't like it and I'm getting an 'Invalid switch' error. If I put in the date 20110101 it works perfectly. Just trying to figure out the maximum number of days I can use for the MinAge parameter.
I've looked on Microsoft support, here an elsewhere and I don't see this being addressed. Thank you in advance for your time and answers.
1899 is the maximum days you can put in for MinAge.
This question already has answers here:
How to convert YYYYMMDDHHMMSS to a date readable by `date`
(6 answers)
Closed 6 years ago.
I have a number, lets say it is 20160823130001 which stands for 2016 august 23rd 1300 hrs 1 second. But sometimes the date stamp will be 20160823125959.
From that number I need to add or subtract 10 mins and I cannot figure out how to do it in bash. This would be easy if it was 1330 hrs but since it is 1300 it needs to end up as 1240 and 1310. I could do some strange checks to subtract or add such as if 125959 then add 51 elseif 130000 add 10 but this seems clunky. How can I make this happen without strange checks using bash?
Thank you for any pointers.
Convert the string into a date first and then do the manipulation. Links on how to do that are in the comments section
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a small script which is creating a backup every 2 hours. Now I would like to delete the old ones. I know "find" can do this, but I want it more advanced.
I want to keep
all backups form the last 24 hours
4 backups from the last 5 days
1 backup from the last 14 days
everything older than 14 days can be deleted
Could you tell me how to do this via. a shell bash script in debian ?
I couldn´t find anything for this via. google.
Thank You.
Do not reinvent the wheel. Take a look at rsnapshot. Unless you want to use this as a learning exercise, I see no reason why you would want to spend the time that has already been spent to solve this problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a file (for example system.log). I need to scan this file to find a specific string that must appear several times during a period of 5 minutes.
I imagine that I can make a script with 4 parameters:
the location of the file
the string to search
the number of times that the string appears
the period of time
If this script finds the string in the specified period of time it writes, for example, the message 'success'
Here's the begin of the script
#!/bin/ksh
#set -x
#############
# VARIABLES #
#############
location="/moteurs/websphere/7.0/esb/managed01/logs/"
file="SystemOut.log"
pattern="WSV0605W: Thread \"SIBFAPInboundThreadPool"
string=$(grep -ic "${pattern}" ${location}/${file})
Now that I've defined my variables, I don't know how can I make a function that scans SystemOut.log every 5 minutes.
Do you kown how can I create this shell?
Yes. Use your favorite editor, write the shell script, execute it. You are done.
Just a half answer, but maybe it gets you somewhere:
To run something repeatedly every five minutes, you can make a loop with a sleep:
while true
do
echo "I'm doing something!" # replace this by the actual code
sleep $[5*60]
done
This will run the code (here just an echo) every five minutes. — Okay, plus the time the code needs, so do not rely on the time being accurate; to get it accurate, you can use
while sleep $[ 5*60 - $(date +%s) % (5*60) ]
do
echo "I'm doing something!" # replace this by the actual code
done
This will always wait for a full 5-minute point on the clock before executing the code.
Question is:
Write pseudocode for a program that
calculates the service charge of a
customer owes for writing a bad check.
The program accepts a customer's name,
the date the check was written (year,
month and day), the current date
(year, month and day), and the amount
of the check in dollars and cents. The
program continues until an eof value
is encountered.
The service charge is $20 plus 2
percent of the amount of the check,
plus $5 for every month that has
passed since the check was written. A
check is one month late as soon as a
new month starts-so a bad check
written on September 30 is one month
overdue on October 1.
A program is generally a series of steps. Can you break down the problem into a series of steps necessary to calculate your answer?
Hints:
Every time the month changes, you owe another $5. Thus, "day" is irrelevant.
Next year at the same month, 12 months are passed. The previous month, the number of elapsed months is 12 - 1.
"2% more than" is equivalent to * 1.02
"Continues until EOF is reached" sounds like a loop.
Try to edit your question and make an honest attempt - no-one will solve your homework for you, but we will help you solve it.
In my humble experience, this kind of confusion is caused by trying to solve the problem and write the code at the same time.
Try solving the problem first.
Get a sheet of paper and draw a flowchart which shows the steps and decisions.
e.g. the last box might be:
EOF: Y = Stop, N = go back to "Read next line"
Pick 3 test examples e.g.
In the current month
In the last year
Greater than a year
Work these examples through your flowchart and check that the result is correct. If not, amend the flowchart and rework the test examples.
When you are happy, "translate" the flowchart into English and you will have working pseudo code.
Load the file
Read and store check_date_month in a variable
Read and store current_date_month in a variable
Read and store check_amount in a variable
Service_charge = 20 + 0.02*(check_amount) + [(current_month - check_date_month) + current_date_year - check_date_year]*5
Read customer's name and show to the user something like:
"Customer's Name"
Service charge: "$"Service_charge
The days in this case are not relevant because the charge increases every time the month changes, so, in the case we are in October and the check was done in September (10-9 = 1) we have to pay $5 more, but maybe we could be in different years, for example 2010 and 2009, that means that between October and September there are now (1 + 12 = 13) months, so now you have to pay $65. I expect this will help you to understand step 5.