Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Programming
 PowerHome Messageboard : PowerHome Programming
Subject Topic: DateTime functions Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 01 2013 at 13:46 | IP Logged Quote GadgetGuy

Having seen a number of questions over the years on the
forum about how to make DateTime functions work, I have
started writing a brief tutorial on their "care and
feeding" to help others understand, but in doing so have
run into a mystery myself.

The PH function "Today()" returns a value like...
09/26/2013 18:38:15:306

The function ph_relativedatetime(Today(),90) returns a
date/time value that is 90 minutes after the function is
executed, since Today() is equal to the moment the
function is run.

But ph_relativedatetime(12/01/2013 13:38:15:306,90)
returns a =SYNTAX ERROR=

The datetime value in the last example should be equal to
the example before it.

Unfortunately the forum only seems to contain examples
where Today() was used rather than a direct datetime
string.

Thus the question. Does ph_relativedatetime only work if
today() is used, or should it work with a fixed datetime
expression, or is this a bug?



Edited by GadgetGuy - December 01 2013 at 13:46


__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 01 2013 at 14:12 | IP Logged Quote GadgetGuy

OK.

As sometimes happens, I can answer my own question.

[Well actually I got it wrong though. See Dave's answer
below. ]

This works...
ph_relativedatetime(datetime("09/23/13 12:00:00"),30)

When a "datetime" value is required there is no way to
directly enter a value as it is an internal "system"
number of clock ticks that must be computed. Thus the
need to use the datetime() function.




Edited by GadgetGuy - December 02 2013 at 06:11


__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: December 01 2013 at 19:16 | IP Logged Quote dhoward

Just to clarify a little further...

You can specify a literal date, time, or datetime by using the format of yyyy-mm-dd hh:mm:ss.fff

So for your example above, changing it to:

ph_relativedatetime(2013-12-01 13:38:15.306,90)

You'll get back the expected results...almost. The milliseconds portion will always be 0 as the ph_relativedatetime function truncates this value. If you want millisecond precision, you must use the ph_relativedtprecise function.

Now you may be asking why the function today() returns its output in mm/dd/yyyy hh:mm:ss format rather than the literal datetime input and its due to Windows formatting of datetime values and can be controlled by regional settings. So input is always yyyy-mm-dd hh:mm:ss.fff and output can vary (usually mm/dd/yyyy hh:mm:ss in the United States) based upon Windows regional settings.

Hope this helps,

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 02 2013 at 06:10 | IP Logged Quote GadgetGuy

Dave-

Yesterday I tried your suggestion as one of the first
things, looking for alternatives, and could not get it to
work.

I tried slashes, dashes, commas, and blanks in about 20
different permutations/combinations and finally decided
direct datetime entry just wasn't possible.

Copy/pasted your example from above this morning, and was
blown away when it worked!

Took me several minutes to finally figure out that the PH
datetime structure is year/month/day rather than
month/day/year.

Thanks for the feedback. I just learned something!


__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: December 08 2013 at 09:54 | IP Logged Quote gg102

If I may chime in on a similar, but not exact issue. I would love to have a date/time subtraction function. This would be used for situations like "The garage door has been open for xx minutes." or "Mike has been away for xx hours, yy minutes zz seconds".

Unless I'm missing something, the problem with the available functions is that they don't handle crossing a day boundary and using them is very clumsy for this purpose.

I can store now() when the garage door opens, and would love to subtract now() from that value when it closes.

Just something I'd like to have.

If anyone knows a way to do this, I'm all ears.
Back to Top View gg102's Profile Search for other posts by gg102
 
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 08 2013 at 10:34 | IP Logged Quote GadgetGuy

gg102-

Good idea, and as you have determined there is currently no way to do math on datetime values, other than "seconds after", etc(At least I haven't discovered such capabilities either.) [:-}

__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: December 08 2013 at 11:52 | IP Logged Quote gg102

What I figured out to write to my log file was the following:

ph_writefile("{LOG_FILE_NAME}",0, today() + " Garage Door has closed. It has been open for: "+
string(int(int(secondsafter( time(mid("{GARAGE_DOOR_OPEN_SINCE}",11,9)),now()
)/60)/60)) +" Hours, " +
string(int(int(secondsafter( time(mid("{GARAGE_DOOR_OPEN_SINCE}",11,9)),now() )/60))-
(int(int(secondsafter( time(mid("{GARAGE_DOOR_OPEN_SINCE}",11,9)),now()
)/60)/60)*60 ))+ " Minutes, since: {GARAGE_DOOR_OPEN_SINCE}." + char(13) + char(10) )

Good luck in trying to figure it out.......
Thus, it sure would be nice to have a date/time subtract function.
Back to Top View gg102's Profile Search for other posts by gg102
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: December 08 2013 at 12:00 | IP Logged Quote dhoward

Concerning date subtraction, probably the best function for this is the ph_minutesafter function. It will take two datetime values and give you the difference in minutes (with decimal so you can also extract seconds). It will handle crossing date boundaries and if the first parm is greater than the second parm, you'll get a negative value.

In the meantime, I'll create a new function that allows you to extract hours, minutes, etc. from minutes.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: December 08 2013 at 12:08 | IP Logged Quote gg102

Thank you Dave.

Oh, Just to be sure:
I want to take a now(), save it, and later subtract it from now() and get a date/time. (duration)


Edited by gg102 - December 08 2013 at 12:14
Back to Top View gg102's Profile Search for other posts by gg102
 
dhoward
Admin Group
Admin Group
Avatar

Joined: June 29 2001
Location: United States
Online Status: Offline
Posts: 4447
Posted: December 08 2013 at 13:52 | IP Logged Quote dhoward

I took a stab at your formula and came up with:

ph_writefile("{LOG_FILE_NAME}",0,ph_rtne(ph_setvar_a(1,1,ph_ minutesafter(ph_getglobal_dt("GARAGE_DOOR_OPEN_SINCE"),today ()))) + string(today()) + " Garage Door has closed. It has been open for: " + string(ph_getvar_n(1,1) / 60,"#") + " Hours, " + string(ph_getvar_n(1,1) - truncate(ph_getvar_n(1,1) / 60,0) * 60,"#") + " Minutes, since:" + ph_getglobal_s("GARAGE_DOOR_OPEN_SINCE") + "~r~n")

Not sure if its really any easier or shorter but may lend some ideas until the new formula is available.

Dave.
Back to Top View dhoward's Profile Search for other posts by dhoward Visit dhoward's Homepage
 
gg102
Senior Member
Senior Member


Joined: January 29 2013
Location: United States
Online Status: Offline
Posts: 245
Posted: December 08 2013 at 17:45 | IP Logged Quote gg102

Oh my, <SMILES>

Interesting twist, but with all respect Dave, it's just as obtuse, maybe a wee bit more.

Although, I think your routine will generate less heat in the CPU than mine. LOL

Thanks for the effort tho.....

I'll keep that one in the file for reference for now.

I think you'll agree now, it would be SO MUCH easier just to have a date/time subtraction (duration) function.
Back to Top View gg102's Profile Search for other posts by gg102
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum