Skip to content

Instantly share code, notes, and snippets.

@abythell
Created April 8, 2013 17:54
Show Gist options
  • Save abythell/5338916 to your computer and use it in GitHub Desktop.
Save abythell/5338916 to your computer and use it in GitHub Desktop.
Use gphoto2 to set the system time from a USB-attached camera. Handy for use on a Raspberry Pi which does not have a real-time clock or network connection for NTP.
#!/bin/bash
# Set system date using gphoto2 camera
#
# Make sure a root user is running this script
#
if [[ $EUID -ne 0 ]]; then
echo "Must run as root/sudo"
exit -1
fi
#
# Get unix epoch from camera. Note, gphoto2 does not support timezones
# or daylight savings. The local system's timezone will be used, assuming
# the camera and the system are in the same zone.
#
DATETIME=`gphoto2 --get-config datetime`
if [ $? -ne 0 ]; then
exit 1
fi
EPOCH=`echo "$DATETIME" | grep Current | awk '{print $2}'`
if [[ ! $EPOCH =~ [0-9]{10} ]]; then
echo "gphoto2 did not return a usable time."
exit 1
fi
#
# Set current time
#
date -s @$EPOCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment