Convert seconds to ISO8601 duration in Freemarker - freemarker

How to convert seconds to ISO 8601 Duration in Freemarker?
For eg. 140 seconds -> PT2M20S
Is there any freemarker builtins that can come in handy here? Or String manipulation?

There isn't (as of 2.3.28). You will have to write a #function or Java methods or TemplateMethodModelEx for it. (BTW, I believe it would PT2M20S.)

I ended up adding this function:
<#function convertDurationSeconds seconds>
<#local mins = (seconds/60)?int />
<#local seconds = (seconds%60) />
<#return 'PT' + (mins > 0)?then(mins + 'M','') + (seconds > 0)?then(seconds + 'S','') />
</#function>
Note: I needed it for min and seconds only since it was for short video durations, this function can be extended to represent other time intervals (Y, M(month), W, D, M, and S)

Related

FreeMarker - add days to dates

I am trying to add 1 day to the current date. I do know ${.now?date} = The Current Date. But how do I add a day to that? I read that you have to use milliseconds (86,400,000 milliseconds) but I'm unsure what the operation should be.
The detailed answer is here:
<#assign currentDate = .now>
Current Date : ${currentDate?date}<br>
<#assign numberOfDays = 1?long>
<#assign timeInMillisecond = (1000 * 60 * 60 * 24 * numberOfDays) >
<#assign aDate = currentDate?long + timeInMillisecond?long>
<#assign Diff = aDate?long>
<#assign OneDayAfterDate = Diff?number_to_date>
<br>Date after ${numberOfDays} Days : ${OneDayAfterDate}<br>
<br>Date after ${numberOfDays} Days in UTC format : ${OneDayAfterDate?iso_utc}<br>
Figured it out!
${(.now?long + 86400000)?number_to_date }

Ruby time subtraction

There is the following task: I need to get minutes between one time and another one: for example, between "8:15" and "7:45". I have the following code:
(Time.parse("8:15") - Time.parse("7:45")).minute
But I get result as "108000.0 seconds".
How can I fix it?
The result you get back is a float of the number of seconds not a Time object. So to get the number of minutes and seconds between the two times:
require 'time'
t1 = Time.parse("8:15")
t2 = Time.parse("7:45")
total_seconds = (t1 - t2) # => 1800.0
minutes = (total_seconds / 60).floor # => 30
seconds = total_seconds.to_i % 60 # => 0
puts "difference is #{minutes} minute(s) and #{seconds} second(s)"
Using floor and modulus (%) allows you to split up the minutes and seconds so it's more human readable, rather than having '6.57 minutes'
You can avoid weird time parsing gotchas (Daylight Saving, running the code around midnight) by simply doing some math on the hours and minutes instead of parsing them into Time objects. Something along these lines (I'd verify the math with tests):
one = "8:15"
two = "7:45"
h1, m1 = one.split(":").map(&:to_i)
h2, m2 = two.split(":").map(&:to_i)
puts (h1 - h2) * 60 + m1 - m2
If you do want to take Daylight Saving into account (e.g. you sometimes want an extra hour added or subtracted depending on today's date) then you will need to involve Time, of course.
Time subtraction returns the value in seconds. So divide by 60 to get the answer in minutes:
=> (Time.parse("8:15") - Time.parse("7:45")) / 60
#> 30.0

Faster date formatting in R?

I often need to convert (long) character strings into the date class in R. I notice that this step seems quite slow.
Example:
date <- c("5/31/2013 23:30", "5/31/2013 23:35", "5/31/2013 23:40", "5/31/2013 23:45", "5/31/2013 23:50", "5/31/2013 23:55")
Date <- as.POSIXct(date, format="%m/%d/%Y %H:%M")
This isn't a huge problem, but I wonder if I'm overlooking an easy route to increased efficiency. Any tips for speeding this up? Thanks.
Since I wrote this before it was pointed out this is a duplicate, I'll add it as an answer anyway. Basically package fasttime can help you IF you have dates AFTER 1970-01-01 00:00:00 AND they are GMT AND they are of the format year, month, day, hour, minute, second. If you can rewrite your dates to this format then fastPOSIXct will be quick:
# data
date <- c( "2013/5/31 23:30" , "2013/5/31 23:35" , "2013/5/31 23:40" , "2013/5/31 23:45" )
require(fasttime)
# fasttime function
dates.ft <- fastPOSIXct( date , tz = "GMT" )
# base function
dates <- as.POSIXct( date , format= "%Y/%m/%d %H:%M")
# rough comparison
require(microbenchmark)
microbenchmark( fastPOSIXct( date , tz = "GMT" ) , as.POSIXct( date , format= "%Y/%m/%d %H:%M") , times = 100L )
#Unit: microseconds
# expr min lq median uq max neval
# fastPOSIXct(date, tz = "GMT") 19.598 21.699 24.148 25.5485 215.927 100
# as.POSIXct(date, format = "%Y/%m/%d %H:%M") 160.633 163.433 168.332 181.9800 278.220 100
But the question would be, is it quicker to transform your dates to a format fasttime can accept or just use as.POSIXct or buy a faster computer?!

ROR + Ruby Date Decrease by 15 minutes

If I have #time = Time.now.strftime("%Y-%m-%d %H:%M:%S"),
How can I reduce this time by 15 minutes ?
I already tried this one :: #reducetime = #time-15.minutes, works fine at console but give errors while execution. Other than this Is there any way to resolve this issue.
Thanks
Your problem is that you're formatting your time into a string before you're done treating it as a time. This would make more sense:
#time = Time.now
#reducetime = #time - 15.minutes
# And then later when you're reading to display #time...
formatted_time = #time.strftime("%Y-%m-%d %H:%M:%S")
You shouldn't format your data until right before you're ready to display it.
If you must have #time as the formatted time then you're going to have to parse it before computing #reducetime:
#reducetime = (DateTime.strptime(#time, "%Y-%m-%d %H:%M:%S") - 15.minutes).to_time

how to convert timestamp on windows ping -s option to date?

ping [SomeIP] -s 4 -t > ping.log
result of that command is like below.
Reply from [SomeIP] : bytes=32 time=4ms TTL=125
Timestamp: [HOP] : 83842793 ->
[HOP] : 83842793 ->
[HOP] : 83832797 ->
[HOP] : 83842793
how can i convert this timestamp to datetime?
The timestamp you get is number of miliseconds past midnight UTC time. Date is current date.
Conversion from here is simple:
time = timestamp /1000
hours = (time / 3600)
minutes = (time / 60) - (hours * 60)
seconds = time mod 60
If anyone is looking for a quick way to convert date/time to timestamp and back, I just came across this chrome extension and I think it's really great! It's the quickest cross platform method and I use it constantly since it parses plenty of date formats and is really fun nice to use.

Resources