Update system time using ntp and NetworkManager (Gentoo)

On my laptop I use NetworkManager to control network interfaces (LAN/WLAN).

Here is a simple script that will sync your clock with Internet time when LAN or WLAN interface is up and stop ntp-client when the interface is down. Put the script in /etc/NetworkManager/dispatcher.d and make it executable (chmod +x ).

#!/bin/bash
IF=$1;
STATE=$2;
 
if [ "$IF" = "eth0" ] || [ "$IF" = "wlan0" ] && [ "$STATE" = "up" ]; then
        /etc/init.d/ntp-client start
else
        /etc/init.d/ntp-client stop
fi

In order for the script to work you need net-misc/ntp package.