7
$\begingroup$

I have a notebook with lots of In[n] in my current session. The notebook has been open for over three days. I would like to get the timestamps for when certain old inputs--say In[50] and In[100]--were executed.

Is there any way to extract such metainformation from a notebook?

$\endgroup$
  • 2
    $\begingroup$ Cell > Notebook History perhaps? $\endgroup$ – AccidentalFourierTransform May 31 '18 at 17:53
  • 3
    $\begingroup$ Try Map[Options[#, CellChangeTimes] &, Cells[EvaluationNotebook[]]] If Notebook history was turned on. Also if history is on you can also go to a cell and Ctrl+Shift+E to reveal a full form like of the cell and Ctrl+Shift+E again to return to standard display $\endgroup$ – Hans May 31 '18 at 17:57
9
$\begingroup$

Update

It turns out that there is a much simpler method than my previous answer. If you need the actual cell label value, than I think messing around with the CellLabelAutoDelete option is necessary. On the other hand, if you know what the CellLabel is, then Cells is perfectly capable of returning the corresponding Cell. So, the following is a simpler version:

cellTimes[n_Integer?Positive] := gmtAbsoluteTimeToString @ Max @ CurrentValue[
    Cells[CellLabel -> "In["<>ToString[n]<>"]:="],
    CellChangeTimes
]

gmtAbsoluteTimeToString[time_] := DateString @ TimeZoneConvert[
    DateObject[time,TimeZone->0]
]

Original answer

You can make use of the Cells function to do this, although it's a bit difficult to extract the CellLabel. With the default CellLabelAutoDelete -> True, Cells will not return the CellLabel of a cell. The following function changes the option setting for CellLabelAutoDelete, and then looks for the time stamp of the given input cell:

cellTimes[n_Integer?Positive] := With[{cells = Cells[CellStyle->"Input"]},
    Internal`WithLocalSettings[
        CurrentValue[EvaluationNotebook[], CellLabelAutoDelete] = False,
        gmtAbsoluteTimeToString @ Max @ CurrentValue[
            Pick[
                cells,
                CurrentValue[cells, CellLabel],
                "In["<>ToString[n]<>"]:="
            ],
            CellChangeTimes
        ],
        CurrentValue[EvaluationNotebook[], CellLabelAutoDelete] = Inherited
    ]
]

gmtAbsoluteTimeToString[time_] := DateString @ TimeZoneConvert[
    DateObject[time,TimeZone->0]
]

For example:

DateString[]

"Thu 31 May 2018 11:13:46"

Pause[10]
cellTimes[$Line - 2]

"Thu 31 May 2018 11:13:46"

$\endgroup$
  • 1
    $\begingroup$ @anderstood Yes, I forgot to correct for a time zone issue. $\endgroup$ – Carl Woll May 31 '18 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.