Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Os.date "isdst" issue.

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Tue Dec 15, 2020 10:38 am    Post subject: Os.date "isdst" issue. Reply with quote

Os.date "isdst" issue.
The code below adds the country time difference automatically. Or I think so.
I have to subtract 3 hours each time.
I have to disassemble the current time zone, reassemble it and format it.
But when I collect it again, I see an extra 3 hours added.


Code:
local dh,dm,ds=os.date("%H"), os.date("%M"), os.date("%S")
print("Available: "..dh..":"..dm..":"..ds)

local xxx5,xxx6,xxx7=3600*dh, 60*dm, ds*1
local xxx8=xxx5+xxx6+xxx7 --3*3600
print(os.date("Formatted: %X", xxx8))



Is there a solution about this?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4652

PostPosted: Tue Dec 15, 2020 12:22 pm    Post subject: Reply with quote

You're completely ignoring other information pertinent to specifying a point in time. In Lua's case, you also need the year, month, and day.

This is how you add 3 hours to a time correctly in Lua:
Code:
local d = os.date'*t'
d.hour = d.hour + 3
local t = os.time(d)
print(os.date("Formatted: %X", t))
For more information, RTFM.
_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Tue Dec 15, 2020 2:10 pm    Post subject: Reply with quote

This answer partially answers my question.
I didn't try to add 3 hours, my code in the question itself adds 3 hours.
But your answer partially enlightened me. Thank you.

If you have an idea; How can I print the difference between the manually inserted time and the current time?

Example;
Expected time: 19:50:00
Current time: 19:40:39
Time left: 0:09:21

The above output is the result I reached with a lot of unnecessary code.
But is there a shorter way to find it?
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 150

Joined: 06 Jul 2014
Posts: 4652

PostPosted: Tue Dec 15, 2020 4:55 pm    Post subject: This post has 1 review(s) Reply with quote

os.date only formats time points. AFAIK there's nothing in the standard library to format time intervals, so you'll have to do that yourself:
Code:
function format_seconds(sec)
  if sec < 0 then return '00:00:00' end
  return ('%02d:%02d:%02d'):format(sec // (60*60), (sec // 60) % 60, sec % 60)
end

Use os.difftime to get the number of seconds left.
Code:
local start = os.date'*t'
local finish = {}

for k,v in pairs(start) do finish[k] = v end
finish.min = finish.min + 10

local starttime = os.time(start)
local finishtime = os.time(finish)

function status()
  print(os.date('Start time:  %X', starttime))
  print(os.date('Finish time: %X', finishtime))
  print('Remaining:   ' .. format_seconds(os.difftime(finishtime, os.time())))
end

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Tue Dec 15, 2020 6:02 pm    Post subject: Reply with quote

Thank you.
You always have a different and effective style.
+1 for your help and patience, thank you again.
Here is the result of the code I have made inexperienced;
"format_seconds" was the code I was looking for.

Code:
if f then f.destroy() end

f=createForm(true)
f.Position=poDesktopCenter
f.Width=220 f.Height=105

local l1=createLabel(f) l1.AutoSize=false
l1.Height=25 l1.Width=200 l1.Left=10 l1.Top=10
l1.Font.Size=12 l1.Font.Style="fsBold" l1.Alignment="taCenter"

local l2=createLabel(f) l2.AutoSize=false
l2.Height=25 l2.Width=200 l2.Left=10 l2.Top=40
l2.Font.Size=12 l2.Font.Style="fsBold" l2.Alignment="taCenter"

local l3=createLabel(f) l3.AutoSize=false
l3.Height=25 l3.Width=200 l3.Left=10 l3.Top=70
l3.Font.Size=12 l3.Font.Style="fsBold" l3.Alignment="taCenter"

---------------------- function -------------------
function format_seconds(sec)
  if sec < 0 then return '00:00:00' end
  return ('%02d:%02d:%02d'):format(sec // (60*60), (sec // 60) % 60, sec % 60)
end

local d1 = os.date'*t'
local xx=1
local x = ({"h02m52s00", "h02m54s00", "h02m56s00"})
local prt3=""
if secTimer then secTimer.destroy() end
secTimer=createTimer() secTimer.Interval=1000 secTimer.Enabled=true

secTimer.OnTimer=function()
if prt3==("Remaining:     00:00:00") then
xx=xx + 1 end
if xx==4 then secTimer.Enabled=false l3.caption="Execute time: Finish!"
else
local xh,xm,xs=string.match(x[xx], "h(.*)m(.*)s(.*)")
--print("\nxx: "..xx)
local xxx1,xxx2,xxx3=3600*xh, 60*xm, xs*1
local xxx4=xxx1+xxx2+xxx3

l1.caption=("Execute time:   "..xh..":"..xm..":"..xs)

local dh,dm,ds=os.date("%H"), os.date("%M"), os.date("%S")

local xxx5,xxx6,xxx7=3600*dh, 60*dm, ds*1
local xxx8=xxx5+xxx6+xxx7
xxx9=xxx4-xxx8--3*3600
xxx8 = os.time(d1)
l2.caption=(os.date("Current time:   %H:%M:%S"))

prt3=('Remaining:     '..format_seconds(xxx9))
l3.caption=(prt3)
end
end
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites