How to get the milliseconds from now() in Julia 0.4-pre? - julia

How to get the milliseconds from now()?
Dates.format(now(), "HH:MM:SS.sss")
gives
"18:32:11.000"
where the .sss part is always .000.

now() does't record milliseconds:
julia> #show x=now(); #show Dates.millisecond(x); #show Dates.second(x)
x = now() = 2015-09-04T19:09:32
Dates.millisecond(x) = 0
Dates.second(x) = 32
32

From your other question I found your comment:
dt = Dates.unix2datetime(time())
Dates.millisecond(dt)
Which does the business. Cheers.

Related

Julia UndefVarError on Metaprogramming

I'm trying to do a solver for equations. When I run the code the X variable appears to be undefined, but it prints out perfectly. What am I missing?
I should give the program some numbers, than operations as Macros and it should create an outer product matrix of the operations applied.
function msu()
print("Insert how many values: ")
quantity = parse(Int64, readline())
values = []
for i in 1:quantity
println("x$i")
num1 = parse(Float64, readline())
push!(values, num1)
end
println(values)
print("How many operations? ")
quantity = parse(Int64, readline())
ops = []
for i in 1:quantity
push!(ops, Meta.parse(readline()))
end
mat = zeros((quantity, quantity))
for i in 1:length(mat)
sum = 0
for j in 1:length(values)
# here begins problems, the following prints are for debugging purpose
print(length(values))
func = Meta.parse("$(ops[convert(Int64, ceil(j / quantity))]) * $(ops[convert(Int64, j % quantity)])")
print(func)
x = values[j]
println(x)
sum += eval(func)
end
mat[i] = sum
end
println(mat)
end
msu()
The original code was in Spanish, if you find any typo it's probably because I skipped a translation.

Create a Julia Datetime from a TimePeriod and StartDate

I want to convert an Int64 representing the number of microseconds passed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar) into a Julia datetime.
julia> time = Dates.Microsecond(6369175082331949400)
julia> Dates.format(time, "yyyymmdd HH:MM:SS.sss")
If you need a DateTime, just make sure you have your Int64 correctly in milliseconds, and you can use the (undocumented) UTInstant constructor, and then later add back the fractional microseconds (comment: your example number, 6369175082331949400, seems big for recent Gregorian time in microseconds, it may be nanoseconds):
julia> using Dates
julia> t = now().instant
Dates.UTInstant{Millisecond}(63694318624788 milliseconds)
julia> dump(t)
Dates.UTInstant{Millisecond}
periods: Millisecond
value: Int64 63694318624788
julia> t2 = Dates.UTInstant(Millisecond(63691750823319))
Dates.UTInstant{Millisecond}(63691750823319 milliseconds)
julia> DateTime(t2)
2019-04-24T01:00:23.319
julia> t3 = DateTime(t2)+ Dates.Microsecond(494)
2019-04-24T01:00:23.319
You can get what you want using Dates.epochms2datetime and applying an adjustment to it for your case as shown below.
Lets take datetime_value as the date we are interested in getting:
datetime_value = Dates.DateTime(2019,1,1,0,0,0)
date_start = Dates.DateTime(1,1,1,0,0,0)
date_diff = datetime_value - date_start
This gives you a value of 63681897600000 milliseconds for date_diff. Now Dates.epochms2datetime considers start of epoch as 0000-01-01T00:00:00. So we need to add 1 Year and 1 Day to the result after using Dates.epochms2datetime to arrive at our datetime value from the milliseconds value:
julia> Dates.epochms2datetime(63681897600000) + Dates.Year(1) + Dates.Day(1)
2019-01-01T00:00:00
I'm not sure I completely understand the question, as Dates.Microsecond merely returns the Int64 value of a Date or Time. However, you can create the DateTime value from a specific date and then work from there. Subtraction is allowed for DateTime values and it returns the difference in milliseconds.
using Dates
dateThen = DateTime(1, 1, 1, 0, 0, 0)
dateNow = now(UTC)
diff = dateNow - dateThen
dump(diff * 1000)
Int64 63694261047549000 (or whatever time you run it.)
Using some of the ideas provided, I came up with:
function convert_datetime(time)::DateTime
num = div(time, 100000)
remainder = rem(time, 100000)
time = DateTime(Dates.UTInstant(Millisecond(num))) + Dates.Day(1)
# time = Dates.epochms2datetime(trade.date_time/100000) + Dates.Year(1) + Dates.Day(1)
time + Dates.Microsecond(remainder)
end

Lubridate fix the time units

When we take the difference of two time, there is something going on automatically there in units.
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
> ymd_hms("2016-05-09 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 59.98333 mins
> ymd_hms("2016-05-10 16:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of 1.041655 days
How can I fix the units without using difftime function.
So I can do the following:
VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
ymd_hms("2016-05-10 17:19:33"),
ymd_hms("2016-05-10 19:55:03")
)
diffs = diff(VECTOR)
IntervalsInHours = toHours(diffs)
Additionally, is there any way to know the units being used in a lubridate time object. For example,
> ymd_hms("2016-05-09 15:17:03") - ymd_hms("2016-05-09 15:17:04")
Time difference of -1 secs
The units used here are seconds.
"you want to use diff function to take the time differences between a VECTOR elements, only in the units specified"
pls try below code : (by int_diff function)
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 1 3749 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 0.00 1.04 2.59
see, whatever the time interval min unit is seconds or not, it is always scaled by seconds as below.
> VECTOR = c(ymd_hms("2016-05-10 16:17:03"),
+ #ymd_hms("2016-05-10 16:17:04"),
+ ymd_hms("2016-05-10 17:19:33"),
+ ymd_hms("2016-05-10 19:55:03")
+ )
> as.numeric(int_diff(VECTOR))
[1] 3750 9330
> round(as.numeric(int_diff(VECTOR))/3600,2)
[1] 1.04 2.59
please try below to transform the time difference into hours.
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 15:17:04")
diffs=as.duration(x-y)
IntervalsInHours=as.numeric(abs(diffs))/3600;IntervalsInHours
or you can use this way:
library(lubridate)
x=ymd_hms("2016-05-09 16:17:03")
y=ymd_hms("2016-05-19 16:17:04")
diffs=as.duration(x-y);
IntervalsInHours=abs(diffs)/dhours(1);IntervalsInHours
I wrote two functions in case anyone might find useful.
timeDiffUnitConvert = function(Diffs, to="day", roundingN = 1){
if(to == "day"){
R = round(as.numeric(as.duration(Diffs))/3600/24,roundingN)
} else if (to == "hour") {
R = round(as.numeric(as.duration(Diffs))/3600, roundingN)
} else if (to == "min") {
R = round(as.numeric(as.duration(Diffs))/60, roundingN)
} else if (to == "sec"){
R = round(as.numeric(as.duration(Diffs)), roundingN)
} else {
stop("to which unit? it must be `day`, `hour`, `min` or `sec`.")
}
R
}
timeDiffVector = function(TimeVector, to="day", roundingN = 1, attachNaMode = "none"){
R = timeDiffUnitConvert(diff(TimeVector), to = to, roundingN = roundingN)
if(attachNaMode == "leading"){
R = c(NA,R)
} else if(attachNaMode == "trailing"){
R = c(R,NA)
} else{
stop("check your attachNaMode: shall be either `leading` or `trailing`")
}
R
}

set datetime milliseconds precision - elixir

I am trying to get a datetime which has only 3 digits in the sub-second part.
Using timex I get the following result:
iex(12)>   {:ok, date} = Timex.format(Timex.shift(Timex.local, days: 16), "{ISO:Extended}")
{:ok, "2017-04-22T09:00:44.403879+03:00"}
How can I get something like this:
{:ok, "2017-04-22T09:00:44.403+03:00"} ?
Since Elixir 1.6.0 there is now the truncate/2 function present on modules Time, DateTime and NativeDateTime for this.
Here is an example using DateTime.truncate/2
iex(1)> dt = Timex.now()
#DateTime<2018-02-16 19:03:51.430946Z>
iex(2)> dt2 = DateTime.truncate(dt, :millisecond)
#DateTime<2018-02-16 19:03:51.430Z>
DateTime has a microsecond field which is a tuple containing the value and precision. If you change the precision to 3, you'll get 3 digits in the microsecond output. I couldn't find any function in Timex which does this, but you can modify the value manually:
iex(1)> dt = %{microsecond: {us, precision}} = Timex.now
#<DateTime(2017-04-06T08:26:24.041004Z Etc/UTC)>
iex(2)> precision
6
iex(3)> dt2 = %{dt | microsecond: {us, 3}}
#<DateTime(2017-04-06T08:26:24.041Z Etc/UTC)>
iex(4)> dt2 |> Timex.format!("{ISO:Extended}")
"2017-04-06T08:26:24.041+00:00"

Can Do While Loop stop when counter reaches X

I want to edit some legacy code written in classic-ASP.
Currently I've got a subroutine declared that uses a for-next loop to output some radio buttons:
For i = 1 to Cols
response.write "blah"
...
Next
i is simply a counter, Cols is a value passed to the sub-routine. I tried editing the for-loop to be a while loop instead:
i = Start
do while i <= Cols
response.write "blah"
...
i = i + 1
loop
But I get a Response Buffer Limit Exceeded error. If I replace Cols with the value it works fine. Is this a limitation in classic-ASP?
Reason I want to use a do while loop is because currently the sub-routine is limited to looping from 1 to Cols. It would be helpful to sometimes specify the loop counts backwards (i.e. step -1) but I can't write:
if Direction = Backwards then
For i = Cols to 1 step -1
else
For i = 1 to Cols
end if
How about:
If Direction = Backwards Then
cs = 10
ce = 1
s = -1
Else
cs = 1
ce = 10
s = 1
End If
For i = cs To ce Step s
''
Next

Resources