26 September 2021

Sometimes a procedure change is behaviour based on the date or time:
* on christmas an engine could give a different value as output (example: christmas date discount ;) )
* an evaluation that consider time as a important parameter inside the formula (example: loan interest)

Using libfaketime is possible to test a different date or time, in a very handy way.
It could be applied for Linux/Unix and Mac systems.

For me two important behaviours are (there are a lot more, see lifaketime README file for further configurations):
* start at date: usefull if you want to start from a specific date time and go further. This option is only available from 0.9.8 version
* absolute date: usefull if it is required always have the same datetime

I normally prefer start at because faketime also effect application logs and it is difficult have a good troubleshooting in case of absolute date.
A minimal log comparison

Example log 'start at' date Example log 'absolute' date
2021-09-26 05:23:00.000 Starting
2021-09-26 05:23:00.001 Set up connection pool
2021-09-26 05:23:03.123 Extract data
2021-09-26 05:23:00.000 Starting
2021-09-26 05:23:00.000 Set up connection pool
2021-09-26 05:23:00.000 Extract data

docker-compose.yml

version: "3.7"
services:
  service-start-at:
    build:
      context: .
      dockerfile: ./Dockerfile
      target: start-at
    networks:
      - net-libfaketime

  service-absolute:
    build:
      context: .
      dockerfile: ./Dockerfile
      target: absolute
    networks:
      - net-libfaketime

networks:
  net-libfaketime:
    driver: bridge

Dockerfile

FROM ubuntu:21.10 as start-at
RUN apt update && apt install -y faketime

ENV LD_PRELOAD="/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1"
ENV FAKETIME_DONT_RESET=1
ENV FAKETIME_DONT_FAKE_MONOTONIC=1
ENV FAKETIME="@2021-09-26 05:30:00"
CMD ["/usr/bin/bash", "-c" , "while [ $SECONDS -lt 5 ]; do date; sleep 1; done"]

FROM ubuntu:21.10 as absolute
RUN apt update && apt install -y faketime

ENV LD_PRELOAD="/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1"
ENV FAKETIME_DONT_RESET=1
ENV FAKETIME_DONT_FAKE_MONOTONIC=1
ENV FAKETIME="2021-09-26 05:30:00"
CMD ["/usr/bin/bash", "-c" , "while [ $SECONDS -lt 5 ]; do date; sleep 1; done"]

Example of usage:

docker-compose up --build

You can see from the terminal output the two different behaviours :)

service-absolute_1  | Sun Sep 26 05:30:00 UTC 2021
service-start-at_1  | Sun Sep 26 05:30:00 UTC 2021
service-start-at_1  | Sun Sep 26 05:30:01 UTC 2021
service-absolute_1  | Sun Sep 26 05:30:00 UTC 2021
service-start-at_1  | Sun Sep 26 05:30:02 UTC 2021
service-absolute_1  | Sun Sep 26 05:30:00 UTC 2021
service-start-at_1  | Sun Sep 26 05:30:03 UTC 2021
service-absolute_1  | Sun Sep 26 05:30:00 UTC 2021
service-start-at_1  | Sun Sep 26 05:30:04 UTC 2021
service-absolute_1  | Sun Sep 26 05:30:00 UTC 2021

References