You are not logged in.

#1 2013-01-09 09:21:47

ufizo
Member
Registered: 2012-04-25
Posts: 8

[SOLVED] Syncing time with android device.

Hello! I hope I am not posting this at the wrong place.
My laptop's CMOS battery has given up, and the time-date are reset at each reboot. 
I currently use timedatectl set-time "<date time>" to set the clock at each boot, but it is too much hassle. So I thought of syncing my clocks with my android devices.

Here is the command I tried: (adb already initialized)

sudo timedatectl set-time "$(adb shell date +"%F %T")"

It gives me the following error:
Failed to parse time specification: 2013-01-09 14:46:42

What really got me confused is that fact that the commands:

sudo timedatectl set-time "$(date +"%F %T")"

and

sudo timedatectl set-time "$(adb shell date +"%F") 14:30:00"

Works perfectly as intended,  but are of no use of course. What could be causing the trouble?

Can someone point some otherway of scripting to sync my clocks at boot, maybe from some other source of time perhaps? I am aware of NTP, but Internet connectivity at boot is not guaranteed, whereas I almost always have my android device with me.

Last edited by ufizo (2013-01-09 15:45:33)

Offline

#2 2013-01-09 09:26:25

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: [SOLVED] Syncing time with android device.

ufizo wrote:

I am aware of NTP, but Internet connectivity at boot is not guaranteed

It doesn't have to be. Just set up NTP to run whenever you connect.

Offline

#3 2013-01-09 09:27:34

ufizo
Member
Registered: 2012-04-25
Posts: 8

Re: [SOLVED] Syncing time with android device.

I understand that, but I rather have to stay offline for long periods.

Offline

#4 2013-01-09 09:34:47

toad
Member
From: if only I knew
Registered: 2008-12-22
Posts: 1,775
Website

Re: [SOLVED] Syncing time with android device.

Different approach - new CMOS battery?


never trust a toad...
::Grateful ArchDonor::
::Grateful Wikipedia Donor::

Offline

#5 2013-01-09 13:46:40

ufizo
Member
Registered: 2012-04-25
Posts: 8

Re: [SOLVED] Syncing time with android device.

toad wrote:

Different approach - new CMOS battery?

I'll buy a new laptop soon for that matter. But for curiosity's sake I want to find out whats wrong with my approach.

Offline

#6 2013-01-09 13:58:17

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: [SOLVED] Syncing time with android device.

Break it apart for troubleshooting:

VAR="$(adb shell date +"%F %T")"
echo $VAR
sudo timedatectl set-time "$VAR"

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2013-01-09 14:05:46

ufizo
Member
Registered: 2012-04-25
Posts: 8

Re: [SOLVED] Syncing time with android device.

Trilby wrote:

Break it apart for troubleshooting:

VAR="$(adb shell date +"%F %T")"
echo $VAR
sudo timedatectl set-time "$VAR"
echo $VAR

gives:

2013-01-09 19:31:41

and

sudo timedatectl set-time "$VAR"

still gives
Failed to parse time specification: 2013-01-09 19:31:41

Interestingly,
If I set another variable as:

VAR1="2013-01-09 19:31:41"

it works.

echo $VAR1 && echo $VAR

gives:

2013-01-09 19:31:41
2013-01-09 19:31:41

Last edited by ufizo (2013-01-09 14:06:37)

Offline

#8 2013-01-09 14:14:58

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Syncing time with android device.

What do you get if you do this:

date +"%F %T" -d "$VAR"

"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#9 2013-01-09 14:29:32

ufizo
Member
Registered: 2012-04-25
Posts: 8

Re: [SOLVED] Syncing time with android device.

2013-01-09 19:31:41

Offline

#10 2013-01-09 15:09:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: [SOLVED] Syncing time with android device.

There's probably some sort of newline, EOF, or non-printing character at the end of what is returned from adb.

Try changing the echo to the following to see if the brackets show up right at the beginning and end of the date:

echo "[$VAR]"

And/or you could use the bash string manipulation to remove the tail end of the string - I don't remember the propper invocation of the top of my head, but it is easily looked up.  Or yet as another alternative, you could "sed" out any unexpected characters:

VAR="$(adb shell date +"%F %T" | sed 's/[^0-9: -]//g )"

Last edited by Trilby (2013-01-09 15:12:29)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2013-01-09 15:19:27

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: [SOLVED] Syncing time with android device.

Since the length of the timedate string is fixed, you could always take the first 19 characters: ${string:0:19}


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#12 2013-01-09 15:23:32

ufizo
Member
Registered: 2012-04-25
Posts: 8

Re: [SOLVED] Syncing time with android device.

Trilby wrote:

There's probably some sort of newline, EOF, or non-printing character at the end of what is returned from adb.

Try changing the echo to the following to see if the brackets show up right at the beginning and end of the date:

echo "[$VAR]"

And/or you could use the bash string manipulation to remove the tail end of the string - I don't remember the propper invocation of the top of my head, but it is easily looked up.  Or yet as another alternative, you could "sed" out any unexpected characters:

VAR="$(adb shell date +"%F %T" | sed 's/[^0-9: -]//g )"

Yes, that was the issue. Removing the tail end fixed the issue. Thanks.

progandy wrote:

Since the length of the timedate string is fixed, you could always take the first 19 characters: ${string:0:19}

That also gets it done. Thank you.

Offline

#13 2013-01-09 15:39:23

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: [SOLVED] Syncing time with android device.

*oops* yes, just taking the first 19 characters is much easier, and more efficient.  Stick with that for the long term solution.

If this is now working, please mark the thread as solved by editting your initial post to prepend "[SOLVED]" to the title.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#14 2013-01-09 16:15:07

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Syncing time with android device.

ufizo wrote:

2013-01-09 19:31:41

Sorry, I had to go out. My suggestion was to put the local date call in the mix:

sudo timedatectl -set-time "$(date -d "$(adb shell date +"F% T%")" +"F% T%")"

Can't test as don't have adb - if the quoting can't be worked out then having it in two steps with a variable was another suggestion.
I did also think there as an odd non-printing character in there, and thought this would get rid of it whatever and wherever it was. It's probably no better and possibly worse than the suggestions above.
Anyway, just adding it for the record. smile

Last edited by skanky (2013-01-09 16:15:46)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB