I've been trying to find the issue in this for a long time. Two lines under where I get the Month variable, string.find(TimeR, "T") returns 11, but when I used TimeR:sub(Ser + 1) that returns 24T14:19:59+00:00. It's acting as if TimeR never changed, because int he original string "T" was 11 character over.
local TimeServer = "http://www.timeapi.org/utc/now"
local TimeR = http.get(TimeServer).readAll() -- Pretend that this gives the string "2013-12-24T14:19:59+00:00"
local function TimeThread()
local Last = 1
local Ser = string.find(TimeR, "-")
local Year = TimeR:sub(Last, Ser - 1)
TimeR = TimeR:sub(Ser + 1)
Ser = string.find(TimeR, "-")
local Month = TimeR:sub(Last, Ser - 1)
TimeR = TimeR:sub(Ser + 1)
Ser = string.find(TimeR, "T")
local Day = TimeR:sub(Last, Ser - 1)
TimeR = TimeR:sub(Ser + 1)
Ser = string.find(TimeR, ":")
local Hour = TimeR:sub(Last, Ser - 1)
TimeR = TimeR:sub(Ser + 1)
Ser = string.find(TimeR, ":")
local Minute = TimeR:sub(Last, Ser - 1)
TimeR = TimeR:sub(Ser + 1)
Ser = string.find(TimeR, "+")
local Second = TimeR:sub(Last, Ser - 1)
print(Year .. ", " .. Month .. ", " .. Day .. ", " .. Hour .. ", " .. Minute .. ", " .. Second)
end
TimeThread()
Try this one-liner:
Year, Month, Day, Hour, Minute, Second = TimeR:match("(.-)%-(.-)%-(.-)T(.-):(.-):(.-)%+")
Related
I defined a table as follows:
syms = "A"
datetimes = 2021.01.01..2022.01.01
n = 200
t = table(take(datetimes,n) as trade_time, take(syms,n) as sym,take(500+rand(10.0,n), n) as price)
You can try the following script:
syms = "A"
datetimes = 2021.01.01..2022.01.01
n = 200
t = table(take(datetimes,n) as trade_time, take(syms,n) as sym,take(500+rand(10.0,n), n) as price)
dayNum = 3
tbName = "t"
def getLastNDay(tbName, dayNum){
colName = "price"
scripts = "update " + tbName + " set lastNPrice = fixedLengthArrayVector("
for(n in 1..(dayNum-1)){
scripts = scripts + "move(" + colName + "," + n + "),"
}
scripts = scripts +"move(" + colName + "," + dayNum + "))"
print(scripts)
runScript(scripts)
}
getLastNDay(tbName, dayNum)
Output:
i'm trying to visualize my data that i have stored in a word table. I can call the table data with ThisDocument.Tables(6).Cell(i,j).Range.Text. I tried to copy this data to the datasheet of the word graph, but this was unsuccessful.
Word table with data
The chart has to visualize the amount of currency in a timeline with time on the x-axis and the amount of money on the y-axis. I have allready inserted a chart in my word document but i need to access its datasheet.
Graph in word that i want to show
Does anyone have an example code that i can use to solve this problem?
I'm trying to build my code like this:
Dim graph As Word.Chart
Set graph = ThisDocument.InlineShapes(1).Chart
If Not Len(ThisDocument.Tables(3).Cell(2, 1).Range.Text) = 2 Then
Dim temp As String
For i = 0 To ThisDocument.Tables(3).Rows.Count - 2
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 1).Value = Left(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text) - 2)
temp = Left(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text) - 2)
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 2).Value = Right(temp, Len(temp) - 2)
Next i
End If
I'm trying to build my code like this:
Dim graph As Word.Chart
Set graph = ThisDocument.InlineShapes(1).Chart
If Not Len(ThisDocument.Tables(3).Cell(2, 1).Range.Text) = 2 Then
Dim temp As String
For i = 0 To ThisDocument.Tables(3).Rows.Count - 2
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 1).Value = Left(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 3).Range.Text) - 2)
temp = Left(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text, Len(ThisDocument.Tables(3).Cell(2 + i, 4).Range.Text) - 2)
graph.ChartData.Workbook.Worksheets(1).Cells(1 + i, 2).Value = Right(temp, Len(temp) - 2)
Next i
End If
Hi I am getting the following error when computing GMT Time using nanoseconds as a long.
System.OverflowException: 'Arithmetic operation resulted in an overflow.'
Is there another data type that would work better with big values?
Private Sub gmtime(ByVal iSeconds As Object, ByVal iNanoseconds As Long, ByRef Timestamp As String)
Dim time As Object
Dim islpyr, lpcnt As Long
Dim t As Object
Dim i As Object
Dim ystart As Long
Dim y As Long
Dim sph As Object 'seconds per hour
Dim spd As Object 'seconds per day
Dim spy As Object 'seconds per year
Dim tm_sec As Long
Dim tm_min As Long
Dim tm_hour As Long
Dim tm_mday As Long
Dim tm_mon As Long
Dim tm_year As Long
Dim tm_wday As Long
Dim tm_yday As Long
Dim tm_isdst As Long
Dim mons(11) As Long
Dim temp As Object
Dim iMicroSeconds As Long
Dim iZeroCount As Long
Dim strZero As String
Dim strMicro As String
Dim iMicroData As Long
mons(0) = 31
mons(1) = 28
mons(2) = 31
mons(3) = 30
mons(4) = 31
mons(5) = 30
mons(6) = 31
mons(7) = 31
mons(8) = 30
mons(9) = 31
mons(10) = 30
mons(11) = 31
sph = CDec(60 * 60)
spd = CDec(24 * sph)
spy = CDec(365 * spd + 6 * sph) 'a year is about 365.25 days
tm_isdst = 0
time = CDec(iSeconds)
If time < 0 Then
time = time * (-1)
End If
i = CDec(time)
i = Fix(i / spd) Mod 7 + 4
While i >= 7
i = i - 7
End While
tm_wday = i
temp = Fix(time / spd)
temp = temp * spd
i = time - temp
tm_hour = Fix(i / sph) Mod 24
tm_min = Fix(i / 60) Mod 60
tm_sec = i Mod 60
y = Fix(time / spy)
y = y + 370
time = Fix(time / spd)
Do
islpyr = 0
If ((y Mod 4) = 0) And (((y Mod 100) <> 0) Or ((y Mod 400) = 0)) Then
islpyr = 1
End If
lpcnt = Fix(y / 4)
lpcnt = lpcnt - Fix(y / 100)
lpcnt = lpcnt + Fix(y / 400)
lpcnt = lpcnt - 89
ystart = (y - 370) * 365 + lpcnt
If ystart > time Then
y = y - 1
End If
Loop While ystart > time
time = time - ystart
If time = 365 Then
time = 0
y = y + 1
End If
If islpyr Then
time = time + 1
End If
tm_yday = time
time = time + 1
For i = 0 To 10
t = mons(i)
If (i = 1) And (islpyr = 1) Then
t = t + 1
End If
If time <= t Then
Exit For
End If
time = time - t
Next i
tm_year = y - 300 + 1900
tm_mon = i + 1
tm_mday = time
strZero = "."
iZeroCount = 6
iMicroSeconds = Fix(iNanoseconds / 1000)
iMicroData = iMicroSeconds
While iMicroSeconds <> 0
iMicroSeconds = Fix(iMicroSeconds / 10)
If (iMicroData Mod 10) = 0 Then
iMicroData = iMicroSeconds
End If
If iZeroCount <> 0 Then
iZeroCount = iZeroCount - 1
End If
End While
For i = 1 To iZeroCount
strZero = strZero + "0"
Next i
If Fix(iNanoseconds / 1000) <> 0 Then
strMicro = strZero + CStr(Fix(iNanoseconds / 1000))
Else
strMicro = strZero
End If
Timestamp = CStr(tm_year) + "-" + CStr(tm_mon) + "-" + CStr(tm_mday) + " " + CStr(tm_hour) + ":" + CStr(tm_min) + ":" + CStr(tm_sec) + strMicro
End Sub
It wouldn't let me add the code as the post is mostly code. The nanoseconds values are coming from a waveform file and this function is used to process it into GMT time.
The function is called on a loop for each line of the file and returns this error mid-way through the loop.
The value is '1.5518651852110167E+270' when it hits the error
I'm sorry but there's no way to put a 10^270 number inside ANY variable.
The biggest variable for numbers is Long that holds:
signed 64-bit (8-byte) integers ranging in value from
-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
(9.2...E+18).
From Long DataType
I sueggest you to avoiding counting time in nanoseconds instead use seconds, minutes, or even days if your TimeSpan is too big.
Counting time in nanoseconds is pointless.
Remarks
If you couldn't avoid using nanoseconds because your file is in nanoseconds your only option is to convert you nanoseconds value in a DateTime format on every line and hope that the value will be shorter than 9.2E+18
Otherwise you could use a BigInteger and use it to calculate a DateTime for every line of your file.
Just for the curiosity: You would need a 896 bytes unsigned DataType to store a 1.5*10^270 inside it.
I'm doing a school project in Visual Basic (using visual studio 2015) and i'm kinda stuck.
My goal is to create a lottery, where player chooses 6 numbers from checkboxes, then he generates six random numbers (1 - 49) and finally, those two sets should be compared and needed result is the number of correctly guessed numbers.
I have both results (guessed numbers, generated numbers) saved in two different labels.
The checkboxes itself are genereted like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lev = 20
tt = 0
For j = 1 To 50
tt = tt + 1
n = n + 1
box(j) = New CheckBox
box(j).Name = "box(" & Str(j) & ")"
If n = 11 Then lev = lev + 110 : n = 1 : tt = 1
box(j).Left = lev
box(j).Parent = Me
box(j).Top = tt * 20
box(j).Tag = j
box(j).Text = j
box(j).Visible = True
Next
box(50).Enabled = False
End Sub
First label (guessed numbers) is filled this way (i'm not posting whole code)
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
and the second one (generated numbers) like this:
Do
rn = rg.Next(1, 50)
If Not r.Contains(rn) Then
r.Add(rn)
End If
Loop Until r.Count = 6
Label1.Text = r(0).ToString + " " + r(1).ToString + " " + r(2).ToString + " " + r(3).ToString + " " + r(4).ToString + " " + r(5).ToString
any idea how to compare numbers stored in those labels and get the result (number of correctly guessed numbers).
thanks in advance
You can compare numbers in the labels by splitting the Text properties of the labels into arrays of strings and converting them to integer arrays. First though there is a tiny problem with your code that adds the guessed numbers to the label.
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + " " + box(j).Text
Next
The " " should be moved to the end of the line because at the moment, the label will always start with a space and that messes with the function below. So you should have -
For j = 1 To 50
If box(j).Checked = True Then Label9.Text = Label9.Text + box(j).Text + " "
Next
Ok. The function below splits the two text labels into their own array and loops through the guesses and checks if any number is contained in the generated numbers. It then returns the number of matches.
Private Function ComparePicks() As Integer
Dim numbersMatched As Integer
Dim picks(5) As Integer
Dim generatedNumbers(5) As Integer
For i As Integer = 0 To 5
picks(i) = CInt(Split(Label9.Text, " "c)(i))
Next
For i As Integer = 0 To 5
generatedNumbers(i) = CInt(Split(Label1.Text, " "c)(i))
Next
For i As Integer = 0 To 5
If generatedNumbers.Contains(picks(i)) Then
numbersMatched += 1
End If
Next
Return numbersMatched
End Function
I have a problem with calculating difference between 2 dates where first is older than second.
For example: I want to find difference between
5.5.2015 and 1.11.2014
I used function
=IF((A(DATEDIF(B12,$W$3,"M")<=12,RANK(Q12,Q:Q)<=11)),Q12;0)
but the function is limited only to situations where the second date is higher than the first one.
I want to know whether B12 is within last 12 months from given date. If it is, then I want to calculate with it.
Is there any way to calculate backwards in excel or VBA?
Thank you.
I know this is an old post already but for anyone who needs this...
Function FindDateDiff(myDate1 As Date, myDate2 As Date) As String
Dim myYears As Long, myMonths As Long, myDays As Long
Dim yearString As String, monthString As String, dayString As String, FinalString As String
If myDate1 > myDate2 Then
myYears = Year(myDate1) - Year(myDate2)
myMonths = Month(myDate1) - Month(myDate2)
myDays = Day(myDate1) - Day(myDate2)
If myDays < 0 Then
myMonths = myMonths - 1
myDays = Day(WorksheetFunction.EoMonth(myDate1, 0)) - Abs(myDays) - 1
End If
Else
myYears = Year(myDate2) - Year(myDate1)
myMonths = Month(myDate2) - Month(myDate1)
myDays = Day(myDate2) - Day(myDate1)
If myDays < 0 Then
myMonths = myMonths - 1
myDays = Day(WorksheetFunction.EoMonth(myDate2, 0)) - Abs(myDays) - 1
End If
End If
If myMonths < 0 Then
myYears = myYears - 1
myMonths = 12 - Abs(myMonths)
End If
If myYears = 0 Then
yearString = ""
ElseIf myYears = 1 Then
yearString = myYears & " year, "
ElseIf myYears > 1 Then
yearString = myYears & " years, "
End If
If myMonths = 0 Then
monthString = ""
ElseIf myMonths = 1 Then
monthString = myMonths & " month, "
ElseIf myMonths > 1 Then
monthString = myMonths & " months, "
End If
If myDays = 0 Then
dayString = ""
ElseIf myDays = 1 Then
dayString = myDays & " day"
ElseIf myDays > 1 Then
dayString = myDays & " days"
End If
FinalString = yearString & monthString & dayString
If Right(FinalString, 2) = ", " Then FinalString = Left(FinalString, Len(FinalString) - 2)
FindDateDiff= FinalString
End Function
Just paste this function in a new module in the workbook and you can start calling this function. '=FindDateDiff(A1,B1)'
This function only require 2 dates as arguments and the order doesn't matter.
I've tested this function with both UK and US format, both works exactly the same.
I used DateDiff before, but the calculation for days and months returns an incorrect value and could be very confuse sometimes.
In VBA use the same function.
NoOfDays = DateDiff("D", DATE1, DATE2)
NoOfDays returns either positive or negative value depending on the dates
I have it solved by using ISERROR
=IF(ISERROR(DATEDIF(RC[-16],R3C23,""M"")<=12),0,RC[-1])