Message Boards Message Boards

Import the current date and time into a CDF?

Posted 5 years ago

Hi everyone,

I need to create a CDF with an expiry timestamp, that is, one that will show certain content before the expiry date and something else afterwards. I don't want to get the timestamp from the user's system because the users are my students and the sneaky rascals can, of course, reset their system dates and times to continue using the file. I had thought that maybe importing the most recent price of a stock, such as

FinancialData["GE", "Aug. 21, 2019", Method -> "Legacy"]

which gives me, as I write this,

({2019,8,21}    8.16)

would do the trick, but was wondering if there is a better way. Any suggestions would be much appreciated.

Gregory

POSTED BY: Gregory Lypny
8 Replies
Posted 4 years ago

Ah, another clever approach! Thank you Hans. I'll play with this.

Greg

POSTED BY: Gregory Lypny

Greg:

If the rascals turn off internet access or simply don't have one (TNFOTO). You can try the following.

Manipulate[
 Dynamic[TimeObject[{0, 0, SessionTime[]}], UpdateInterval -> 1], 
 SynchronousUpdating -> True, SaveDefinitions -> False]

Try as a docked cell in you cdf.

docked cell SessionTime

Tested the docked cell cdf on Windows and Mac; upon opening it starts at 0 seconds and increments. If I change the OS(s) system clock, while the cdf file is opened, the panel stops updating. So, with a few more computations you can test when the TimeObject[] is static and do something. If the TimeObject[] based on the SessionTime[] reaches some criteria say two hours you can also do something.

I can't readily think of a way to save the state such that upon reopening cdf file that the session starts at last time used.

Creating Docked Cell

SetOptions[EvaluationNotebook[], 
  DockedCells -> {Cell[BoxData[ToBoxes[Row[{
Manipulate[
 Dynamic[TimeObject[{0, 0, SessionTime[]}], UpdateInterval -> 1], 
 SynchronousUpdating -> True, SaveDefinitions -> False]
         }
        ]]], "DockedCell"]}];
POSTED BY: Hans Michel
Posted 4 years ago

Hello J.M.,

Thanks for this. I'm afraid it kept giving me errors, so what I did instead was to wrap by URLRead call in Quiet and Check, allowing me to trap errors, one of which would be the user not being connected to the Internet.

Regards,

Greg

POSTED BY: Gregory Lypny
Posted 4 years ago

Hi Michel,

Finally got it working. Thanks.

What if the student is not connected to the Internet, and therefore URLRead can't get the time. Is there a way to check whether the user is connected?

Greg

POSTED BY: Gregory Lypny
Posted 4 years ago

Perhaps something along the lines of

TrueQ[PacletManager`$AllowInternet] &&
FreeQ[PingTime["www.wolfram.com"], $TimedOut | $Failed]

can be used to check for connectivity.

POSTED BY: J. M.

Greg:

I did not have this issue with the above code saved as a FreeCDF file using CDFPreview. At each opening of the CDF file the data was updated.

The following is tangential to your immediate issue you wish solved.

DynamicModule[{t = None}, 
 Column[{Dynamic[
    ActionMenu[
     "Update Time:", {"On" :> (t = (UpdateInterval -> 1);), 
      "Off" :> (t = None;)}, Method -> "Queued"]], 
   Dynamic[Refresh[
     Row[{ClockGauge[AbsoluteTime[TimeZone -> 0], 
        PlotLabel -> Style["Student", Large, Bold]], 
       ClockGauge[
        AbsoluteTime[
         DateList[
          Lookup[Association[
            Values[URLRead[
               HTTPRequest["http://www.google.com"]][{"Headers"}]]], 
           "date"]]], PlotLabel -> Style["Server", Large, Bold]]}], 
     t]]}, Alignment -> Center]]

enter image description here

Now this worked each time I open the cdf file. It is a bit slow upon opening the first time. But it updates. The external URL is different from the above. But HTTPRequest seems to work in a CDF file. I am not using CDF Player. Other ideas are DockedCells, Using CloudDeploy or APIFunction to deploy an API call that returns a current timestamp. There are examples in the documentation.

POSTED BY: Hans Michel
Posted 5 years ago

Hey Hans, Sorry for the late reply. Thanks for this. It's clever. In tinkering with it, however, I've found that the initialization doesn't pick up the current date and time. It works one time, but not when I relaunch the CDF. Could it be that CDFs are not "allowed" to import from URLs? Greg

POSTED BY: Gregory Lypny

Hi Greg:

Maybe you can just prevent the sneaky rascals from changing local time.

Manipulate[{}, Panel[externaldate - DateList[TimeZone -> 0]], 
 Initialization :> (externaldate = 
    DateList[
     Lookup[Association[
       Values[URLRead[
          HTTPRequest[
           "http://explore.concordia.ca/gregory-lypny"]][{"Headers"}]]\
], "date"]])
 , SynchronousUpdating -> True, SaveDefinitions -> False
 ]

What I can think of off the cuff. Use your own (University provided) site's HTTP response header to send back the time. If user's local time is with in a tolerance than you can trust their local time

POSTED BY: Hans Michel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract