9

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Shredderroy
  • 5,249
  • 17
  • 26

1 Answers1

11

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"

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 1
    @anderstood Yes, I forgot to correct for a time zone issue. – Carl Woll May 31 '18 at 18:15
  • Strictly speaking, CellChangeTimes contains times when the cell was edited, not when it was evaluated, unfortunately. So this method will give approximate execution times only under assumption that the cell was edited right before evaluation. – Alexey Popkov Oct 16 '19 at 13:23