IBM ILE RPG - How to store time values greater than 24h - rpgle

How can I store time values (e.g.: 33h 24m 02s) in a TIME variable since *HIVAL - the max value - is 24h 00m 00s.
When storing it in a data structure (DS) sometimes (when a values is less than 10) e.g: 33:24:03 the value is displayed as 33.24.3 since the field is not padding with zeros automatically

Well you have already answered your own question, the limit is 24 hours.
I'm not sure what you are trying to accomplish, but I would suggest you use the %diff() Built-in Function (BIF) to get the difference between the two time/date/timestamp values and store it in an integer. And then to manipulate another time/timestamp field you could use the %seconds() BIF.
For example:
ctl-opt dftactgrp(*no) actgrp(*new);
ctl-opt timfmt(*iso);
dcl-s time1 time inz(t'09.25.00');
dcl-s time2 time inz(t'10.00.00');
dcl-s time3 time;
dcl-s SecondsDiff int(10);
dcl-s result char(1);
SecondsDiff = %diff(time2: time1: *seconds);
time3 = %time() + %seconds(SecondsDiff);
dsply ('In 35 minutes it will be: ' + %char(time3)) '*EXT' result;
*inlr = *on;
This is a very simplistic example, if you could give me more information to what you are trying to accomplish I could give a more specific example.
Edit
Here is a sample procedure that will do what you are requesting:
ctl-opt dftactgrp(*no) actgrp(*new);
dcl-s SecondsChar char(20);
dcl-s Result varchar(32);
dcl-s WaitAnyKey char(1);
dsply 'Enter an amount of seconds: ' '*EXT' SecondsChar;
Result = SecondsToDisplay(%int(SecondsChar));
dsply Result '*EXT' WaitAnyKey;
*inlr = *on;
dcl-proc SecondsToDisplay;
dcl-pi *n varchar(32);
Seconds int(10) value;
end-pi;
dcl-s Result varchar(32) inz('');
dcl-s CurrentValue int(10);
Seconds = %abs(Seconds);
//Get the days
CurrentValue = Seconds / 86400;
if (CurrentValue > 0);
Result = %char(CurrentValue) + 'd ';
Seconds = %rem(Seconds: 86400);
endif;
//Get the hours
CurrentValue = Seconds / 3600;
if (CurrentValue > 0 OR Result <> '');
Result += %char(CurrentValue) + 'h ';
Seconds = %rem(Seconds: 3600);
endif;
//Get the minutes
CurrentValue = Seconds / 60;
if (CurrentValue > 0 OR Result <> '');
Result += %char(CurrentValue) + 'm ';
Seconds = %rem(Seconds: 60);
endif;
//The seconds
Result += %char(Seconds) + 's';
return Result;
end-proc;
And some sample output:
DSPLY Enter an amount of seconds: 799240
DSPLY 9d 6h 0m 40s

Related

Groovy not converting seconds to hours properly

I have an integer that is:
19045800
I tried different code:
def c = Calendar.instance
c.clear()
c.set(Calendar.SECOND, 19045800)
echo c.format('HH:mm:ss').toString()
String timestamp = new GregorianCalendar( 0, 0, 0, 0, 0, 19045800, 0 ).time.format( 'HH:mm:ss' )
echo timestamp
Both return 10:30:00
19045800 seconds is supposed to be over 5000 hours. What am I doing wrong?
I'm not sure what you are looking for. But if your requirement is to calculate the number of hours, minutes, and the remainder of seconds for given seconds following code will work.
def timeInSeconds = 19045800
int hours = timeInSeconds/3600
int minutes = (timeInSeconds%3600)/60
int seconds = ((timeInSeconds%3600)%60)
println("Hours: " + hours)
println("Minutes: " + minutes)
println("Seconds: " + seconds)
You're attempting to use a Calendar, but what you're actually discussing is what Java calls a Duration – a length of time in a particular measurement unit.
import java.time.*
def dur = Duration.ofSeconds(19045800)
def hours = dur.toHours()
def minutes = dur.minusHours(hours).toMinutes()
def seconds = dur.minusHours(hours).minusMinutes(minutes).toSeconds()
println "hours = ${hours}"
println "minutes = ${minutes}"
println "seconds = ${seconds}"
prints:
hours = 5290
minutes = 30
seconds = 0

Is there any way to display time format with am/pm in mongodb

time :
{ $dateToString: { format: "%H:%M:%S:%L%z", date: "$generated_on", timezone: "+05:30" }}
with the help of this time is generated but I want time in AM/PM format}\
So basically, for time being (version 5), Mongo DB does not support the AM/PM format on the date object, you have two options:
option one: Store the correct format in the first place
option two(heavy operation): store a JS function inside the MongoDB for this custom format and call that in your aggregation pipeline, js function would be like this:
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
Add MongoDB custom JS function myCustomDate:
db.system.js.insertOne(
{
_id: "myCustomDate",
value: function (date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
}
);
and use this based on your requirements on mapReduce command

How to count the number of identical letters in a string in RPG

I would like to count the number of 'i' in the name 'olivia' but I have the impression that my condition is not valid?
How to do charAt in RPG 4 please?
FOR i = 1 to %len(name);
IF %check(name : letter) = 0;
count += 1;
ENDIF;
ENDFOR;
Here is the complete code:
**free
dcl-s name varchar(50);
dcl-s letter char(1);
dcl-s count packed(2:0);
dcl-s i packed(3:0);
dcl-s waitInput char(1);
dcl-s message varchar(50);
name = 'olivia';
letter = 'i';
count = 0;
FOR i = 1 to %len(name);
IF %check(name : letter) = 0;
count += 1;
ENDIF;
ENDFOR;
message = 'The name ' + %char(name) + ' has ' + %char(count) + 'time(s) the letter ' + (letter) ;
dsply message '' waitInput;
*INLR = *on;
IF %check(name : letter) = 0;
=>
IF %subst(name :i:1) = letter;

How to delay orders in pine script by numbers of minutes in 1hr timeframe?

I'm trying to delay entry orders by minutes.
ex: At 12:00 pm the macd line crossover signal line, but I wanna start entry.order at 12:02 pm.
So I tried the following code
//#version=4
strategy("delay2",calc_on_every_tick=true)
i_qtyTimeUnits = input(2, "Quantity", minval = 0)
int _timeFrom_ = na
_timeFrom_ := i_qtyTimeUnits*60*1000
// Entry conditions.
fastLength = input(12)
slowlength = input(26)
signalLength = input(9)
MACD = sma(close, fastLength) - sma(close, slowlength)
signal = sma(MACD, signalLength)
delta = MACD - signal
bool goLong = delta>0
bool goShort = delta<0
float entrytime = na
float delaytime = na
entrytime := if goLong
nz(entrytime[1], time)
delaytime := if entrytime>0
nz(delaytime[1], entrytime+_timeFrom_)
delayElapsed = time>=delaytime
plot(entrytime)
plot(delaytime)
plot(time)
if goLong and delayElapsed
strategy.entry("Long", strategy.long, comment="Long")
strategy.close('short', when = goShort)
I've plotted and checked that delayElapsed is true, but it just doesn't work,please help.
I made a script that buys 2 minutes after a red candle has appeared. The buy is marked with an X.
There is a variable in pine script that is called time. The variable keeps track at the time where every candle is displayed. time is the number of milliseconds since 1 Jan 1970. For example, the current time when I'm writing this is 1625334502399.
To determine the time when you should buy, you can add the number of milliseconds you want to the time and store it in a variable.
entrytime := time + 2 * 60 * 1000
So 2 minutes is equal to 2 * 60 * 1000 milliseconds. There are 1000 milliseconds on a second and 60 seconds on a minute.
You should store this value in a var variable because that value doesn't get erased after every bar.
var float entrytime = na
You can change to any timeframe and it will still buy 2 min after.
Here is the full code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at
https://mozilla.org/MPL/2.0/
// © CanYouCatchMe
//#version=4
study("buying after 2 min", overlay=true)
delay_minutes = input(defval = 2, title = "Delay minutes")
var float entrytime = na //is used to store the time when it should buy
if (open > close and na(entrytime)) //Red candle and there is no current "entrytime"
entrytime := time + delay_minutes * 60 * 1000 //"time" is the time in milliseconds where to candle appered. There is 1000 milliseconds on a second. And 60 seconds on a minute.
buy_condition = false
if (time >= entrytime) //Buys if the current time is greater or the same as the stored time.
entrytime := na //Sets entrytime to na so it stops buying.
buy_condition:=true
plotshape(buy_condition, style=shape.xcross, color=color.green, size=size.small) //Plotting an green "X" where it should buy
//Displaying the values "Data Window" on the right for Debugging.
plotchar(time, "time", "", location = location.top)
plotchar(entrytime, "entrytime", "", location = location.top)

How can summarize all numbers in NSArray?

I have NSArray with strings - 00:02:34, 02:05:17 so i need to calculate all strings in my array and get result: 2 hours:7 minutes and 51 seconds.
I tried this:
// Get strings from array for the first and separate for three objects by:
for (NSDictionary *dic in dicts) {
NSString *string = dic[#"duration"]; (my type of string 00:00:00)
NSArray *components = [string componentsSeparatedByString:#":"];
NSInteger minutes = [components[1] integerValue];
NSInteger seconds = [components[2] integerValue];
NSInteger hour = [components[0] integerValue];
}
But how can i summ this date to get results? Thanks for help.
There are a few different ways you could approach this.
Personally, I'd iterate through dicts and convert each duration string to seconds and keep count of the total seconds in an integer outside of the loop.
Then, you can easily convert the cumulative seconds total back into hours, minutes and seconds and compose a string from them, after the loop is complete:
int totalSeconds = 0;
for (NSDictionary * dic in dicts) {
NSString *string = dic[#"duration"];
NSArray *components = [string componentsSeparatedByString:#":"];
totalSeconds += (int) [components[0] integerValue] * 60 * 60;
totalSeconds += (int) [components[1] integerValue] * 60;
totalSeconds += (int) [components[2] integerValue];
}
int hour = totalSeconds / 3600;
int mins = (totalSeconds % 3600) / 60;
int secs = totalSeconds % 60;
NSString * totalString = [NSString stringWithFormat:#"%d:%d:%d", hour, mins, secs];
Note: you'll have to write a bit of code to compose the string and include zeros as appropriate, where any of the values are less than 10.

Resources