Rails 3: datetime_select with am/pm options - ruby-on-rails-3

In Rails 3, is there a way to use datetime_select and display hours showing 12 hour am/pm options rather than 24-hour options?

In case anyone stumbles upon this question, the answer for rails 3.2 is:
<%= f.datetime_select :attribute_name,
ampm: true %>

Here's the method I added to my helper class:
def am_pm_hour_select(field_name)
select_tag(field_name,options_for_select([
["1 AM", "01"],["2 AM", "02"],["3 AM", "03"],["4 AM", "04"],["5 AM", "05"],["6 AM", "06"],
["7 AM", "07"],["8 AM", "08"],["9 AM", "09"],["10 AM", "10"],["11 AM", "11"],["12 PM", "12"],
["1 PM", "13"],["2 PM", "14"],["3 PM", "15"],["4 PM", "16"],["5 PM", "17"],["6 PM", "18"],
["7 PM", "19"],["8 PM", "20"],["9 PM", "21"],["10 PM", "22"],["11 PM", "23"],["12 AM", "0"]]))
end
Then I plugged that method into my view:
<%= am_pm_hour_select "eventtime[start(4i)]" %>
It seemed to do the trick, but if there's a more idiomatic way of doing this, I'd be interested to hear.
(update: fixed bugs found by thucydides)

The code below treats midnight and noon incorrectly: it calls noon '12 AM,' which is midnight; it calls midnight '12 PM,' which is noon.
Also, the code should use 0:00 as midnight, which is the international standard.
Fixed:
def am_pm_hour_select(field_name)
select_tag(field_name,options_for_select([
["1 AM", "01"],["2 AM", "02"],["3 AM", "03"],["4 AM", "04"],["5 AM", "05"],["6 AM", "06"],
["7 AM", "07"],["8 AM", "08"],["9 AM", "09"],["10 AM", "10"],["11 AM", "11"],["Noon", "12"],
["1 PM", "13"],["2 PM", "14"],["3 PM", "15"],["4 PM", "16"],["5 PM", "17"],["6 PM", "18"],
["7 PM", "19"],["8 PM", "20"],["9 PM", "21"],["10 PM", "22"],["11 PM", "23"],["Midnight", "0"]]))
end

Related

Find all nearest date from list of date in kotlin

I have current date and I have of list which is coming from server. I want to find all first nearest data.
"Results": [
{
"date": "May 9, 2020 8:09:03 PM",
"id": 1
},
{
"date": "Apr 14, 2020 8:09:03 PM",
"id": 2
},
{
"date": "Mar 15, 2020 8:09:03 PM",
"id": 3
},
{
"date": "May 9, 2020 8:19:03 PM",
"id": 4
}
],
Today date is Wed Jul 20 00:00:00 GMT+01:00 2022 I am getting through this my own StackOverflow. Inside this SO I am taking current date.
Expected Output
[Result(date=May 9, 2020 8:09:03 PM, id = 1), Result(date=May 9, 2020 8:19:03 PM, id = 4)]
So How can I do in idiomatic way in kotlin ?
There are quite a few ways this question could be solved with, ranging from simple to moderately complex depending on requirements such as efficiency. With some assumptions, below is a linear-time solution that is decently idiomatic and is only 3 lines in essence.
import kotlin.math.abs
import java.lang.Long.MAX_VALUE
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import java.time.temporal.Temporal
data class Result(val date: LocalDateTime, val id: Int)
fun getClosestDays(toDate: Temporal, results: List<Result>): List<Result> {
// Find the minimum amount of days to the current date
var minimumDayCount = MAX_VALUE
results.forEach { minimumDayCount = minOf(minimumDayCount, abs(ChronoUnit.DAYS.between(toDate, it.date))) }
// Grab all results that match the minimum day count
return results.filter { abs(ChronoUnit.DAYS.between(toDate, it.date)) == minimumDayCount }
}
fun main() {
getClosestDays(
LocalDateTime.now(),
listOf(
Result(LocalDateTime.of(2020, 5, 9, 8, 9, 3), 1),
Result(LocalDateTime.of(2020, 4, 14, 8, 9, 3), 2),
Result(LocalDateTime.of(2020, 3, 15, 8, 9, 3), 3),
Result(LocalDateTime.of(2020, 5, 9, 8, 19, 3), 4)
)
).also { println(it) }
}
Here is the output:
[Result(date=2020-05-09T08:09:03, id=1), Result(date=2020-05-09T08:19:03, id=4)]
And here you can play with it yourself.

Kotlin - List of Object Filter

I have a list of nested objects I want a logic to filter the list.
Theater, Screen, Show, Movie are data classes.
Theater(
theaterId = 1, name = "Inox", screen = listOf(
Screen(id = 1, showList = listOf(
Show(1, "9 AM", movieId = 1,160) ,
Show(2, "12 PM", movieId = 2,200),
Show(3, "3 PM", movieId = 3,240)
)) ,
Screen(id = 2, showList = listOf(
Show(1, "10 AM", movieId = 4,160),
Show(2, "1 PM", movieId = 5,200),
Show(3, "4 PM", movieId = 1,240)
)))) , Theater(
theaterId = 2, name = "PVR", screen = listOf(
Screen(id = 1, showList = listOf(
Show(1, "9 AM", movieId = 2,160),
Show(2, "12 PM", movieId = 3,200),
Show(3, "3 PM", movieId = 4,240)
)),
Screen(id = 2, showList = listOf(
Show(1, "10 AM", movieId = 5,160),
Show(2, "1 PM", movieId = 1,200),
Show(3, "4 PM", movieId = 2,240)
))))
The expected list after passing a movieId. = 1 should be like .
Theater(
theaterId = 1, name = "Inox", screen = listOf(
Screen(id = 1, showList = listOf(
Show(1, "9 AM", movieId = 1,160) ,
)) ,
Screen(id = 2, showList = listOf(
Show(3, "4 PM", movieId = 1,240)
)))) , Theater(
theaterId = 2, name = "PVR", screen = listOf(
Screen(id = 1, showList = listOf(
)),
Screen(id = 2, showList = listOf(
Show(2, "1 PM", movieId = 1,200),
))))
In the second theater there is no movie running in screen 1 so it should be empty.
val result = input.map { theater ->
theater.copy(screen = theater.screen.map { screen ->
screen.copy(showList = screen.showList.filter { it.movieId == 1 })
})
}
https://pl.kotl.in/duRIOgCV7

Combining separate temporal measurement series

I have a data set that combines two temporal measurement series with one row per measurement
time: 1, measurement: a, value: 5
time: 2, measurement: b, value: false
time: 10, measurement: a, value: 2
time: 13, measurement: b, value: true
time: 20, measurement: a, value: 4
time: 24, measurement: b, value: true
time: 30, measurement: a, value: 6
time: 32, measurement: b, value: false
in a visualization using Vega lite, I'd like to combine the measurement series and encode measurement a and b in a single visualization without simply layering their representation on a temporal axis but representing their value in a single encoding spec.
either measurement a values need to be interpolated and added as a new value to rows of measurement b
eg:
time: 2, measurement: b, value: false, interpolatedMeasurementA: 4.6667
or the other way around, which leaves the question of how to interpolate a boolean. maybe closest value by time, or simpler: last value
eg:
time: 30, measurement: a, value: 6, lastValueMeasurementB: true
I suppose this could be done either query side in which case this question would be regarding indexDB Flux query language
or this could be done on the visualization side in which case this would be regarding vega-lite
There's not any true linear interpolation schemes built-in to Vega-Lite (though the loess transform comes close), but you can achieve roughly what you wish with a window transform.
Here is an example (view in editor):
{
"data": {
"values": [
{"time": 1, "measurement": "a", "value": 5},
{"time": 2, "measurement": "b", "value": false},
{"time": 10, "measurement": "a", "value": 2},
{"time": 13, "measurement": "b", "value": true},
{"time": 20, "measurement": "a", "value": 4},
{"time": 24, "measurement": "b", "value": true},
{"time": 30, "measurement": "a", "value": 6},
{"time": 32, "measurement": "b", "value": false}
]
},
"transform": [
{
"calculate": "datum.measurement == 'a' ? datum.value : null",
"as": "measurement_a"
},
{
"window": [
{"op": "mean", "field": "measurement_a", "as": "interpolated"}
],
"sort": [{"field": "time"}],
"frame": [1, 1]
},
{"filter": "datum.measurement == 'b'"}
],
"mark": "line",
"encoding": {
"x": {"field": "time"},
"y": {"field": "interpolated"},
"color": {"field": "value"}
}
}
This first uses a calculate transform to isolate the values to be interpolated, then a window transform that computes the mean over adjacent values (frame: [1, 1]), then a filter transform to isolate interpolated rows.
If you wanted to go the other route, you could do a similar sequence of transforms targeting the boolean value instead.

REGEXP_SUBSTR Oracle | Extract a string between two delimiters

Im trying to extract a name from a column that return a big clob like this one:
{
"idStatus":6,
"atrasoSLA":0.0,
"atrasoSLAStr":"00:00",
"baseReports":false,
"idItemTrabalho":10019,
"portfolio":"Segurança",
"servicoRelacionado":"ATIVOS (SEGURANÇA)",
"idStatusControleSla":8,
"dataFinalUltimoControleSla":"Sep 3, 2018 11:55:12 AM",
"grupoExecutor":"Segurança",
"idGrupoExecutor":71,
"contrato":"Central de Serviços - SEFAZ-MA",
"dataHoraCaptura":"Sep 3, 2018 11:42:32 AM",
"dataHoraFim":"Sep 3, 2018 11:55:12 AM",
"dataHoraInicio":"Sep 3, 2018 11:42:19 AM",
"dataHoraInicioSLA":"Sep 3, 2018 11:42:19 AM",
"dataHoraInicioSLAStr":"09/03/2018 11:42 AM",
"dataHoraLimite":"Sep 4, 2018 7:42:00 AM",
"dataHoraLimiteStr":"09/04/2018 07:42 AM",
"dataHoraSolicitacao":"Sep 3, 2018 11:42:19 AM",
"dataHoraSolicitacaoStr":"09/03/2018 11:42 AM",
"demanda":"Requisição",
"descricao":"\u003cp\u003eRafael reportou que o CITSMART homologa\u0026ccedil;\u0026atilde;o\u0026nbsp;esta\u0026nbsp;offline.\u0026nbsp;\u003c/p\u003e\n",
"descricaoSemFormatacao":"Rafael reportou que o CITSMART homologação esta offline. ",
"descrSituacao":"citcorpore.comum.fechada",
"detalhamentoCausa":"\u003cp\u003eO incidente foi causado devido o servidor de aplica\u0026ccedil;\u0026otilde;es do CITSMART estar em DHCP, com isso o IP 10.1.1.247 foi alterado para outro IP causando a falha na comunica\u0026ccedil;\u0026atilde;o com o APACHE.\u003c/p\u003e\n",
"emailcontato":"rafael.feitosa#sefaz.ma.gov.br",
"emailResponsavel":"nilson#sefaz.ma.gov.br",
"enviaEmailAcoes":"S",
"enviaEmailCriacao":"S",
"enviaEmailFinalizacao":"S",
"faseAtual":"Execução",
"grupoNivel1":"SDNIVEL1",
"idAcordoNivelServico":8,
"idCalendario":2,
"idContatoSolicitacaoServico":1844,
"idContrato":2,
"idFaseAtual":2,
"idOrigem":1,
"idPrioridade":5,
"idServico":70,
"idServicoContrato":61,
"idSolicitacaoServico":1559,
"idSolicitante":2220,
"idTipoDemandaServico":1,
"idUnidade":104,
"idTarefaEncerramento":10019,
"impacto":"B",
"nomecontato":"Rafael Brito Feitosa",
"nomeServico":"ATIVOS (SEGURANÇA) - Análise LOGs/Desempenho/Capacidade/Disponibilidade",
"nomeTarefa":"Atender solicitacao",
"nomeUnidadeResponsavel":"COTEC",
"observacao":" ",
"origem":"Central de Serviços",
"prazoCapturaHH":0,
"prazoCapturaMM":0,
"prazoHH":8,
"prazoMM":0,
"prioridade":"5",
"responsavel":"Nilson Roniery da Silva Vieira (COTEC)",
"resposta":"\u003cp\u003ePara solucionar este foi inserido um IP fixo da DMZ EXTERNA e mais o seguintes passos:\u003c/p\u003e\n\n\u003col\u003e\n\t\u003cli\u003eAdicionado IP fixo na m\u0026aacute;quina, sendo ele: 172.20.1.55;\u003c/li\u003e\n\t\u003cli\u003eForam criadas regras no Firewall:\n\t\u003cul\u003e\n\t\t\u003cli\u003e172.20.1.55 --\u0026gt; 10.1.1.56 (BD) porta 1521\u003c/li\u003e\n\t\t\u003cli\u003e172.20.1.55 -\u0026gt;\u0026gt;\u0026nbsp; INTERNET portas 80 e 443\u003c/li\u003e\n\t\t\u003cli\u003eVPN CIT --\u0026gt;\u0026nbsp;172.20.1.55\u003c/li\u003e\n\t\u003c/ul\u003e\n\t\u003c/li\u003e\n\t\u003cli\u003eForam alterados os proxies no apache conforme imagem abaixo\u003cimg src\u003d\"/citsmart/galeriaImagens/1/2/439.png\" style\u003d\"height:105px; width:884px\" /\u003e\u003c/li\u003e\n\t\u003cli\u003eFoi restartado o servi\u0026ccedil;o do citsmart no AS.\u0026nbsp;\n\t\u003cul\u003e\n\t\t\u003cli\u003e\u003cem\u003e#\u0026nbsp;/etc/init.d/citsmart_itsm stop\u003c/em\u003e\u003c/li\u003e\n\t\t\u003cli\u003e\u003cem\u003e# /etc/init.d/citsmart_itsm start\u003c/em\u003e\u003c/li\u003e\n\t\u003c/ul\u003e\n\t\u003c/li\u003e\n\u003c/ol\u003e\n",
"seqReabertura":0,
"servico":"ATIVOS (SEGURANÇA) - Análise LOGs/Desempenho/Capacidade/Disponibilidade",
"situacaoSLA":"A",
"slaACombinar":"N",
"solicitante":"Rafael Brito Feitosa",
"solicitanteUnidade":"Rafael Brito Feitosa",
"solucaoTemporaria":"N",
"telefonecontato":"Não disponível.",
"tempoAtendimentoHH":0,
"tempoAtendimentoMM":12,
"tempoAtrasoHH":0,
"tempoAtrasoMM":0,
"tempoCapturaHH":0,
"tempoCapturaMM":0,
"tempoCapturaSS":13,
"tempoDecorridoHH":0,
"tempoDecorridoMM":0,
"urgencia":"B",
"ordernacao":0,
"usuarioDto":{
"idUsuario":635,
"idEmpregado":635,
"idPerfilAcessoUsuario":6,
"idEmpresa":1,
"login":"sefaz.ma.gov.br\\034013",
"nomeUsuario":"Nilson Roniery da Silva Vieira",
"senha":"f04534e4998415904454ae1ceb2040fa05bf548e",
"status":"A",
"ldap":"S",
"email":"nilson#sefaz.ma.gov.br",
"ldapGroupId":1,
"fromToken":false
},
"idResponsavel":635,
"idGrupoAtual":71,
"idGrupoNivel1":2,
"idTarefa":10019,
"grupoAtual":"Segurança"
}
In this case the name i want is "Nilson Roniery da Silva"
And the only thing i know is that the names always come after the "nomeUsuario":" and at the end have a "
so, how can i make a select that only brings me the name between "nomeUsuario":" and "
You should be able to use json_value to parse this clob:
SELECT JSON_VALUE(mycolumn, '$.usuarioDto.nomeUsuario') FROM mytable

Rendering multi series line chart with time on x axis in AnyChart

I am trying Anychart line chart with multi series with date time on x axis. Not able to render the chart perfectly. Its drawing the series 1 with the given data then for the second series its rendering the date time freshly on x axis after the 1st series values then plotting the series 2.
data is like:
"data": [
{"x": "10/2/2016 01:00:00 AM", "value": "128.14"},
{"x": "10/2/2016 01:10:00 AM", "value": "112.61"}
]
},{
// second series data
"data": [
{"x": "10/2/2016 01:01:00 AM", "value": "90.54"},
{"x": "10/2/2016 01:02:00 AM", "value": "104.19"},
{"x": "10/2/2016 01:11:00 AM", "value": "150.67"}
]
It has to plot on x axis like 10/2/2016 01:00:00 AM, 10/2/2016 01:01:00 AM, 10/2/2016 01:02:00 AM, 10/2/2016 01:10:00 AM, 10/2/2016 01:11:00 AM
but it plotting like 10/2/2016 01:00:00 AM, 10/2/2016 01:10:00 AM, 10/2/2016 01:01:00 AM, 10/2/2016 01:02:00 AM, 10/2/2016 01:11:00 AM
Updating the code:
anychart.onDocumentReady(function() {
// JSON data
var json = {
// chart settings
"chart": {
// chart type
"type": "line",
// chart title
"title": "Axes settings from JSON",
// series settings
"series": [{
// first series data
"data": [
{"x": "10/2/2016 01:00:00 AM", "value": 128.14},
{"x": "10/2/2016 01:10:00 AM", "value": 112.61},
{"x": "10/3/2016 01:00:00 AM", "value": 12.14},
{"x": "10/3/2016 01:10:00 AM", "value": 152.61},
]},{
"data": [
{"x": "10/2/2016 01:09:00 AM", "value": 28.14},
{"x": "10/2/2016 01:11:00 AM", "value": 12.61},
{"x": "10/3/2016 01:01:00 AM", "value": 1.14},
{"x": "10/3/2016 01:12:00 AM", "value": 15.61},
]
}],
// x scale settings
"xScale": {
ticks:
{scale: "DateTime"}
},
xAxes: [{
title: "Basic X Axis"
}],
// chart container
"container": "container"
}
};
// get JSON data
var chart = anychart.fromJson(json);
// draw chart
chart.draw();
});
With this type of data you need to use scatter chart: http://docs.anychart.com/7.12.0/Basic_Charts_Types/Scatter_Chart
Datetime scale should be set like this in JSON:
"xScale": {
type: "datetime",
minimum: "10/02/2016 00:00:00",
maximum: "10/03/2016 12:00:00",
}
Here is a sample: https://jsfiddle.net/3ewcnp5j/102/