# How to transform dynamic data into static data

Maybe I asked this somewhere sometime ago.

Is there a way to change dynamic data into static data?

I have defined the macro

\def\date{\the\day.\the\month.\the\year}


With every run of TeX the result is the current date. My aim is, to change the data of a given date statically, in example, to have the data of "1.4.2019" stored, not to be changed to the current date by a new run of TeX.

I have to improve this question, it was not clear enough. I want to store a large number of dates, i.e. 2019.5.6, 2019.5.7, 2019.5.8 etc and the recently stored dates shall not be overwritten by the current date. Therefore, the solution can not be, to define \date statically, but to produce definitions like \date1 \date2 \date3....

The background is, that I have a very large table, of thousands of cells, where the date of a new entry or update should appear, similar to a "timestamp" in mysql-databases.

Solutions should be TeX only, not LaTeX.

Thanks

• \def\date{1.4.2019}? – Ulrike Fischer Apr 19 '19 at 22:44
• @UlrikeFischer where's the fun in that:-) – David Carlisle Apr 19 '19 at 22:49
• @Ulrike Fischer Not quite...I want to generate the date dynamically and transfrom it into static data. Means, that TeX generates the current date and stores it statically and unchangeable. – Matthias Borck-Elsner Apr 19 '19 at 22:52

\openin0=\jobname.dat
\ifeof0
\closein0
\immediate\openout0=\jobname.dat
\immediate\write0{\def\string\date{\the\day.\the\month.\the\year}}
\immediate\closeout0
\fi

\input\jobname.dat

date: \date

\bye


For the revised question it's essentially the same but you have to loop over all the dates so

if the .dat file is seeded with 4 dates, to make it interesting

\datecount=4
\def\dateiv {8.4.2019}
\def\dateiii{8.3.2019}
\def\dateii{8.2.2019}
\def\datei{6.1.2019}


and modify the tex to look like

\newcount\datecount

\openin0=\jobname.dat
\ifeof0
\else
\closein0
\input \jobname.dat
\fi

\immediate\openout0=\jobname.dat
\expandafter\gdef\csname date\romannumeral\datecount\endcsname{\the\day.\the\month.\the\year}
{\immediate\write0{\string\datecount=\the\datecount}
\loop
\immediate\write0{\string\def\string\date\romannumeral\datecount{\csname date\romannumeral\datecount\endcsname}}
\ifnum\datecount>1
\repeat
\immediate\closeout0
}

date: \datei -- \csname date\romannumeral\datecount\endcsname

\bye


then after the next run the dat file will look like

\datecount=5
\def\datev{6.5.2019}
\def\dateiv{8.4.2019}
\def\dateiii{8.3.2019}
\def\dateii{8.2.2019}
\def\datei{6.1.2019}


with typeset output

• Didn't work for me - my test folder contained already a \jobname.dat ;-(. – Ulrike Fischer Apr 19 '19 at 23:06
• @David Carlisle Wow, this was a fast response. Concerning the \jobname.files I wished to find another solution, there are to many... Can you think of a way to manage this with boxes? – Matthias Borck-Elsner Apr 19 '19 at 23:13
• @MatthiasBorck-Elsner clearly you need to write to a file somewhere, You don't need one file per item of data you can store all the state in a single file. – David Carlisle Apr 19 '19 at 23:19
• @David Carlisle LaTeX and Plain are great, I'm just a fan of the basics and the primitives. That's the fun in that :-) .. .for me. – Matthias Borck-Elsner Apr 19 '19 at 23:29
• @MatthiasBorck-Elsner yes but it makes some things difficult, I used read and write 0 in the above which may interact with some unknown other use of them, I would allocate some but that may run out of streams (there are only 16) and if you restrict to primitives not even plain, then there is no allocation mechanism, using an existing format provides a framework for these sort of constructs to exist , if you just use primitives then as shown above they work but you have to manage any interactions... – David Carlisle Apr 19 '19 at 23:32

Seems to be impossible, none of the answers worked. In any case it looks like I have to put in the data manually.

Sadly,Knuth hasn't thought of implementing the timestamp, so this is the end of the story...

• Could you be a bit more specific about why the proposed soluton didn't work for you? – siracusa Jun 21 '19 at 1:28
• Well, perhaps you can add more informations to your question, for example an exact example of the data to be added in exactly which form? Then we do not have to guess what you are wanting. Perhaps using an separate file is the solution for you ... – Mensch Jun 21 '19 at 1:33
• I want to store the values of \the\year.\the\month.\the\day\the\hour:\the\minute:\the\second of a specific moment and keep the data and write again \the\year.\the\month.\the\day\the\hour:\the\minute:\the\second of the next specific moment and keep these data too, without overwriting the first data... I do not know how to explain this clearer. Maybe it works with a comparison, but ok, EOF Story – Matthias Borck-Elsner Jun 21 '19 at 20:27
• @MatthiasBorck-Elsner That's exactly what David's answer does (beside the fact that you can't have seconds in TeX). On every compilation run it adds a new entry to the file which stores the current date but keeps all the existing entries. – siracusa Jun 25 '19 at 14:29

similar to a "timestamp" in mysql-databases

MySQL and other database software have persistence as one of their primary features: today you may execute a statement like INSERT INTO ... and its effect remains when you access the database days or months later. Underneath, this is usually achieved by writing to some files, but this detail is hidden from you, the user.

TeX is not a database; it is a program for typesetting. You have a .tex file, you run TeX on it, and then TeX forgets about the file; it retains nothing for the future. But that seems to be exactly what you want:

My aim is […] to have the data of "1.4.2019" stored […] I want to store a large number of dates […] that TeX generates the current date and stores it statically and unchangeable […] I want to store the values of […] of a specific moment and keep the data and …

This is not what TeX is designed for, but if you want persistence you can easily achieve it the same way databases do: namely, by writing to a file. You run TeX on a file, you have it compute the timestamp (or whatever) and write it to a file, and on later runs it can pick up the data from the file. This is what David Carlisle told you in the comments on his answer:

"store value statically" == "write it to a file" so this is just about the only possible answer.

If you don't want to write to a file then you're asking for the (essentially) impossible.

Sadly,Knuth hasn't thought of implementing the timestamp, so this is the end of the story...

This is not correct. The semantics of \the\day, \the\month, \the\year etc. in TeX are exactly the same as those of the CURRENT_TIMESTAMP() (etc) functions in MySQL:

• If TeX encounters \the\day on a certain date, then the value is that day's date.

• If MySQL encounters CURRENT_DATE() on a certain date, then the value is that day's date.

There is no difference. The difference is elsewhere: databases are designed to store data (by automatically writing to a file behind the scenes), in TeX you have to do it yourself by writing to a file yourself (which for some reason you don't seem to be satisfied with).

• I do not understand this. There is no \the\hour, \the\minute\the\second in TeX (???) Furthermore, if I write \date into a file,it will be in any case the current_date unless I have defined an incremental naming of \def\dateXX, as David suggested.This works,but it is not,what I am looking for. The only solution I can think of, is a comparison between the systems date and the date function in TeX. As I stated, it seems to be impossible. (not to give up..) – Matthias Borck-Elsner Jun 23 '19 at 0:51
• @MatthiasBorck-Elsner TeX has \time for the number of minutes past midnight, from which you can compute hour and minute. And yes if you write \day into a file it will be the current date, and this is exactly the same as in MySQL where if you ask for the automatically generated timestamp you get the timestamp at the time of insertion. There is no difference in this aspect. – ShreevatsaR Jun 23 '19 at 1:25
• All of this does not make sense if minutes and seconds are not accessable in TeX. – Matthias Borck-Elsner Jun 30 '19 at 20:20
• @MatthiasBorck-Elsner Minutes are, seconds aren't. (As mentioned.) Anyway that's only a question of the granularity of the timestamp (e.g. MySQL supports microsecond granularity but not nanosecond granularity, just as TeX natively offers minute granularity but not second granularity), and I don't see the difference as fundamental. – ShreevatsaR Jun 30 '19 at 21:07