Change time format

Post your questions and help other users.

Moderator: Martin

Post Reply
Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Change time format

Post by Lucy » 06 Dec 2019 16:31

Can i convert standard time output, example "08:30" to milliseconds so i can use getduration with real time and next alarm?

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

Re: Change time format

Post by Desmanto » 07 Dec 2019 02:31

Thanks to your question, I retry my quest to find the shortest formula to get timezone in miliseconds. And ta da :D , from

Code: Select all

tz = -getDate("{0,dateformat,z}", "z");
to

Code: Select all

tz = -getDate(0, "s");
I also decided that the shortest way to get today date (at 00:00), is to use

Code: Select all

today = getDate(getDate(), 0, 0, 0);
==========================

Now back to your topic, where you want to get 08:30 only as the miliseconds since that day. You can use

Code: Select all

mili = getDate("08:30", "hh:mm");
However this will get you different result depends on your timezone. If UTC (which is the real value we want), it should be (8 x 3600000) + (30 x 60000) = 30600000 miliseconds. But my timezone is UTC+7, I get 5400000 instead (the time is stored as 01:30).

So I have to added back my timezone to the mili, to get the UTC based back. That's why we need to find the timezone. And that's where the above formula come into play.

Code: Select all

mili = getDate("08:30", "hh:mm");
tz = -getDate(0, "s");
cmili = mili + tz;
We use 0 based to get the seconds format. It will get the 1 January 1970 (which is the base linux epoch time), minus your timezone. So at mine, tz is -25200000 (-7 hours). To make it positive, I negate it. So when this added back to the 5400000, I will get the correct 30600000. Use cmili in your calculation.

I might be wrong about your real goal. If it is different, please tell the final result you want to get, and we can adjust the script to it.

Depends on your alarm format, you might encounter something similar in the MIUI : viewtopic.php?f=5&t=7757&p=23144&hilit=Talarm#p23144
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.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Change time format

Post by Lucy » 07 Dec 2019 03:29

😂 you made me laugh when i read the start of your reply!!
Yep that looks about like the conversion i need.

My next alarm time reads as "mon 09:55".
So i done a get date for just the time and stripped the alarm time day and assigned new glovars. So both next alarm and get date look like "03:22". I immediately tried to get duration from that not thinking and obviously encountered an error with format.

So now that you presented me with that beautiful post i should be able to use what you showed ne with this...

Code: Select all


global_alarm = next_alarm 
// this is a glovar i already have, just showing you what global_alarn is.

true_time = "{getDate(),dateformat,HH:mm}";
// grab now time in same format as alarm minus the day that i donot want.

tempalarm = global_alarm;
//give my glovar a temporary home.

Strippedalarm = right(tempalarm, length(tempalarm) - 4);
//strip the day "sat" from the alarm time.

//this is where i would apply your code to reformat.

global_time_runtime =
(Strippedalarm - true_time);
// this should give duration between times in milliseconds. 

global_time_runtime_formated =
getDurationString(global_time_runtime);
// convert the milliseconds format to string to give me a readout of "2d 3h 22m" (as example).!

This should work!? Ima have a go after.
Thankyou

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

Re: Change time format

Post by Desmanto » 07 Dec 2019 17:05

Hahaha, you can't imagine how much time I spent just to find the shortest way to get the timezone value. I keep trying it when I need it again. Now the laziest part is to replace any existing formula, but I usually just leave them alone; not trying to fix the already working ones.

You got the similar alarm with MIUI, and I think this is the new standard in newer android. You should use the script in the link above instead. If you remove the day first, you will find you get the wrong alarm day when it skip days. Example today is saturday, and the next alarm is "Mon 08:00". if you remove the "Mon" first, you might get wrong result that next alarm is Sunday 08:00. My script in the link above already take the day into calculation, so better use that instead of the post above.

And also you may not convert the true_time into string first. You need it in the calculation later, so you should leave it as datetime format. Strippedalarm also won't work, as it is pure string "09:55". can't be used in calculation. This is the example I replace the calculation part with the one in the link above.

Code: Select all

global_alarm = next_alarm; //example "Mon 09:55"

talarm = getDate(global_alarm, "EEE HH:mm"); //get alarm time using the pattern
toweek = getDate() / (7*86400000) * (7*86400000); //this find current week

nexta = toweek + talarm; //plus the alarm and current week
if(nexta < getDate())
  nexta = addDays(nexta, 7); //if next alarm is in the past, add it with 7 days

global_time_runtime = nexta - getDate(); //find the duration in milliseconds
global_time_runtime_formated = getDurationString(global_time_runtime); //Format the duration
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.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Change time format

Post by Lucy » 07 Dec 2019 23:38

Ah yeah i see what your saying. Im gonna change it up.
Oh as for my true time glovar. It is not in that script, i just call it. Was showing you what the glovar is. But yes i understand too well the format comversion😂 mistakes is my way of coding

Post Reply