Extracting Hours, Minutes, and Seconds from a Time Value in ProBuilder

06 Jan 2016
0 comment
0 attachment

This code snippet demonstrates how to break down a time value into hours, minutes, and seconds components using basic arithmetic operations in ProBuilder language. The time is initially in a format that combines hours, minutes, and seconds (HHMMSS), and the goal is to extract each component separately.


hhmmss = opentime
hhmm00 = max(0,(round((opentime/100)-0.5))*100)
hh0000 = max(0,(round((opentime/10000)-0.5))*10000)
ss = hhmmss - hhmm00
mm = (hhmmss - hh0000 - ss)/100
hh = hh0000/10000

Explanation of the code:

  • hhmmss is assigned the value of opentime, which is assumed to be a time in HHMMSS format.
  • hhmm00 calculates the time rounded down to the nearest hour and minute (HHMM00). This is done by dividing opentime by 100, rounding to the nearest whole number, and then multiplying back by 100 to get a format without seconds.
  • hh0000 calculates the time rounded down to the nearest hour (HH0000). This involves dividing opentime by 10000, rounding to the nearest whole number, and then multiplying back by 10000 to get a format without minutes and seconds.
  • ss (seconds) is found by subtracting hhmm00 from hhmmss, effectively removing the hour and minute components from the original time.
  • mm (minutes) is calculated by subtracting hh0000 and ss from hhmmss, then dividing by 100 to shift the decimal place correctly.
  • hh (hours) is simply hh0000 divided by 10000, adjusting the scale to get the hour component alone.

This snippet is useful for time manipulation tasks where separating these components is necessary for further processing or display.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/add-subtract-time/#post-118177

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...