Why is if condition now working with now() - formula

I'm trying to work with an if condition with now() but it doesn't work no matter the now() is more or less than the fix time(). what can be the reason?
=IF(NOW()>TIME(9,25,0),"No Records","ABSENT")

Try this:
=IF(MOD(NOW(), 1) > TIME(9, 25, 0), "No Records", "ABSENT")
Date time is a numeric value internally. Time part is decimal part, so MOD(..., 1) gives you the time part as it gives the remainder for the division by 1.

Related

Inconsistency with as.difftime

I can convert strings containing hour, minute or second specifications to a difftime:
> as.difftime("12 h", "%H")
Time difference of 12 hours
> as.difftime("12 m", "%M")
Time difference of 12 mins
> as.difftime("12 s", "%S")
Time difference of 12 secs
But I can't do so with a week specification, because there is no appropriate format …, although "weeks" is a legitimate unit of difftime:
> as.difftime("12 w", "%…")
Am I overlooking something?
Roland noted:
It doesn't work beyond hours. … obvious, if you study the code. The relevant part is
difftime(strptime(tim, format = format), strptime("0:0:0", format = "%X"), units = units)
. If you specify only a time for strptime it adds the current date.
Indeed it is easy to study the code in the R Console:
> as.difftime
function (tim, format = "%X", units = "auto")
{
if (inherits(tim, "difftime"))
return(tim)
if (is.character(tim)) {
difftime(strptime(tim, format = format), strptime("0:0:0",
format = "%X"), units = units)
}
else {
if (!is.numeric(tim))
stop("'tim' is not character or numeric")
if (units == "auto")
stop("need explicit units for numeric conversion")
if (!(units %in% c("secs", "mins", "hours", "days", "weeks")))
stop("invalid units specified")
.difftime(tim, units = units)
}
}
The crux of the matter is that the use of strptime causes a time interval character string given to as.difftime to be treated as a point in time, from which 0 h of the
current day is subtracted. This for various reasons makes as.difftime with a string unusable for days and weeks, for example a value of 0 days is not accepted by strptime, although it were perfectly valid as an interval.
A request for comments on the R-devel mailing list didn't elicit wide response. (I received some valuable thoughts from Emil Bode by private mail, thus I won't reproduce them here.) So, I'll refrain from proposing a change to as.difftime, also because changing it would introduce discrepances between R versions.

Forex coding in R, while loop issues inside a for loop

I have some code
dates <- as.POSIXct(subset(Ident, coredata.Ident. == "Bull.Engulfing")$Date, format="%Y.%m.%d %H:%M")
for(i in seq_along(dates)){
EnterPrice<-as.vector(Sept17$Open[(dates)])
StopLoss<-as.vector(EnterPrice-0.0050)
TakeProfit<-as.vector(EnterPrice+0.0100)
MinDate<-as.vector(Sept17$Low[(dates)])
MaxDate<-as.vector(Sept17$High[(dates)])
n<-0
m<-0
while (MinDate > StopLoss) {
n<-n+1
MinDate<-Sept17$Low[Sept17[dates,which.i=TRUE]+n]
}
while (MaxDate < TakeProfit) {
m<-m+1
MaxDate<-Sept17$High[Sept17[dates,which.i=TRUE]+m]
}
if (m<n) {
F$Outcome=print("Win")
F$Hours=m
} else {
F$Outcome=print("Lose")
F$Hours=n
}
}
This returns an error of
Error in `[.xts`(Sept17$High, Sept17[dates, which.i = TRUE] + m) :
subscript out of bounds
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Basically what it does is it takes some data called Ident and computes dates and times for when the Ident data says Bull.Engulfing. It then computes EnterPrice, StopLoss, TakeProfit, MinDate and MaxDate on each of the dates computed.
For each date/hour I want it to go through the while loop and work out how many hours (n) it takes for one of the conditions to not be satisfied in the while loop. I would like it to display a vector of either Loss if the StopLoss condition is not satisfied or Profit if the TakeProfit condition is not satisfied and I would like to display the number of hours (n) it takes to achieve the unsatisfied condition.
I have tried above but not sure where to go from there.
So for example, let's say a component of dates is 2017-01-01 10:00.
The EnterPrice might be 1.3100 therefore the StopLoss would be 1.3050 and TakeProfit would be 1.3200.
It would then check every hour after that using the Sept17 data and if the min on that day and hour went less than the StopLoss it would say Lose in a vector, if the max on a day and hour went above the TakeProfit it would say Win, and it would tell me the number of hours (n) it would take to get there.
It would then loop through for every other date where a Bull.Engulfing occurred.
TIA

Julia: conversion between different time periods

Full disclosure: I've only been using Julia for about a day, so it may be too soon to ask questions.
I'm not really understanding the utility of the Dates module's Period types. Let's say I had two times and I wanted to find the number of minutes between them. It seems like the natural thing to do would be to subtract the times and then convert the result to minutes. I can deal with not having a Minute constructor (which seems most natural to my Python-addled brain), but it seems like convert should be able to do something.
The "solution" of converting from Millisecond to Int to Minute seems a little gross. What's the better/right/idiomatic way of doing this? (I did RTFM, but maybe the answer is there and I missed it.)
y, m, d = (2015, 03, 16)
hr1, min1, sec1 = (8, 14, 00)
hr2, min2, sec2 = (9, 23, 00)
t1 = DateTime(y, m, d, hr1, min1, sec1)
t2 = DateTime(y, m, d, hr2, min2, sec2)
# println(t2 - t1) # 4140000 milliseconds
# Minute(t2 - t1) # ERROR: ArgumentError("Can't convert Millisecond to Minute")
# minute(t2 - t1) # ERROR: `minute` has no method matching
# minute(::Millisecond)
# convert(Minute, (t2-t1)) # ERROR: `convert` has no method matching
# convert(::Type{Minute}, ::Millisecond)
delta_t_ms = convert(Int, t2 - t1)
function ms_to_min(time_ms)
MS_PER_S = 1000
S_PER_MIN = 60
# recall that division is floating point unless you use div function
return div(time_ms, (MS_PER_S * S_PER_MIN))
end
delta_t_min = ms_to_min(delta_t_ms)
println(Minute(delta_t_min)) # 69 minutes
(My apologies for choosing a snicker-inducing time interval. I happened to convert two friends' birthdays into hours and minutes without really thinking about it.)
Good question; seems like we should add it! (Disclosure: I made the Dates module).
For real, we had conversions in there at one point, but then for some reason or another they were taken out (I think it revolved around whether inexact conversions should throw errors or not, which has recently been cleaned up quite a bit in Base for Ints/Floats). I think it definitely makes sense to add them back in. We actually have a handful in there for other operations, so obviously they're useful.
As always, it's also a matter of who has the time to code/test/submit and hopefully that's driven by people with real needs for the functionFeel free to submit a PR if you're feeling ambitious!

tSQLt Assert Failure Message Numeric Precision

How can I increase the precision of FLOAT failed assertion messages in tSQLt?
For example
DECLARE #Expected FLOAT = -5.4371511392520810
PRINT STR(#Expected, 40, 20)
DECLARE #Actual FLOAT = #Expected - 0.0000000001
PRINT STR(#Actual, 40, 20)
EXEC tSQLt.AssertEquals #Expected, #Actual
gives
-5.4371511392520810
-5.4371511393520811
[UnitTest].[test A] failed: Expected: <-5.43715> but was: <-5.43715>
In most computer languages (including T-SQL) floating point values are approximate, so comparing FLOAT variables for being equal is often a bad idea (especially after doing some maths on them) E.g. a FLOAT variable is only accurate to about 15 digits (by default)
You can see this by adding the following line at the end of your sample code:
PRINT STR((#Actual - #Expected) * 1000000, 40, 20)
which returns -0.0001000000082740
So you could either
Use the built in SQL function ROUND to allow numbers approximately the same to be viewed as equal:
EXEC tSQLt.AssertEquals ROUND (#Expected, 14), ROUND (#Actual, 14)
Use an exact type for the variables, like NUMERIC (38, 19). Replacing every FLOAT in your example with NUMERIC (38, 19) seems to give the same result, but when you add the PRINT STR((#Actual - #Expected) * 1000000, 40, 20) mentioned above, it now prints exactly
-0.0001000000000000, showing that there is an inaccuracy in the PRINT statement as well
Of course your tSQLt.AssertEquals test will still fail since the values are different in the 10th digit after the decimal point. (one number is ...925... and the other is ...935...). If you want it to pass even then, round the values off to 9 digits with ROUND
Further information:
See David Goldberg's excellent article What Every Computer Scientist Should Know About Floating-Point Arithmetic here or here under the heading Rounding Errors.
http://msdn.microsoft.com/en-us/library/ms173773.aspx
http://www.informit.com/library/content.aspx?b=STY_Sql_Server_7&seqNum=93

Calculating wage by hours worked

Hey all, i am trying to figure out how to calculate the wage for an employee when they clock out. This is the code i am currently using:
Dim theStartTime As Date
Dim theEndTime As Date
Dim totalTime As String
theStartTime = "16:11:06"
theEndTime = "18:22:01"
totalTime = Format(CDbl((theEndTime - theStartTime) * 24), "#0.0")
So workable hours would be: 2h 11m
Right now, with my calculation code above, i get 2.2. What would i need to add in order to get it to calculate the correct time of 2:11 instead of 2:20?
David
Note that 2.2 hours is not 2:20, it's 2:12.
Change
Format(CDbl((theEndTime - theStartTime) * 24), "#0.0")
to
Format(theEndTime - theStartTime, "h:mm")
You're getting the right value, just rounding it off when you print. theEndTime - theStartTime is a time span equal to the difference between the two times. As you discovered, multiplying it by 24 will give you the number of hours different. However, you then have to divide by 24 again to use date/time formatting.
Check out all the ways to format dates and time in VB6.
First, I highly suggest going to the .NET framework (with it's easy-to-use TimeSpan class) if possible.
But, dealing in VB6 you should be able to use the DATEDIFF function (and it's been many years since I've touched VB6 so the specific syntax might be a bit off
Dim iHours As Integer, iMins As Integer
iMins = DateDiff("n", theStartTime, theEndTime)
iHours = iMins / 60
iMins = iMins Mod 60
You should also try casting it to the Currency type which can represent all numeric values (within 4 digits to the left of decimal point and 15 digits to the right).
Format(CCur((theEndTime - theStartTime) * 24), "#0.00")

Resources