A fast one for all of you who don’t want to reinvent the wheel all the time, licensed under the [no-problem-bugger-off-license]. Print timestamps in C or Python, for debugging by print statements, and for when a logging library is just too much.

In C:

/*********************************************************************
 * Filename:      timestamp.c
 * Version:       $Header: $
 * Description:   A routine to print the current time in a
 *                human-readable format.
 * Author:        Michael Hinz <michael@hinz.dyndns.org>
 * Created at:    Sun Mar 19 21:58:08 2000
 * Modified at:   Fri Mar 14 12:26:48 2008
 * Modified by:   Michael Hinz <michael@hinz.dyndns.org>
 * Language:      C
 * Status:        Should work.
 * Update count:  3
 * Copyright:     Copyright (c) 2000-2008 by Michael Hinz
 ********************************************************************/
  
#include <stdio.h>
#include <time.h>
  
void timestamp(FILE *out)
{
  struct tm *tmp;
  time_t t;
  
  t = time(NULL);
  tmp = localtime(&t); /* or gmtime, if you want GMT^H^H^HUTC */
  fprintf(out, "%02d.%02d.%04d %02d:%02d:%02d", 
     tmp->tm_mday, tmp->tm_mon+1, tmp->tm_year+1900,
     tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
}

/* example of how to call this function: */
  
int main()
{
  printf("The current date and time are: ");
  timestamp(stdout);
  printf("\n");
  return 0;
}

and in Python:

""" ------------------------------------------------------------------
    Copyright (C) 2000 by Michael Hinz
    Filename:      timestamp.py
    Version:       $Header: $
    Description:   
    Author:        Michael Hinz <michael@hinz.dyndns.org>
    Created at:    Sat Apr  1 00:21:15 2000
    Modified at:   Sat Apr  1 00:24:22 2000
    Modified by:   Michael Hinz <michael@hinz.dyndns.org>
    Language:      Python
    Status:        Experimental, do not distribute.
    Update count:  5
   ---------------------------------------------------------------
"""
  
import time
  
def timestamp():
    lt = time.localtime(time.time())
    return "%02d.%02d.%04d %02d:%02d:%02d" % (lt[2], lt[1], lt[0], lt[3], lt[4], lt[5])
  
if __name__ == '__main__':
    print("time now:", timestamp())

There you go.

Btw. if you use my email address from 2000 I’m genuinely impressed. It will not work though.

The Python code was written for Python 1.x since 2.0 came out in october 2000 and this code was written before that. The date is not a joke. And all I had to fix at the last revision (july 2022) was to add parentheses to the print statement, since Python 3.x requires that now.

The boilerplate code can be found on my github, in case you are interested… and still use Emacs.