Date substraction why don't I get hh:mm:ss in Rebol / Red? - rebol

1°) I get only the number of days, how to also get the hh:mm:ss ?
diff: (now - (10-Nov-2017/15:00:00))
6
2°) What's the most elegant way to get the number of minutes ?

1°) I get only the number of days, how to also get the hh:mm:ss ?
Use difference function for that:
>> difference now 10-Nov-2017/15:00:00
== 145:19:24
2°) What's the most elegant way to get the number of minutes ?
The same way as for any other date value:
>> d: difference now 10-Nov-2017/15:00:00
>> d/minute
== 21
Alternatively, you can use pick to avoid setting the intermediary date to a word:
>> pick (difference now 10-Nov-2017/15:00:00) 2 ; Red and Rebol2/3
== 21
>> pick (difference now 10-Nov-2017/15:00:00) 'minute ; Rebol3, not yet implemented in Red
== 21

Related

Pig - Is there a better way to format a value with 6 digits

I am developing a pig script and have to display the number as 6 digits eventhough the number is 4 digit.
ex: 6001 should be displayed as 006001.
Thank you.
Alternatively, you can cast it to string and append '00'.
y = foreach x generate CONCAT('00',(chararray)val1);

Doing math with sprintf

When creating a label for GNUplot, reading from text files, how would I get the difference in hours:minutes from two columns which each contain an H:M timestamp (e.g. 23.42).
For example, this creates a concatenation of two columns for an existing label:
myDate(col1,col3)=sprintf("%s-%s",strcol(1),strcol(3))
Is it possible modify it to do that date math to get something like:
timeDiffLabel(col5,col6)=sprintf(do-some-math-here,strcol(5),strcol(6))
To parse a time, use the strptime function:
print strptime('%H:%M', '12:34')
This prints 45240.0, which is the number of seconds parsed from the time string.
If you parse the strings from the two columns like this, you can subtract the values and reformat the result with strftime:
timeDiff(c1, c2) = strftime('%k:%M', strptime('%H:%M', strcol(c2)) - strptime('%H:%M', strcol(c1)))
plot 'test.dat' using 0:0:(timeDiff(1,2)) with labels
This works in principle, but only for positive differences. If the difference is e.g. -1, you'll get 23, because the str*time functions work on datetimes.
A more sophisticated solution uses only the absolute value of the difference for the actual formatting, and prepends an optional - to the result:
timeDiff(c1, c2) = (diff = strptime('%H:%M', strcol(c2)) - strptime('%H:%M', strcol(c1)), (diff < 0 ? '-' : '').strftime('%k:%M', abs(diff)))
plot 'test.dat' using 0:0:(timeDiff(1,2)) with labels
So, with a test file like
12:34 23:54
13:45 11:44
2:33 1:11
you get

What is the keyword to get time in milliseconds in robot framework?

Currently I am getting time with the keyword Get time epoch , which is returning time in seconds. But I need time in milliseconds , So that I can get time span for a particular event.
or is there any other way to get the time span for a particular event or a testsceanrio?
Check the new test library DateTime, which contains keyword Get Current Date, which also returns milliseconds. It also has keyword Subtract Dates to calculate difference between two timestamps.
One of the more powerful features of robot is that you can directly call python code from a test script using the Evaluate keyword. For example, you can call the time.time() function, and do a little math:
*** Test cases
| Example getting the time in milliseconds
| | ${ms}= | Evaluate | int(round(time.time() * 1000)) | time
| | log | time in ms: ${ms}
Note that even though time.time returns a floating point value, not all systems will return a value more precise than one second.
Using the DateTime library, as suggested by janne:
*** Settings ***
Library DateTime
*** Test Cases ***
Performance Test
${timeAvgMs} = Test wall clock time 100 MyKeywordToPerformanceTest and optional arguments
Should be true ${timeAvgMs} < 50
*** Keywords ***
MyKeywordToPerformanceTest
# Do something here
Test wall clock time
[Arguments] ${iterations} #{commandAndArgs}
${timeBefore} = Get Current Date
:FOR ${it} IN RANGE ${iterations}
\ #{commandAndArgs}
${timeAfter} = Get Current Date
${timeTotalMs} = Subtract Date From Date ${timeAfter} ${timeBefore} result_format=number
${timeAvgMs} = Evaluate int(${timeTotalMs} / ${iterations} * 1000)
Return from keyword ${timeAvgMs}
In the report, for each suite, test and keyword, you have the information about start, end and length with millisecond details. Something like:
Start / End / Elapsed: 20140602 10:57:15.948 / 20140602 10:57:16.985 / 00:00:01.037
I don't see a way to do it using Builtin, look:
def get_time(format='timestamp', time_=None):
"""Return the given or current time in requested format.
If time is not given, current time is used. How time is returned is
is deternined based on the given 'format' string as follows. Note that all
checks are case insensitive.
- If 'format' contains word 'epoch' the time is returned in seconds after
the unix epoch.
- If 'format' contains any of the words 'year', 'month', 'day', 'hour',
'min' or 'sec' only selected parts are returned. The order of the returned
parts is always the one in previous sentence and order of words in
'format' is not significant. Parts are returned as zero padded strings
(e.g. May -> '05').
- Otherwise (and by default) the time is returned as a timestamp string in
format '2006-02-24 15:08:31'
"""
time_ = int(time_ or time.time())
format = format.lower()
# 1) Return time in seconds since epoc
if 'epoch' in format:
return time_
timetuple = time.localtime(time_)
parts = []
for i, match in enumerate('year month day hour min sec'.split()):
if match in format:
parts.append('%.2d' % timetuple[i])
# 2) Return time as timestamp
if not parts:
return format_time(timetuple, daysep='-')
# Return requested parts of the time
elif len(parts) == 1:
return parts[0]
else:
return parts
You have to write your own module, you need something like:
import time
def get_time_in_millies():
time_millies = lambda: int(round(time.time() * 1000))
return time_millies
Then import this library in Ride for the suite and you can use the method name like keyword, in my case it would be Get Time In Millies. More info here.

How to perform math calculations in Active Record Querying in Rails

I'm building an application and I'm finding it necessary to perform some simple math calculations in my query. Essentially, I've got a database with daily values from the S&P 500, and I need to get a listing of days depending on the criteria entered.
The user inputs both a day range, and a % range. For instance, if the date range is Jan 1/2013 - Apr 1/2013 and the % range is -1% - 1%, it should return a list of all S%P 500 daily values between the dates where difference between the opening and closing values are in the % range.
The problem is that I don't actually have a column for %; I only have a column for opening/closing values. It is simple enough to calculate the % given only the opening and closing values (close-open)/open*100. But I'm not sure how to do this within the query.
Right now the query is successfully searching within the date range. My query is:
#cases = Close.find(:all, conditions:["date between ? and ?",
#f_start, #f_end])
But how can I get it to check if the current row's (close-open)/open*100 value is between the two % range values?
Alternatively, if this is not possible or in bad practice, where should I be handling this?
You can calculate the open/close range yourself in Ruby/Rails and pass it on the same way you do with the date range. Something like:
percent = #f_percent / 100.0 # 5% => 0.05
low = #f_close * (1.0 - percent)
high = #f_close * (1.0 + percent)
Close.where 'date between ? and ? AND close between ? and ?', \
#f_start, #f_end, low, high

Formula in gawk

I have a problem that I’m trying to work out in gawk. This should be so simple, but my attempts ended up with a divide by zero error.
What I trying to accomplish is as follows –
maxlines = 22 (fixed value)
maxnumber = > max lines (unknown value)
Example:
maxlines=22
maxnumber=60
My output should look like the following:
print lines:
1
2
...
22
print lines:
23
24
...
45
print lines:
46 (remainder of 60 (maxnumber))
47
...
60
It's not clear what you're asking, but I assume you want to loop through input lines and print a new header (page header?) after every 22 lines. Using a simple counter and check for
count % 22 == 1
which tells you it's time to print the next page.
Or you could keep two counters, one for the absolute line number and another for the line number within the current page. When the second counter exceeds 22, reset it to zero and print the next page heading.
Worked out gawk precedence with some help and this works -
maxlines = 22
maxnumber = 60
for (i = 1; i <= maxnumber; i++){
if ( ! ( (i-1) % maxlines) ){
print "\nprint lines:"
}
print i
}