Funktion addDays in einer Schleife

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Funktion addDays in einer Schleife

Post by Bingwu » 27 Jul 2017 07:53

Hallo!

Ich habe nicht erwartete Ergebnisse in einem Script mit einer Schleife in Verbindung mit der Funktion "addDays".
Ziel ist es, ein jeweils um einen Tag erhöhtes Datum (mit der Uhrzeit 0 Uhr) zu generieren.

Das Script (in vereinfachter Darstellung):
start = getDate(2017,7,27,0,0,0)
for(i in [0 to 364]) {
tag = addDays(start,i);
log("{tag,dateformat,dd.MM.yyyy HH.mm.ss,SSS}")
}
Wenn ich mir die Daten im Log ansehe, so werden auch Daten mit der Uhrzeit 23 Uhr des Vortags generiert.

Auszug aus dem Log:
.
.
28.10.2017 00.00.00,000
29.10.2017 00.00.00,000
29.10.2017 23.00.00,000
30.10.2017 23.00.00,000
.
.
23.03.2018 23.00.00,000
24.03.2018 23.00.00,000
26.03.2018 00.00.00,000
26.03.2018 00.00.00,000
.
.
25.07.2018 00.00.00,000
26.07.2018 00.00.00,000
Ich hoffe, dass mir jemand einen Tip geben kann. Vielen Dank!

Gruss
Peter

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Funktion addDays in einer Schleife

Post by Desmanto » 27 Jul 2017 11:30

Do you edit the post a few hours ago? I've tried your script before edit, there are some capital letter mismatch. Script and variable are case-sensitive, especially in pattern character.

I modify your script a bit, and throw out the log. I prefer to keep it all in single variable {all}. Then use another Condition : Debug Dialog to check the result. The script still can be compacted, but will make it harder to understand.

Code: Select all

all = "";
start = getDate(2017,7,27,0,0,0);
for (i in [0 to 364]) { 
tag = addDays(start,i); 
all = concat(all,"{tag,dateformat,dd.MM.yyyy HH.mm.ss,SSS}\n");
}
Your new script works properly at mine. No 23:00:00 found
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Funktion addDays in einer Schleife

Post by Bingwu » 27 Jul 2017 12:13

Hello!
Hallo!

No, the post is unchanged, so the script as well.
Nein, der Post ist unverändert, das Script demnach auch.

I have copied your script directly from the forum to my script ... same result as with my script (see attached text file)
Ich habe dein Script direkt aus dem Forum in mein Script kopiert ... selbes Ergebnis wie mit meinem Script (siehe beigefügte Textdatei)

Strange!
Seltsam!
Attachments
all.txt
(8.91 KiB) Downloaded 875 times

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Funktion addDays in einer Schleife

Post by Desmanto » 27 Jul 2017 14:00

Ah, Seems like your daylight saving time.
I live in Indonesia, near equator, we don't have DST, thus i have no problem with it.
I have no experience dealing with DST, can't find any reference for DST in automagic, nor any workaround at the automagic preference.
Maybe Martin can help.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Funktion addDays in einer Schleife (Sommerzeitproblem)

Post by Bingwu » 27 Jul 2017 14:49

"Ah!" seems to be international! This was also my reaction! :-)
"Ah!" scheint international zu sein! Das war auch gerade meine Reaktion!

I did not think about daylight safing time! Well observed! (thumbs up)
An die Sommerzeit habe ich nicht gedacht! Gut beobachtet! (Daumen hoch)

The reason is now found, now only an elegant solution is missing. In the meantime, I add (Edit: ... or subtract :o) the hour on these days. This also works.
Der Grund ist jetzt gefunden, jetzt fehlt nur noch eine elegante Lösung. In der Zwischenzeit addiere (Edit: ... oder subtrahiere) ich bei diesen Tagen die Stunde. Das funktioniert auch.

Thank you for your efforts!
Danke für deine Bemühungen!

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Funktion addDays in einer Schleife

Post by Martin » 27 Jul 2017 19:53

Hi,

addDays just adds 24 hours which can lead to this situation for DST.

A workaround would be to add one more hour to ensure that the date lies on the next calendar date and then get the date for midnight.

Code: Select all

all = "";
start = getDate(2017,7,27,0,0,0);
for (i in [0 to 364]) { 
tag = getDate(addHours(start,i*24+1), 0, 0, 0); 
all = concat(all,"{tag,dateformat,dd.MM.yyyy HH.mm.ss,SSS}\n");
}
or avoiding the problem:

Code: Select all

all = "";
start = getDate(2017,7,27,0,0,0);
for (i in [0 to 364]) { 
tag = addDays(start,i); 
all = concat(all,"{tag,dateformat,dd.MM.yyyy} 00.00.00,000\n");
}
or use the getDate function to return the dates:

Code: Select all

all = "";
for (i in [0 to 364]) { 
tag = getDate(2017,7,27+i,0,0,0);
all = concat(all,"{tag,dateformat,dd.MM.yyyy HH.mm.ss,SSS}\n");
}
Not sure which solution works best in your case.

Regards,
Martin

User avatar
Bingwu
Posts: 114
Joined: 26 Feb 2016 10:26

Re: Funktion addDays in einer Schleife

Post by Bingwu » 27 Jul 2017 20:11

Hallo Martin!

Ich nehme "Tor 3"! :D

Code: Select all

getDate(2017,7,27+i,0,0,0)
Cool und simpel!
Danke!

Gruss
Peter

Post Reply