How to Calculate Days Since Unix Epoch (1/1/1970) for OpenLDAP’s shadowLastChange Using Linux Date Command


4 views

When working with OpenLDAP's password policies, the shadowLastChange attribute requires the number of days since January 1, 1970 (Unix epoch). Here's how to compute this value efficiently using standard Linux tools.

The simplest method uses the GNU date command with arithmetic expansion:

echo $(( $(date +%s) / 86400 ))

This command:

  • Gets current timestamp in seconds with date +%s
  • Divides by 86400 (seconds per day)
  • Performs integer arithmetic with $(( ))

For systems without GNU date (like BSD variants):

perl -e 'print int(time/(60*60*24)),"\n";'

Or with Python:

python -c 'import time; print(int(time.time()/86400))'

To update an account's password change date:

ldapmodify -x -D "cn=admin,dc=example,dc=com" -W <

The calculation uses UTC by default. For precise local time conversion:

echo $(( ($(date +%s) + $(date +%z) * 3600) / 86400 ))

This adjusts for your current UTC offset in hours (%z).

Check your calculation against known values:

# January 2, 1970 should be 1
date -d "1970-01-02" +%s | awk '{print int($1/86400)}'

In Unix-like systems, time is often measured as seconds since 1970-01-01 00:00:00 UTC (the Epoch). For OpenLDAP's shadow password system, the shadowLastChange attribute requires days since Epoch rather than seconds.

Here's the most efficient way to get the current days since Epoch:

echo $(( $(date +%s) / 86400 ))

Breakdown:

  • date +%s - Gets current time in seconds since Epoch
  • 86400 - Seconds per day (24*60*60)
  • $(( ... )) - Bash arithmetic expansion

Using AWK

awk 'BEGIN {print systime()/86400}'

Python One-liner

python -c 'import time; print(int(time.time()/86400))'

When updating an LDAP entry with ldapmodify:

dn: uid=user1,ou=People,dc=example,dc=com
changetype: modify
replace: shadowLastChange
shadowLastChange: $(echo $(( $(date +%s) / 86400 )))

For precise calculations across timezones, consider using UTC explicitly:

echo $(( $(TZ=UTC date +%s) / 86400 ))

To verify your calculation is correct, you can convert back to human-readable date:

date -d @$(( 18628 * 86400 ))