-1

I have date and time input by user in the format DD-MM-YYYY HH:MM:SS. For example: 27-01-2019 12:47:00 PM IST

Please help me how to set the date, time and time zone of raspberry pi from its command line interface.

6
  • 2
    Possible duplicate of How to set date & time on Raspberry Pi through a GUI?
    – Milliways
    Jan 27, 2019 at 7:20
  • 1
    Why do you want to do this?
    – Milliways
    Jan 27, 2019 at 7:26
  • I need to take the date & time input from user in a GUI and set the date/time on raspberry pi without any access to internet. I have the saved the date time info in a text file. But, now the issue is how to set date/time of Pi from that data in the format mentioned above
    – Raghavan K
    Jan 27, 2019 at 7:53
  • Is your code written in Python?
    – CoderMike
    Jan 27, 2019 at 8:05
  • Yes. Python 3.4.0
    – Raghavan K
    Jan 27, 2019 at 8:18

2 Answers 2

2

A programming language agnostic way to do this would be to call the date --set="STRING" command to set the system clock and then hwclock --systohc --utc command to set the RTC. How you do this from your code depends on the language you use.

From the manual page for date it looks like you can drop your string as-is into the command but you might want to check it on the actual version of date shipped on your Pi:

Date String

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation.

0
1

Use the date utility.

If you really MUST you could build an application around this. Python has date, time and datetime modules.

Frankly you would be better to spend $2 on a RTC module. We used to do things like this in 1975!

4
  • The whole idea is that user enters date and time , which will be stored in the battery backed RTC through Pi's I2C interface. The same RTC will be accessed to get the date and time whenever Pi boots up. Can u explain a bit about ur idea of using Python time modules here?
    – Raghavan K
    Jan 27, 2019 at 11:59
  • @RaghavanK You were the one who raised Python. Setting time in a RTC is a trivial procedure using hwclock. You would have been better to have asked your real question rather than posing an XY problem
    – Milliways
    Jan 27, 2019 at 12:04
  • You have not understood my question. Leave it if you cannot answer. But, u can't make fun of a question just because u think it is simple
    – Raghavan K
    Jan 27, 2019 at 12:25
  • Please calm down and be nice. Thank you both.
    – Ghanima
    Jan 27, 2019 at 13:46

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