How to convert seconds (or mins), derived from $horolog, back to human-readable date/time?

834 views
Skip to first unread message

Micky Hulse

unread,
Aug 6, 2014, 5:14:35 PM8/6/14
to intersystems. public. cache
Hi,

Here's the code I'm dealing with (note the interger division, so
remainder is removed):

set tPeriod = ($PIECE($HOROLOG,",",1) * 1440) + ($PIECE($HOROLOG,",",2) \ 60)
// Convert period to an interval of 60 minutes
set tPeriod = (tPeriod \ 60) * 60

The final result of tPeriod looks like:

91304040

How can I convert the above back to an $horolog? I just need to get
that integer into some form of human-readable date format.

If it's possible to convert, then what combination of $zdatetime
and/or $zdatetimeh should I use?

Tips and/or a kick in the right direction would be very helpful. :)

Thanks!

OldMster

unread,
Aug 6, 2014, 6:48:14 PM8/6/14
to intersystems...@googlegroups.com
The second set tPeriod = (tPeriod \ 60) * 60 seems redundant, but this should work

Set horolog=(tperiod\1440)_","_(tperiod#1440*60)

The result will always been an even hour, since tPeriod is determined to reduce the current horolog to the last hour encountered

Mark

Micky Hulse

unread,
Aug 6, 2014, 7:13:14 PM8/6/14
to intersystems. public. cache
On Wed, Aug 6, 2014 at 3:48 PM, OldMster <sires...@gmail.com> wrote:
> Set horolog=(tperiod\1440)_","_(tperiod#1440*60)

Awesome!!! Thanks you so much Mark! I greatly appreciate the help.
That one had me really stumped. :)

I owe you one!

Have a nice day!

Cheers,
Micky

--
<git.io/micky>

Micky Hulse

unread,
Aug 6, 2014, 9:18:44 PM8/6/14
to intersystems. public. cache
On Wed, Aug 6, 2014 at 3:48 PM, OldMster <sires...@gmail.com> wrote:
> The result will always been an even hour, since tPeriod is determined to
> reduce the current horolog to the last hour encountered

Interestingly, I'm noticing timestamps in future:

w $zdatetime($horolog, 3, 4), " | ", $zdatetime(period, 3, 4)

… which outputs:

2014-08-06 06:13PM | 2014-08-06 06:18PM

When you say "reduce the current horolog to the last hour
encountered", does that mean I should just drop the time (as it's
unreliable)?

It's interesting that the "fixed" timestamps are just a little bit in
the future … I assume this is because the remainder was dropped before
your fix/code got to it?

Thanks again for the help!

Cheers,
Micky

--
<git.io/micky>

OldMster

unread,
Aug 6, 2014, 9:41:51 PM8/6/14
to intersystems...@googlegroups.com
Since you didn't post how you got period, I can't be sure what happened.  When I run it on my system forward through the code you posted and backward through the code I posted, it always comes out on an hour.
Mark

Results:

FLA>D ^ZPERIOD

63405,0     91303200    63405,0     08/06/2014 00:00:00     08/06/2014 00:00:00
63405,900   91303200    63405,0     08/06/2014 00:15:00     08/06/2014 00:00:00
63405,1800  91303200    63405,0     08/06/2014 00:30:00     08/06/2014 00:00:00
63405,2700  91303200    63405,0     08/06/2014 00:45:00     08/06/2014 00:00:00
63405,3600  91303260    63405,3600  08/06/2014 01:00:00     08/06/2014 01:00:00
63405,4500  91303260    63405,3600  08/06/2014 01:15:00     08/06/2014 01:00:00
63405,5400  91303260    63405,3600  08/06/2014 01:30:00     08/06/2014 01:00:00
63405,6300  91303260    63405,3600  08/06/2014 01:45:00     08/06/2014 01:00:00
63405,7200  91303320    63405,7200  08/06/2014 02:00:00     08/06/2014 02:00:00
63405,8100  91303320    63405,7200  08/06/2014 02:15:00     08/06/2014 02:00:00
63405,9000  91303320    63405,7200  08/06/2014 02:30:00     08/06/2014 02:00:00
63405,9900  91303320    63405,7200  08/06/2014 02:45:00     08/06/2014 02:00:00
63405,10800 91303380    63405,10800 08/06/2014 03:00:00     08/06/2014 03:00:00
63405,11700 91303380    63405,10800 08/06/2014 03:15:00     08/06/2014 03:00:00
63405,12600 91303380    63405,10800 08/06/2014 03:30:00     08/06/2014 03:00:00
63405,13500 91303380    63405,10800 08/06/2014 03:45:00     08/06/2014 03:00:00
63405,14400 91303440    63405,14400 08/06/2014 04:00:00     08/06/2014 04:00:00
63405,15300 91303440    63405,14400 08/06/2014 04:15:00     08/06/2014 04:00:00
63405,16200 91303440    63405,14400 08/06/2014 04:30:00     08/06/2014 04:00:00
63405,17100 91303440    63405,14400 08/06/2014 04:45:00     08/06/2014 04:00:00
63405,18000 91303500    63405,18000 08/06/2014 05:00:00     08/06/2014 05:00:00
63405,18900 91303500    63405,18000 08/06/2014 05:15:00     08/06/2014 05:00:00
63405,19800 91303500    63405,18000 08/06/2014 05:30:00     08/06/2014 05:00:00
63405,20700 91303500    63405,18000 08/06/2014 05:45:00     08/06/2014 05:00:00
63405,21600 91303560    63405,21600 08/06/2014 06:00:00     08/06/2014 06:00:00
63405,22500 91303560    63405,21600 08/06/2014 06:15:00     08/06/2014 06:00:00
63405,23400 91303560    63405,21600 08/06/2014 06:30:00     08/06/2014 06:00:00
63405,24300 91303560    63405,21600 08/06/2014 06:45:00     08/06/2014 06:00:00
63405,25200 91303620    63405,25200 08/06/2014 07:00:00     08/06/2014 07:00:00
63405,26100 91303620    63405,25200 08/06/2014 07:15:00     08/06/2014 07:00:00
63405,27000 91303620    63405,25200 08/06/2014 07:30:00     08/06/2014 07:00:00
63405,27900 91303620    63405,25200 08/06/2014 07:45:00     08/06/2014 07:00:00
63405,28800 91303680    63405,28800 08/06/2014 08:00:00     08/06/2014 08:00:00
63405,29700 91303680    63405,28800 08/06/2014 08:15:00     08/06/2014 08:00:00
63405,30600 91303680    63405,28800 08/06/2014 08:30:00     08/06/2014 08:00:00
63405,31500 91303680    63405,28800 08/06/2014 08:45:00     08/06/2014 08:00:00
63405,32400 91303740    63405,32400 08/06/2014 09:00:00     08/06/2014 09:00:00
63405,33300 91303740    63405,32400 08/06/2014 09:15:00     08/06/2014 09:00:00
63405,34200 91303740    63405,32400 08/06/2014 09:30:00     08/06/2014 09:00:00
63405,35100 91303740    63405,32400 08/06/2014 09:45:00     08/06/2014 09:00:00
63405,36000 91303800    63405,36000 08/06/2014 10:00:00     08/06/2014 10:00:00
63405,36900 91303800    63405,36000 08/06/2014 10:15:00     08/06/2014 10:00:00
63405,37800 91303800    63405,36000 08/06/2014 10:30:00     08/06/2014 10:00:00
63405,38700 91303800    63405,36000 08/06/2014 10:45:00     08/06/2014 10:00:00
63405,39600 91303860    63405,39600 08/06/2014 11:00:00     08/06/2014 11:00:00
63405,40500 91303860    63405,39600 08/06/2014 11:15:00     08/06/2014 11:00:00
63405,41400 91303860    63405,39600 08/06/2014 11:30:00     08/06/2014 11:00:00
63405,42300 91303860    63405,39600 08/06/2014 11:45:00     08/06/2014 11:00:00
63405,43200 91303920    63405,43200 08/06/2014 12:00:00     08/06/2014 12:00:00
63405,44100 91303920    63405,43200 08/06/2014 12:15:00     08/06/2014 12:00:00
63405,45000 91303920    63405,43200 08/06/2014 12:30:00     08/06/2014 12:00:00
63405,45900 91303920    63405,43200 08/06/2014 12:45:00     08/06/2014 12:00:00
63405,46800 91303980    63405,46800 08/06/2014 13:00:00     08/06/2014 13:00:00
63405,47700 91303980    63405,46800 08/06/2014 13:15:00     08/06/2014 13:00:00
63405,48600 91303980    63405,46800 08/06/2014 13:30:00     08/06/2014 13:00:00
63405,49500 91303980    63405,46800 08/06/2014 13:45:00     08/06/2014 13:00:00
63405,50400 91304040    63405,50400 08/06/2014 14:00:00     08/06/2014 14:00:00
63405,51300 91304040    63405,50400 08/06/2014 14:15:00     08/06/2014 14:00:00
63405,52200 91304040    63405,50400 08/06/2014 14:30:00     08/06/2014 14:00:00
63405,53100 91304040    63405,50400 08/06/2014 14:45:00     08/06/2014 14:00:00
63405,54000 91304100    63405,54000 08/06/2014 15:00:00     08/06/2014 15:00:00
63405,54900 91304100    63405,54000 08/06/2014 15:15:00     08/06/2014 15:00:00
63405,55800 91304100    63405,54000 08/06/2014 15:30:00     08/06/2014 15:00:00
63405,56700 91304100    63405,54000 08/06/2014 15:45:00     08/06/2014 15:00:00
63405,57600 91304160    63405,57600 08/06/2014 16:00:00     08/06/2014 16:00:00
63405,58500 91304160    63405,57600 08/06/2014 16:15:00     08/06/2014 16:00:00
63405,59400 91304160    63405,57600 08/06/2014 16:30:00     08/06/2014 16:00:00
63405,60300 91304160    63405,57600 08/06/2014 16:45:00     08/06/2014 16:00:00
63405,61200 91304220    63405,61200 08/06/2014 17:00:00     08/06/2014 17:00:00
63405,62100 91304220    63405,61200 08/06/2014 17:15:00     08/06/2014 17:00:00
63405,63000 91304220    63405,61200 08/06/2014 17:30:00     08/06/2014 17:00:00
63405,63900 91304220    63405,61200 08/06/2014 17:45:00     08/06/2014 17:00:00
63405,64800 91304280    63405,64800 08/06/2014 18:00:00     08/06/2014 18:00:00
63405,65700 91304280    63405,64800 08/06/2014 18:15:00     08/06/2014 18:00:00
63405,66600 91304280    63405,64800 08/06/2014 18:30:00     08/06/2014 18:00:00
63405,67500 91304280    63405,64800 08/06/2014 18:45:00     08/06/2014 18:00:00
63405,68400 91304340    63405,68400 08/06/2014 19:00:00     08/06/2014 19:00:00
63405,69300 91304340    63405,68400 08/06/2014 19:15:00     08/06/2014 19:00:00
63405,70200 91304340    63405,68400 08/06/2014 19:30:00     08/06/2014 19:00:00
63405,71100 91304340    63405,68400 08/06/2014 19:45:00     08/06/2014 19:00:00
63405,72000 91304400    63405,72000 08/06/2014 20:00:00     08/06/2014 20:00:00
63405,72900 91304400    63405,72000 08/06/2014 20:15:00     08/06/2014 20:00:00
63405,73800 91304400    63405,72000 08/06/2014 20:30:00     08/06/2014 20:00:00
63405,74700 91304400    63405,72000 08/06/2014 20:45:00     08/06/2014 20:00:00
63405,75600 91304460    63405,75600 08/06/2014 21:00:00     08/06/2014 21:00:00
63405,76500 91304460    63405,75600 08/06/2014 21:15:00     08/06/2014 21:00:00
63405,77400 91304460    63405,75600 08/06/2014 21:30:00     08/06/2014 21:00:00
63405,78300 91304460    63405,75600 08/06/2014 21:45:00     08/06/2014 21:00:00
63405,79200 91304520    63405,79200 08/06/2014 22:00:00     08/06/2014 22:00:00
63405,80100 91304520    63405,79200 08/06/2014 22:15:00     08/06/2014 22:00:00
63405,81000 91304520    63405,79200 08/06/2014 22:30:00     08/06/2014 22:00:00
63405,81900 91304520    63405,79200 08/06/2014 22:45:00     08/06/2014 22:00:00
63405,82800 91304580    63405,82800 08/06/2014 23:00:00     08/06/2014 23:00:00
63405,83700 91304580    63405,82800 08/06/2014 23:15:00     08/06/2014 23:00:00
63405,84600 91304580    63405,82800 08/06/2014 23:30:00     08/06/2014 23:00:00
63405,85500 91304580    63405,82800 08/06/2014 23:45:00     08/06/2014 23:00:00

Test Code:

ZPERIOD ;
S NOW=$H
S DAY=$P($H,",",1)
F TIME=0:900:86399 D
. S NOW=DAY_","_TIME
. S TPERIOD=$$TPERIOD(NOW)
. S HPERIOD=$$HPERIOD(TPERIOD)
. W !,NOW,?12,TPERIOD,?24,HPERIOD,?36,$ZDT(NOW),?60,$ZDT(HPERIOD)
QUIT
TPERIOD(NOW)
N TEMP
S TEMP=($P(NOW,",",1)*1440)+($P(NOW,",",2)\60)
S TEMP=TEMP\60*60
QUIT TEMP
HPERIOD(PERIOD)
Q PERIOD\1440_","_(PERIOD#1440*60)

Micky Hulse

unread,
Aug 6, 2014, 10:21:59 PM8/6/14
to intersystems. public. cache
On Wed, Aug 6, 2014 at 6:41 PM, OldMster <sires...@gmail.com> wrote:
> PERIOD\1440_","_(PERIOD#1440*60)

DOH! it was my bad … After having looked at your new code example in
more detail, I realized that I put parens around the modulo operator.
For example (bad code):

set period = (period \ 1440) _ "," _ (period # (1440 * 60))

I'm a paren fanatic. In this case, I assumed the 1440 * 60 needed to
be grouped before applying the modulous (noob mistake, I know). :D

This is how it should be:

set period = (period \ 1440) _ "," _ ((period # 1440) * 60)

Anyway, I dropped the added parens (exactly like your original code
example) and it works exactly like your example output.

Thanks billion Mark! I really appreciate you taking the time to help me out.

Have a great day!

Cheers,
Micky

--
<git.io/micky>

OldMster

unread,
Aug 7, 2014, 9:21:02 AM8/7/14
to intersystems...@googlegroups.com
No problem Mickey, glad it helped!

Incidentally, this is a variation on a common pattern used in Mumps.  If you need to tell how much time has elapsed between timepoints, you can do this:

Set Time1=$H
Set Time1=($p(Time1,",",1)*86400)+$p(Time1,",",2)
H 15 ;for example - just to put some time in here
Set Time2=$H
Set Time2=($p(Time2,",",2)*86400)+$p(Time2,",",2)
;now Time1 is the number of seconds since 12/31/1840 at exactly midnight, Time2 is the same

To see how many seconds between Time2 and Time1, just subtract ( Set Elapsed=Time2-Time1)
To see how many days between Time2 and Time1 ( Set Days=(Time2-Time1)\86400 )

It is useful, particularly if evaluating amount of time that crosses a day boundary - no messy if statements to see if the first piece of $H changed.

Mark

Ed de Moel

unread,
Aug 7, 2014, 9:37:56 AM8/7/14
to intersystems...@googlegroups.com
In an earlier response to this message-chain I noticed that both $Horolog
and $ZTimeStamp were being used.

While these two look very much alike, be aware that $Horolog reflects "local" time
and $ZTimeStamp reflects UCT (also known as GMT).
For me this means that there is a difference of 4 hours between those two, i.e.
in Greenwich Mean Time it is 4 hours later than in Boston's Easter Daylight Saving Time:

USER>s h=$Horolog,z=$ZTimeStamp w !,h,!,z,!,$p(z,",",2)-$p(h,",",2)\3600
 
63406,34592
63406,48992.915
4
USER>



--
**************************************************************
This electronic mail transmission contains confidential and/or
privileged information intended only for the person(s) named.
Any use, distribution, copying or disclosure by another person
is strictly prohibited.
**************************************************************

OldMster

unread,
Aug 7, 2014, 10:09:53 AM8/7/14
to intersystems...@googlegroups.com
Ed,
That is also good information, I sometimes use that to see what the current time offset is when I need to know it.
I don't see $ZTimestamp in the previous messages, at least on google groups view.  $ZDateTime and $ZDateTimeH are there.
Mark

Micky Hulse

unread,
Aug 7, 2014, 2:43:32 PM8/7/14
to intersystems. public. cache
On Thu, Aug 7, 2014 at 6:37 AM, Ed de Moel <dem...@jacquardsystems.com> wrote:
> While these two look very much alike, be aware that $Horolog reflects
> "local" time
> and $ZTimeStamp reflects UCT (also known as GMT).
> For me this means that there is a difference of 4 hours between those two,
> i.e.
> in Greenwich Mean Time it is 4 hours later than in Boston's Easter Daylight
> Saving Time:

Thanks for clarification and tip/code Ed! I was wondering if I should
be worried about timezones. In this case, the code Mark gave me
appears to be giving me the proper time for my server's location.
Still though, I'm glad you mentioned, as the clarification helps me
learn the subtleties of Caché date/time functions.

I really appreciate the help folks! As always, ya'll are life savers. :)

Micky Hulse

unread,
Aug 7, 2014, 2:47:00 PM8/7/14
to intersystems. public. cache
Hi Mark!

On Thu, Aug 7, 2014 at 6:21 AM, OldMster <sires...@gmail.com> wrote:
> Incidentally, this is a variation on a common pattern used in Mumps. If you
> need to tell how much time has elapsed between timepoints, you can do this:
> … snip…
> It is useful, particularly if evaluating amount of time that crosses a day
> boundary - no messy if statements to see if the first piece of $H changed.

That's interesting! Learn something new every day. :D

I really appreciate the follow up with details and more example code.
This has been a good learning experience. Again, I owe you one! :)

Have an excellent day!

Cheers,
Micky

--
<git.io/micky>
Reply all
Reply to author
Forward
0 new messages