yashsingh1/xdaforums huggingface dataset:
[G2 Themes and Apps] Doc Sniper:
I don't think that's possible, because the time drift is calculated while the OS is running with correct time and date set. And while you're flashing the zip you're in recovery mode and have a wrong time (with 1970) so your personal time drift can't be calculated.
[G2 Themes and Apps] Atze001:
A little help for you.

You can use Script Manager and copy my Script (save as) to your internal Memory.

Run the script with Scriptmanager (as SU) and thats all.
[G2 Themes and Apps] dougiebee:
I wrote a quick app to ease installation, I think this is about as straight forward as it could get. Requires root. As before, your ROM must have initscript support.

The app has 3 functions:

Get Offset (Get the Clock Time offset, so you can create your own flashable zip)

Install (Install the fix in /data/local/userinit.sh)

Uninstall (Remove /data/local/userinit.sh)

Can someone please test this? Works perfectly on my phone. I'll update the OP once I get confirmation from someone else.

To test, remove any existing fix, make sure the time is correct, install, reboot.

I'd like to add creation of a flashable zip, but that will have to wait till I have time.

Thanks go to sniperle and Atze001 for the idea.
[G2 Themes and Apps] ramonmaruko:
Tested this and it seemed to work. I encountered no more errors after rebooting.
[G2 Themes and Apps] Doc Sniper:
Official Bug Report for CM

I opened a bug report on the CyanogenMod issue tracker: CyanogenMod- CYAN-3075 - RTC write problem
[G2 Themes and Apps] -Zork-:
Tested on Stock rom with CMW of Philz, and didn´t work, still 1970 in recovery backup. :rolleyes:
[G2 Themes and Apps] DerSpatz:
It works only on android system level, not on recovery level...
[G2 Themes and Apps] dougiebee:
Version bump to 1.1

Just a small change - the script now uses the full path of '/system/bin/date' rather than just 'date'

The result of this is that you can set the time in recovery. Firstly, install the new version of the app and click 'install' to regenerate the userinit.sh file

Now, when you go into recovery, you can execute the following from your PC to set date

adb shell "mount /system ; mount /data ; /data/local/userinit.sh"
[G2 Themes and Apps] Phil3759:
But date -s is not allowed unless you patch kernel in a non recommended way as per Qualcomm directives

If no recovery dev patched kernel this way it is for a good reason
[G2 Themes and Apps] dougiebee:
Not sure I follow? date -s just sets the system date and doesn't attempt to adjust the RTC? It should be independent of what HW you're running on or even whether there is an RTC at all?

Are you referring to rtc_write_enable in the kernel? That certainly seems to be recommended to stay set to false, although that would seem to prevent anything but initscript based fixes
[G2 Themes and Apps] Phil3759:
date -s is not allowed on jflte family at least

Did you try in a recovery with non patched kernel?

Else could be missing se permissions
[G2 Themes and Apps] dougiebee:
I ran this with your recovery and with Wootever's 2.6.3.4 recovery, both worked - both set the system date. Note that this is not a permanent fix and is lost on reboot, I still have no way of writing to the RTC. I tried with your time_daemon, I tried "busybox hwclock -w" with a busybix that supports the hwclock extension. Neither wrote to the RTC.

I feel we'll need a modded kernel, although as you say, that's not recommended by qualcomm.

~ # /sdcard/busybox hwclock -w
hwclock: RTC_SET_TIME: Invalid argument

Prior art:

http://xdaforums.com/showpost.php?p=46426005&postcount=4

For the LG G2:

http://xdaforums.com/showpost.php?p=47994017&postcount=551

http://xdaforums.com/showpost.php?p=47994017&postcount=552

However I'm currently not setup to compile my own kernel.
[G2 Themes and Apps] Doc Sniper:
hwclock --systohc

As long as the kernel has the build in flag

.rtc_write_enable = false

set, hwclock --systohc is not allowed at all.

But I don't understand why. Is this a feature of the Snapdragon 800 ?

On my other phone, a Nexus 4 with Snapdragon 400, writing the time to RTC is allowed.
[G2 Themes and Apps] Phil3759:
I will dig into it

If it works, I can add it to postrecoverybooot.sh on boot

You can already do if you know how to repack recovery/boot images
[G2 Themes and Apps] dougiebee:
Thanks, repacking the recovery image worked a treat.

For info, my postrecoveryboot.sh reads:

#!/sbin/sh

mount /dev/block/platform/msm_sdcc.1/by-name/system /system -o ro
mount /dev/block/platform/msm_sdcc.1/by-name/userdata /data -o ro

[[ -f /data/local/userinit.sh ]] && /data/local/userinit.sh

umount /data
umount /system

I'm wondering if it would be better to store the time offset on the sdcard, somewhere like /data/media/0/rtc_offset so that userinit.sh and postrecoveryboot.sh could both reference it. Would have the added advantage that recovery wouldn't be dependent upon anything other than the offset file (might need to store a copy of the system date executable). Those using stock roms would then get the benefit for setting time in recovery too.
[G2 Themes and Apps] Phil3759:
Thank you again for the tests :good:

I will test if it is valid on all plateforms

Can you attach also the userinit.sh ?
[G2 Themes and Apps] dougiebee:
New version of app

OK, just updated the app to version 2.0 to aid setting date/time in recovery as well as the ROM.

Changes:

Now works with both AOSP and Stock ROMS (previously would freeze on stock)

Two installation buttons, one for stock and one for AOSP

Stock version will simply store the clock offset in /data/media/rtc_offset

AOSP version will store the clock offset in /data/media/rtc_offset and write userinit.sh to /data/local/userinit.sh - both files are required

Recovery can have its own script and reference /data/media/rtc_offset

Install the new version and click install for your ROM type. There should be no harm in clicking AOSP if you have a stock ROM - userinit.sh won't run. It's not required for stock, and does not work if you don't have busybox.

Note, you need to wait for an updated recovery that uses this file to correct the date/time.
[G2 Themes and Apps] dougiebee:
Just updated the app to v2.0, things have changed a bit as both AOSP and stock will now work (stock only needs the recovery fix as the ROMs are fine).

You don't need to care about userinit.sh in recovery anymore. I added two files to your recovery - /sbin/postrecoveryboot.sh and /sbin/toolbox which is a renamed busybox, used for setting date.

The stored busybox is required as recovery's busybox/toolbox won't set the date. Note the sleep at the start of the script is necessary as something was umounting /data at the same time as I was mounting it. Find the files in the attached zip. I'll PM you my modded d802 recovery.

Edit - I can't ... so I've attached my modded version of your d802 recovery. Folks, if you try this - it works like a dream for me, but use at your own risk. I guess it won't work with an encrypted /data. In case it needs spelling out - don't flash if you don't have a d802. I'm not prepared to repack other recoveries at this stage as I want Phil's feedback, and he'll hopefully add it to his recoveries.

Second edit - reuploaded "added_recovery_files" as the initial zip was empty. Oops :)

My /data/media/rtc_offset contains the number 1384422224

For info, I got the idea for using a specific busybox to set the date from here http://tau.shadowchild.nl/attn1/?p=341
[G2 Themes and Apps] simpsonr:
Just dl'd and installed. Went to recovery, created a nandroid and the title of the backup is 1970..... what am I missing?

I'm on a vs980 with Carbon kk rom
[G2 Themes and Apps] dougiebee:
My bad, the post wasn't clear. I just added the following text:

Note, you need to wait for an updated recovery that uses this file to correct the date/time.

If you have a d802, you can try my test repack in my reply to Phil. Or you can wait for his feedback and (hopefully) for him to incorporate into his recoveries.
[G2 Themes and Apps] simpsonr:
Ah, ok. Makes sense. How do I know if/when a new twrp will have these changes?
[G2 Themes and Apps] Phil3759:
thank you for all your work on this

I will look at it as soon as I have time and give full credit if used

Thank you in all cases :good:
[G2 Themes and Apps] Phil3759:
Well, reading through it better, I never thought that the date -s was failing because of busybox built in cwm. Good catch. Was not aware of that limitation

Bundling cwm with a separate busybox is not possible because of size limitations. I will see if we can use /system date function instead
[G2 Themes and Apps] dougiebee:
I can use /system/bin/date as I'm on CM11 and that's got busybox. May not work for stock users who don't have busybox. Note when using /system/bin/date I had to change strftime to strftime("%Y%m%d.%H%M%S",$1)

I did repack your d802 recovery with a stored busybox, it worked fine. It size is an issue, maybe it could be compressed? Alternatively I could try building busybox with only the date function defined?
[G2 Themes and Apps] Phil3759:
I build for near 100 devices. I do not plan to go with device specific fixes. Most won't fit and it has no sense to waste so much space

Stock toolbox supports date -s option
[G2 Themes and Apps] murdoch1:
CWM Patched

Would you plan to mirror a release from Phil (subject to his approval naturally) which incorporates your fix? I see you have applied the to 6.0.7.6 but notice that Phil has 6.0.7.9 version on his site for download). I have the D802 but not tried your patched version as wanted the latest recovery available.

Cheers

Andy
[G2 Themes and Apps] dougiebee:
I take your point, it is large and yes device specific fixes would be unmanageable. However this can be made a lot better. Busybox would be made way smaller if built with only the date plugin, and can also be bzip2'ed

My main concern would be that /system/bin/date varies from rom to rom and from device to device, and would likely be less reliable if relying on the system bin.
[G2 Themes and Apps] dougiebee:
I'll wait to see what's picked up, but yes, if this isn't incorporated into Philz or TWPR then I could likely mirror a repacked version subject to the authors approval.
[G2 Themes and Apps] Phil3759:
I gave a trial

Seems like a non bad approach, however, I have a few issues:

in ROM, I tried:

expr $(date +%s) - $(cat /sys/class/rtc/rtc0/since_epoch)
#result = 1389388964

in recovery, using adb shell, this set date correctly in recovery:

realdate=$(expr 1389388964 + `cat /sys/class/rtc/rtc0/since_epoch` | awk '{print strftime("%Y%m%d.%H%M%S",$1)}')
/system/toolbox date -s $realdate

once I set time using toolbox date -s manually in adb shell, it sticks between reboots. Reapplying the fix using offset obviously adds a few years to the thing!

Since date sticks in recovery, the current date from "date +%s" and "cat /sys/class/rtc/rtc0/since_epoch" are equal. This seems to persist even in main ROM and when trying it with your apk v2.0

expr $(date +%s) - $(cat /sys/class/rtc/rtc0/since_epoch)
#result = 0

I can no longer reset to 1970 loading time_daemon, just 2013

After resetting with time_daemon in recovery and reloading ROM, "cat /sys/class/rtc/rtc0/since_epoch" is now since 01.01.2013

Now obviously:

expr $(date +%s) - $(cat /sys/class/rtc/rtc0/since_epoch)
#result = 32395659

So, this way seems to have messed up the rtc settings, lost real time since epoch and potentially causing unwanted issues as warned by Qualcomm maybe.

Probably, resetting CMOS by pulling battery for a few hours should reset everything since my kernel has no patched to allow writing to rtc

I am not sure this is safe to use at all. I feel the only proper way is to find a way to correctly load time_daemon
[G2 Themes and Apps] dougiebee:
Sounds like a fair summary, need to be safe especially across so many devices.

Can I ask what device you tested this on? I'm guessing you don't have a G2? I'm not aware of anyone who has been able to get the clock to stick, hence the G2-specific problem? Or can some variants write to the clock and others not? I compiled a test kernel with all rtc_write_enable flags I could find set to true, still couldn't write to the clock.

The aim of my fix is a system such as the d802 where the clock cannot (currently) be adjusted/corrected.

I did manage to get the size of the busybox_date binary down to about 245kb in the end.

ps thanks for testing :good:
[G2 Themes and Apps] dougiebee:
Neither does mine - however it seems your rtc does persist across reboots? It certainly doesn't for the d802, back to 1970 on every (re)boot, time_daemon has no effect. What model do you have? Do we have any idea which models do and don't respond to time_daemon? Well done LG, jeez.....
[G2 Themes and Apps] Phil3759:
I am on a Samsung Galaxy S4 i9505

Samsung phones seem to have the time stick on reboot

So, using your method, on G2, you have no issues at all?

If that's the case, I can still add it for some devices through postrecoveryboot.sh instead of hard-coding it in recovery sources.

I also noticed that busybox date -s option is not disabled, it just throws a 38 error code, I am not sure about. Maybe it is disabled on Qualcomm chips?
[G2 Themes and Apps] dougiebee:
Correct, I have no issues at all. Even when the rom is running and date is correct, if I reboot into recovery I'm back to 1970. The date never sticks, the rtc never gets updated, which is why the workaround works.

If we ever do learn how to set/maintain time on the G2 then yes this workaround would be unsuitable given the problems you found, but I guess that's quite unlikely in the short/medium term.

Would be great if you could include the postrecoveryboot.sh and custom busybox on G2 recoveries rather than hard coded. I'll post my revised much smaller version when I'm on my PC.
[G2 Themes and Apps] Phil3759:
I plan to use toolbox from /system/bin

Is it ok on cm ROMs?
[G2 Themes and Apps] dougiebee:
Yeah it's fine. I was actually concerned about stock rather than CM, but I just checked and they're both fine using /system/bin/toolbox

Just be sure of which date string you use as they're annoyingly different for busybox and toolbox

"%Y%m%d.%H%M%S" for toolbox

"%Y.%m.%d-%H:%M:%S" for busybox

Thanks again
[G2 Themes and Apps] dougiebee:
Small update to v2.1

Just a small compatibility update. OP updated with version 2.1

As Phil3759 pointed out /system/bin/toolbox date works in all cases.

Installing the AOSP version will now use "/system/bin/toolbox date -s" rather than "/system/bin/date -s"
[G2 Themes and Apps] lAndroXl:
Nice, works great ;)

thanks a lot :laugh:
[G2 Themes and Apps] dannejanne:
Worked like a charm. Thanks.
[G2 Themes and Apps] Phil3759:
I am working to add this to PhilZ Touch

Will update soon

Thank you for all the support
[G2 Themes and Apps] Phil3759:
One request:

Why don't you save the rtc_offset to /sdcard

Would be easier for recovery to check it. That would maybe expand support to older HTC devices that has no /data/media

That way, recovery can use primary storage path that will auto link to emulated /sdcard if needed

Or even add an option to save it to /sdcard/clockworkmod folder directly
[G2 Themes and Apps] dougiebee:
So this is interesting. I compiled a new version and removed my old date-fix files ready to test the new version.

Still running CM11, I rebooted and noticed that the date was correct. Into recovery, back to 1970. Reboot back to CM11 (in aeroplane mode), clock was still correct. I've double and triple checked. It seems they have a proper fix. And they're not writing to the RTC, I checked.

I notice 5 particular kernel commits of interest:

https://github.com/CyanogenMod/android_kernel_lge_msm8974/commit/312651908f8150571feffdabc06389a2f9713ebe

Particularly the description in this one:

https://github.com/CyanogenMod/android_kernel_lge_msm8974/commit/d79db09399595a1aad5016d943c260fda487d628

https://github.com/CyanogenMod/android_kernel_lge_msm8974/commit/cf4d6e6ad02fdac3e59af29ea11857e8436cc659

https://github.com/CyanogenMod/android_kernel_lge_msm8974/commit/be618f953f9c25ce64aad31e64b050d0eebdfcac

https://github.com/CyanogenMod/android_kernel_lge_msm8974/commit/1cf55d82b9d4db304f7dfa5e5aee241e049ebd98

I assume there may be some related system commits too. Are you still interested in an updated app, or would tracking down CM's fix be more of interest?
[G2 Themes and Apps] Phil3759:
Good catch

What you mean is when you set time in main rom it now persists in recovery? Or you have to set it once with date -s in recovery and you are done? For Galaxy S4 series, it is case 2
[G2 Themes and Apps] dougiebee:
OK, it's nothing to do with the kernel. The time still does not persist, but it is set correctly by the ROM on each boot. even in aeroplane mode.

I now know how LG (and CM) set the date on the G2. The below was using your latest recovery without modification:

~ # uname -a
Linux localhost 3.4.0-CM+ #1 SMP PREEMPT Sat Dec 28 15:14:02 CET 2013 armv7l GNU/Linux
~ #
~ # date
Wed Mar 11 02:13:53 GMT 1970
~ #
~ # mount /system
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Mar 11 02:14:08 GMT 1970
~ #
~ # mount /data
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Jan 22 11:58:12 GMT 2014
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Jan 22 11:58:19 GMT 2014
~ #
~ # mv /data/time /data/time.bak
[3]+ Killed /system/bin/time_daemon
[2]+ Killed /system/bin/time_daemon
~ #
~ # date
Wed Jan 22 11:58:45 GMT 2014
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Mar 11 02:15:03 GMT 1970
~ #
~ # mv /data/time.bak /data/time
[2]+ Killed /system/bin/time_daemon
~ #
~ # date
Wed Mar 11 02:15:17 GMT 1970
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Jan 22 11:59:11 GMT 2014
~ #
~ # ls -l /data/time
-rw------- 1 system net_raw 8 Jan 22 11:03 ats_1
-rw------- 1 system net_raw 8 Jan 22 11:03 ats_2
~ #

If /data/time/ats_* files are not present, the date will not be set. I have no idea what CM commit fixed the ROM to behave like stock. I clearly remember seeing /data/time/ats_* on the stock LG ROM.

So using time_daemon with both system and data mounted should be fine as long as the ROM has behaved itself and created /data/time/ats_* correctly :good:
[G2 Themes and Apps] Phil3759:
1-The above code sets correctly time in recovery?

Could be i also need to fix recovery kernel to latest so that it persists on boot
[G2 Themes and Apps] dougiebee:
Yes, it does.

I separately repacked your recovery with the latest CM kernel but it made no difference, it still doesn't persist on boot.
[G2 Themes and Apps] MaluNoPeleke:
If the date/time would be fixed in the recovery, would it be necessary that the installed ROM includes the new CM commits to also always have the correct date/time?
[G2 Themes and Apps] Phil3759:
I will try the code on my s4. Would be great to fix time for all qcom devices include htc

Thank you for the support

Strange that with s4 the date -s sets it in a way it persists on recovery reboot
[G2 Themes and Apps] dougiebee:
The code is just running time_daemon with both system and data mounted. Would be very interested to hear if your s4 has a /data/time directory or not?
[G2 Themes and Apps] dougiebee:
I'm only guessing, but I would expect so. I don't know when CM changed.
[G2 Themes and Apps] Phil3759:
i9505 has the /data/time data, but I use stock kernel/rom

I did not test your code yet, however I am sure it doesn't work on i9505

Chenglu uses it on Note 3 but same code with my kernel repacked from sources doesn't work

On i9505, running this twice:

LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &

date

LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &

date

will increment the date by a few months/years every time

I also released this fix a few time ago inside recovery menu for G2. All users reported it failing :confused:

All this is confusing
[G2 Themes and Apps] dougiebee:
Yeah, I remember it failing. I'll revert back to stock later and test again on that.

It's a bummer that the i9505 increments every time and the g2 doesn't - certainly doesn't make supporting multiple devices easy.
[G2 Themes and Apps] Phil3759:
Yes, please do

Before implementing a fix, I must be sure it works

The issue with i9505 is that date persists when rebooting recovery, causing the daemon to fail and increment time

There must be many things there. Kernel is probably related. My time_daemon trial on Note 3 failed while a repack on stock kernel + permissive selinux works
[G2 Themes and Apps] dougiebee:
Exactly the same behaviour in recovery with stock rom installed as I got with CM :good:

/system and /data must both be mounted or it will not work

/data/time/ must exist with ats_1 and ats_2 inside

Running multiple times is not a problem

Date does not persist across any reboot, but ROM will correct it during boot so that's not an issue

~ #
~ # date
Wed Mar 11 10:44:01 GMT 1970
~ #
~ # mount /system
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Mar 11 10:45:20 GMT 1970
~ #
~ # mount /data
~ #
~ # date
Wed Mar 11 10:45:31 GMT 1970
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Jan 22 20:29:22 GMT 2014
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
[2]- Killed /system/bin/time_daemon
~ #
~ # date
Wed Jan 22 20:29:44 GMT 2014
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
~ #
~ # date
Wed Jan 22 20:29:52 GMT 2014
~ #
~ # LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib /system/bin/time_daemon &
[3] Killed /system/bin/time_daemon
~ #
~ # date
Wed Jan 22 20:29:54 GMT 2014
~ #
[G2 Themes and Apps] Phil3759:
Thank you

I will release a multi fix using 3 modes:

Maybe with that we cover most people
[G2 Themes and Apps] dougiebee:
Sounds good, comprehensive. I'll build a new version of the app tomorrow that puts the rtc_offset file in /sdcard
[G2 Themes and Apps] dougiebee:
Done, OP updated.

Version 2.2 will save rtc_offset to /sdcard and to /data/media if available.

The initscript will use either file, looking for /sdcard first.
[G2 Themes and Apps] Kreuger:
Hey I installed this on my D803. Installed with the initscript. The code came back 1390575456 does this mean anything?
[G2 Themes and Apps] dougiebee:
Yeah that's pretty normal, there will be a date offset between your phone clock and the real date, that's it expressed in seconds. So your ROM should be good now. There's no recovery that uses this yet but Philz recovery is working on using this and other clock fixes
[G2 Themes and Apps] Phil3759:
Can you look and test this one please:

http://xdaforums.com/showthread.php?p=49782499

This version uses settimeofday() function

Since time now sticks in recovery, it should work

I will after work on drop to /system calls to date binary when settimeofday() fails

And finally, I will add the time_daemon fix
[G2 Themes and Apps] dougiebee:
As reported in the other thread, date/time is volatile. Time not correct on entering recovery. Can set it using your GUI tool but is lost upon reboot.
[G2 Themes and Apps] Phil3759:
http://xdaforums.com/showpost.php?p=49792953&postcount=328

The Qcom Time Daemon option runs this shell command after mounting /system and /data

LD_LIBRARY_PATH=LD_LIBRARY_PATH:/system/vendor/lib:/system/lib; /system/bin/time_daemon &(sleep 2; killall time_daemon; /sbin/umount /system) &

if it fails, try these:

If still fails, provide a log
[G2 Themes and Apps] provolinoo:
[MENTION=617634]dougiebee[/MENTION]

thanks a lot, just installed 2.2 :)

could you please add a way to show that the patch is installed and in which way when you open the app? :)
[G2 Themes and Apps] Phil3759:
Seems it fails to load time daemon

Hope you can debug it. Strange you say it works in shell while it doesn't from recovery

Could be i should use export for libpath