Time calculate

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
boruciak
Posts: 20
Joined: 31 Aug 2018 09:06

Time calculate

Post by boruciak » 18 Dec 2018 13:48

Hi


time_1=07:12:15
time_2="{getDate(), dateformat,HH:mm:ss}"

How to calculate time in script?

time=time_2 - time_1

Thx

User avatar
fagci
Posts: 78
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

Re: Time calculate

Post by fagci » 18 Dec 2018 15:18

d = "1:30:00";

p = split(d, ":");

cDate = getDate();

nDate = addSeconds(cDate,p[2]);
nDate = addMinutes(nDate,p[1]);
nDate = addHours(nDate,p[0]);

fDate ="{nDate, dateformat,HH:mm:ss}";


log(fDate);

// sorry, didn't understand question at first read
Last edited by fagci on 18 Dec 2018 16:56, edited 2 times in total.
All systems nominal.

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

Re: Time calculate

Post by Desmanto » 18 Dec 2018 16:45

Dealing with date in string format, you have to convert it back to a datetime format first. You can use

Code: Select all

time_1 = "07:12:15";
t1 = getDate(time_1, "HH:mm:ss");
But this will give you t1 in 1st January 1970, 07:12:15. Because there is not date, month, year pattern there. So you have to get today's date and append it first to the time_1 and parse it.

Code: Select all

time_1 = "07:12:15";
time_2 = getDate(); //get current time
today = "{time_2,dateformat,dd MMM yyyy}"; // 18 Des 2018
today_time_1 = today + ", " + time_1; // 18 Des 2018, 07:12:15

t1 = getDate(today_time_1, "dd MMM yyyy, HH:mm:ss"); //convert to time
time = abs(time_2 - t1);
I use abs(), as I assume you want to find the difference between time_1 and time_2. It will convert any negative to positive, so give you the difference only.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200117 Official, Android Pie 9.0, Rooted.

User avatar
fagci
Posts: 78
Joined: 03 Feb 2017 13:06
Location: Novosibirsk, RU
Contact:

Re: Time calculate

Post by fagci » 18 Dec 2018 17:25

Possible question not correct, because:

If you want to calc time between two dates, simply save them and calc timestamp difference.

If you want substract interval, ex.: 7h, 5min (7:05:00), use my method.

//btw, current day domain is not needeed.

targetTime = '1:30:00';
d1 = getDate(targetTime, "HH:mm:ss");
d2 = getDate("{getDate(),dateformat,HH:mm:ss}", "HH:mm:ss");

log(1.0*(d2-d1)/1000/60/60);
All systems nominal.

boruciak
Posts: 20
Joined: 31 Aug 2018 09:06

Re: Time calculate

Post by boruciak » 19 Dec 2018 07:33

Ok thank you for help.
In fact, I did not specify exactly what I mean.
My plan is:
- save getData() to the file
- after some time - reading data from the file
- comparison with current getData
- the result is "something" was done by the "x" time

I still have a question.

t1=getDate();
//sleep 10m
t1=getDate();
T=abs(t2-t1);
T="{T,dateformat HH:mm:ss}";

result 01:10:00 - an hour more - why is this?

P.S. Excuse me for asking stupid or obvious questions

anuraag
Posts: 301
Joined: 24 Jan 2015 02:06

Re: Time calculate

Post by anuraag » 19 Dec 2018 08:32

boruciak wrote: T="{T,dateformat HH:mm:ss}";
change it to
T=getDurationString(T);

Or
T="{T,dateformat,timezone,UTC,HH:mm:ss}"

boruciak
Posts: 20
Joined: 31 Aug 2018 09:06

Re: Time calculate

Post by boruciak » 19 Dec 2018 08:44

ok - there was no question.
I get it :)
Tnx

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

Re: Time calculate

Post by Desmanto » 19 Dec 2018 16:48

@fagci : I also use the similar concept before. But I realized that the time d2 calculated will be in 1st January 1970 range. While in most cases we want to use today's range. So it is better to convert today's range. We can reused the d1 or d2 later if we need them. I usually reused them, so I make it as my best practice to use today's range.

@boruciak : If you are sure that the t2 always later than t1, you don't need abs() anymore. I put that since I thought you are using alarm as the time, which always happen in the future.

If you need to count only the duration, just save the starting point time to a glovar.

Code: Select all

global_starting_point = getDate();
later after finish, subtract it from current time.

Code: Select all

elapsed = getDate() - global_starting_point;
Then you can convert it to duration as pointed out by anuraag.

Time converted without UTC mark, will be affected by your timezone. You are in UTC+1, hence the time will be plus 1 hour.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200117 Official, Android Pie 9.0, Rooted.

boruciak
Posts: 20
Joined: 31 Aug 2018 09:06

Re: Time calculate

Post by boruciak » 20 Dec 2018 06:46

it's perfect.
Tnx

Post Reply