3

If I want to set the time I know I can use a command like this:

sudo date +%T -s '17:45:00'

And it will set the clock to 5:45pm in the system time zone. This means that if I have the system set to UTC this will be 17:45 UTC, but if the system is set to Eastern time then the same command would actually update the clock to be 12:45 UTC. Is there some way I can specify the timezone identifier along with the time, in the date -s command?

2
  • What version of date are you using? This doesn't match the documentation of date that I'm reading. And why are you doing this at all? – Michael Hampton Jan 26 '18 at 18:03
  • Looks like the version is 8.2.3. It's what came installed on Raspbian. – soapergem Jan 26 '18 at 20:37
2

After a bit of trial-and-error experimentation, I figured it out. To set just the time and to force it to be in UTC, I can do this:

sudo date +%T%Z -s "17:45:00UTC"

If I want to set the date and the time simultaneously, I can do this:

sudo date -s '2018-01-26T17:45:00Z'
1
  • It's all in the man page. Both -d and -s take a "date string" and its format is described (actually a very permissive input). You can use date -d as a dry run for date -s – xenoid Jan 26 '18 at 21:34
1

In most flavors of Linux (Particularly Redhat/Centos off the top of my head), do this:

# Delete the current time zone file
rm -f /etc/localtime

# Set it to the new value
ln -s /usr/share/zoneinfo/UTC /etc/localtime

I understand that this does not use the date command as you requested.

1

If the device you are updating the time on, does not have a connection to the internet. And you don't care about sub-seconds accuracy. You can use this on a computer with a known correct time to generate a bash command that sets the correct time using UTC timestamp.

echo "date +%s -s @$(date +%s)"

This will generate something like date +%s -s @1541377821 that you can copy and paste to the other computer.

1
  • Underrated answer, this saved me some keystrokes :) – pilkch Feb 8 '19 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.