[TOOL] Fix TWRP Backup Dates / Find Your Phone's Birthday

Search This thread

jeffsf

Senior Member
Mar 7, 2011
1,121
976
San Francisco
Well, a week into my Nexus 5 and the dates on backups got to me. "Was that the one before or after I pooched my phone?"

I was doing rough conversions at first, then convinced myself that there was a reasonably fixed offset between "real time" and what TWRP was labeling things. @helicopter88 let me know that there was a missing ability in the chip, so I looked for a run-time solution. Unfortunately, adjtimex wouldn't take a large enough offset.

Since the reasonably useless date stamps on backups were, well, reasonably useless, I did write a little script that converted the dates on the backups to something that matched my local time, given the offset.

Looks like @Dees_Troy has saved me a bunch of coding inside TWRP as there is a going-forward fix in the TWRP sources now. Since he found the magic file where the offset is stored, you don't have to try to "guess" it from looking at your clock and your phone's (though $ date && adb shell date will get you the information you need).

For those backups you already have, here's how you can get the dates and times fixed up on them.

Edit -- Shell scripts made up for on-phone use:

sh birthday.sh while on the phone will give you offset and your phone's birthday (Yes, Korean time, of course!)

Code:
root@hammerhead:/ # sh /data/media/0/twrp-dates-tool/birthday.sh                                       <
1386550085
Mon Dec  9 09:48:05 KST 2013

sh spew-twrp-rename.sh /path/to/TWRP/BACKUPS will write the commands to move the directories to the terminal (stdout)

The "spew" script finds the offset itself.
It will rewrite both old (20th century) dates and current ones, but only time-shift the older ones.
It will also convert spaces in the file names to underscore characters.

See attached files (.txt added to allow upload)

No, there isn't a "zip" for this -- checking that the right things are being done with your backups before they can't be undone is important





Read on only if you want to do this using perl.


Now, you can adb pull /data/system/time/ to get two files, ats_1 and ats_2 that have the offset in milliseconds as a 64-bit unsigned integer. Pick one or the other, get the human-readable output trim off the last three digits, and there is your offset (within a second). Mine drifts a second each week or so, but hey, this is a lot closer than 1970 something!

Once you have the offset, this script will take a list of files and spit out the commands to rename them, if they match a TWRP-format date in the 1970s. (Edit $offset to match yours.)

Code:
#!/usr/bin/perl

use strict;

use Date::Parse;
use Date::Format;

my $offset = 1386550084; # Edit this to match your offset

my $line;
my ($year,$mo,$day,$h,$m,$s);

my $twrp_time;
my $new_time_string;
my $before;
my $after;

LINE:
while ( defined ( $line = <> ) ) {

    chomp $line; chomp $line;

    next LINE unless $line =~ m/((197\d)-(\d\d)-(\d\d)--(\d\d)-(\d\d)-(\d\d))/;

    $before = $`; $after = $';
    $twrp_time = str2time("$2/$3/$4 $5:$6:$7 GMT");

    $new_time_string = time2str("%Y-%m-%d_%H%M%S%z", $twrp_time + $offset);

    $before =~ s/ /_/g;
    $after =~ s/ /_/g;

    print "mv '$line' ${before}${new_time_string}${after}\n";
}

Output looks like
Code:
mv '1970-02-22--23-02-13 omni_hammerhead-userdebug 4.4.2 KOT49H 128' 2014-01-30_155017-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_128
mv '1970-02-23--13-50-29 omni_hammerhead-userdebug 4.4.2 KOT49H 128' 2014-01-31_063833-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_128
mv '1970-02-23--15-22-06 omni_hammerhead-userdebug 4.4.2 KOT49H 126' 2014-01-31_081010-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_126
mv '1970-02-23--16-10-01 omni_hammerhead-userdebug 4.4.2 KOT49H 126' 2014-01-31_085805-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_126
mv '1970-02-25--17-11-08 omni_hammerhead-userdebug 4.4.2 KOT49H 127' 2014-02-02_095912-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_127
which you can copy and paste into a shell on your device, or other places you have your TWRP backups backed up.

.
 

Attachments

  • birthday.sh.txt
    436 bytes · Views: 254
  • spew-twrp-rename.sh.txt
    1.2 KB · Views: 101
Last edited:

aquavalanche

Senior Member
Jan 27, 2012
221
28
Whoa! Nice one man! A bit of extra work is just fine rather than renaming each of the backups! Thanks a bunch

Sent from my Nexus 5 using Tapatalk
 

jeffsf

Senior Member
Mar 7, 2011
1,121
976
San Francisco

To be clear, the perl scripts run on something that has perl installed, like your laptop or desktop. The output runs just fine over adb shell to rename files on your phone. It also works just fine to rename the backup copies that you are keeping on your Mac, Linux, FreeBSD box. For Windows without Cygwin or similar, you'd want to change "mv" to "move"

Edit -- shell scripts for on-phone use added to lead post
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 10
    Well, a week into my Nexus 5 and the dates on backups got to me. "Was that the one before or after I pooched my phone?"

    I was doing rough conversions at first, then convinced myself that there was a reasonably fixed offset between "real time" and what TWRP was labeling things. @helicopter88 let me know that there was a missing ability in the chip, so I looked for a run-time solution. Unfortunately, adjtimex wouldn't take a large enough offset.

    Since the reasonably useless date stamps on backups were, well, reasonably useless, I did write a little script that converted the dates on the backups to something that matched my local time, given the offset.

    Looks like @Dees_Troy has saved me a bunch of coding inside TWRP as there is a going-forward fix in the TWRP sources now. Since he found the magic file where the offset is stored, you don't have to try to "guess" it from looking at your clock and your phone's (though $ date && adb shell date will get you the information you need).

    For those backups you already have, here's how you can get the dates and times fixed up on them.

    Edit -- Shell scripts made up for on-phone use:

    sh birthday.sh while on the phone will give you offset and your phone's birthday (Yes, Korean time, of course!)

    Code:
    root@hammerhead:/ # sh /data/media/0/twrp-dates-tool/birthday.sh                                       <
    1386550085
    Mon Dec  9 09:48:05 KST 2013

    sh spew-twrp-rename.sh /path/to/TWRP/BACKUPS will write the commands to move the directories to the terminal (stdout)

    The "spew" script finds the offset itself.
    It will rewrite both old (20th century) dates and current ones, but only time-shift the older ones.
    It will also convert spaces in the file names to underscore characters.

    See attached files (.txt added to allow upload)

    No, there isn't a "zip" for this -- checking that the right things are being done with your backups before they can't be undone is important





    Read on only if you want to do this using perl.


    Now, you can adb pull /data/system/time/ to get two files, ats_1 and ats_2 that have the offset in milliseconds as a 64-bit unsigned integer. Pick one or the other, get the human-readable output trim off the last three digits, and there is your offset (within a second). Mine drifts a second each week or so, but hey, this is a lot closer than 1970 something!

    Once you have the offset, this script will take a list of files and spit out the commands to rename them, if they match a TWRP-format date in the 1970s. (Edit $offset to match yours.)

    Code:
    #!/usr/bin/perl
    
    use strict;
    
    use Date::Parse;
    use Date::Format;
    
    my $offset = 1386550084; # Edit this to match your offset
    
    my $line;
    my ($year,$mo,$day,$h,$m,$s);
    
    my $twrp_time;
    my $new_time_string;
    my $before;
    my $after;
    
    LINE:
    while ( defined ( $line = <> ) ) {
    
        chomp $line; chomp $line;
    
        next LINE unless $line =~ m/((197\d)-(\d\d)-(\d\d)--(\d\d)-(\d\d)-(\d\d))/;
    
        $before = $`; $after = $';
        $twrp_time = str2time("$2/$3/$4 $5:$6:$7 GMT");
    
        $new_time_string = time2str("%Y-%m-%d_%H%M%S%z", $twrp_time + $offset);
    
        $before =~ s/ /_/g;
        $after =~ s/ /_/g;
    
        print "mv '$line' ${before}${new_time_string}${after}\n";
    }

    Output looks like
    Code:
    mv '1970-02-22--23-02-13 omni_hammerhead-userdebug 4.4.2 KOT49H 128' 2014-01-30_155017-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_128
    mv '1970-02-23--13-50-29 omni_hammerhead-userdebug 4.4.2 KOT49H 128' 2014-01-31_063833-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_128
    mv '1970-02-23--15-22-06 omni_hammerhead-userdebug 4.4.2 KOT49H 126' 2014-01-31_081010-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_126
    mv '1970-02-23--16-10-01 omni_hammerhead-userdebug 4.4.2 KOT49H 126' 2014-01-31_085805-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_126
    mv '1970-02-25--17-11-08 omni_hammerhead-userdebug 4.4.2 KOT49H 127' 2014-02-02_095912-0800_omni_hammerhead-userdebug_4.4.2_KOT49H_127
    which you can copy and paste into a shell on your device, or other places you have your TWRP backups backed up.

    .
    2
    Yes, or over "adb shell" which makes cut-and-paste a lot easier.

    Posted from whatever phone booted today.
    1
    Possible a zip file to flash for this ?

    Let me look into what options there are in toolbox/busybox for the date parsing. As this was a quick-and-dirty solution, I didn't dive into trying to get it to run on the device.