1

Тема: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

Приветствую всех. У меня есть таймер с кнопками https://imgur.com/a/vkEcmoP, при нажатии кнопки должно взяться время, которое указанно на таймере и прибавится к нынешнему времени, попробовал многое, но выходит результат по типу 11:02:86, либо же вообще заменяет нынешнее время на время с таймера, помогите пожалуйста с этой проблемой.
Код самого таймера:


StartTimer1:
    if (!Timer1Running) {
        SetTimer, UpdateTimer1, 1000
        Timer1Running := true
    }
Return
UpdateTimer1:
    if (Timer1Running && Timer1Seconds > 0) {
        Timer1Seconds -= 1
        Minutes1 := Floor(Timer1Seconds / 60)
        Seconds1 := Mod(Timer1Seconds, 60)
        FormattedTime1 := Format("{:02}:{:02}", Minutes1, Seconds1)
        GuiControl,, Timer1Text, %FormattedTime1% Время до свапа локи
    } else if (Timer1Seconds <= 0 && Timer1Running) {
        GuiControl,, Timer1Text, Time's up! Время свапа локи
        SetTimer, UpdateTimer1, Off
        Timer1Running := false
    }
Return

Код самой кнопки:

ResumeTimer1:
    if (!Timer1Running) {
        SetTimer, UpdateTimer1, 1000
        Timer1Running := true
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
		A_Min1 := A_Min + Minutes1
		A_Sec1 := A_Sec + Seconds1
        A_Min2 := Floor(A_Min1 / 60)
        A_Sec2 := Mod(A_Sec1, 60)
		FormattedTime0111 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min2, A_Sec2)
        SendInput, /cb Таймер возобновлён, смена локации в %FormattedTime0111%
    }
Return

2

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

А зачем выкладываете код по кускам? Запостите в том виде, в котором можно запустить и увидеть проблему.

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

3 (изменено: Lelouch, 2025-01-10 17:05:30)

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

#Persistent
#NoEnv
SetBatchLines, -1

Gui, Color, White
Gui, Font, S12, Verdana

; Timer 1
Gui, Add, Text, x10 y0 w150 Center vTimer1Text, 02:00 Время до свапа локи
Gui, Add, Button, x10 y40 w150 gStartTimer1, Start Timer 1
Gui, Add, Button, x10 y80 w150 gResumeTimer1, Resume Timer 1
Gui, Add, Button, x10 y120 w150 gStopTimer1, Stop Timer 1
Gui, Add, Button, x10 y160 w150 gResetTimer1, Reset Timer 1

; Timer 2
Gui, Add, Text, x170 y0 w150 Center vTimer2Text, 05:00 Время до свапа зоны
Gui, Add, Button, x170 y40 w150 gStartTimer2, Start Timer 2
Gui, Add, Button, x170 y80 w150 gResumeTimer2, Resume Timer 2
Gui, Add, Button, x170 y120 w150 gStopTimer2, Stop Timer 2
Gui, Add, Button, x170 y160 w150 gResetTimer2, Reset Timer 2

; Timer 3
Gui, Add, Text, x330 y0 w150 Center vTimer3Text, 01:00 Контроль трупов
Gui, Add, Button, x330 y60 w150 gStartTimer3, Start Timer 3
Gui, Add, Button, x330 y100 w150 gStopTimer3, Stop Timer 3
Gui, Add, Button, x330 y140 w150 gResetTimer3, Reset Timer 3

Gui, +AlwaysOnTop +ToolWindow -Caption +Border
Gui, Show, AutoSize x1280 y0

; Set transparency to 50% after showing the GUI
WinSet, Transparent, 128, ahk_class AutoHotkeyGUI

; Set the countdown durations
DefaultTimer1Seconds := 2 * 60
DefaultTimer2Seconds := 5 * 60
DefaultTimer3Seconds := 1 * 60
Timer1Seconds := DefaultTimer1Seconds
Timer2Seconds := DefaultTimer2Seconds
Timer3Seconds := DefaultTimer3Seconds

; Flags to control the timers
Timer1Running := false
Timer2Running := false
Timer3Running := false

; Ensure timers are off initially
SetTimer, UpdateTimer1, Off
SetTimer, UpdateTimer2, Off
SetTimer, UpdateTimer3, Off

; Set a timer to call ResetTimer1 after 1 second
SetTimer, AutoResetTimer1, -1000
return

StartTimer1:
    ; Start Timer 1 if it's not running
    if (!Timer1Running) {
        SetTimer, UpdateTimer1, 1000
        Timer1Running := true
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
        A_Min1 := A_Min + 02 
		FormattedTime01 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min, A_Sec)
		FormattedTime011 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min1, A_Sec)
        SendInput, /cb Вы заняли локацию в %FormattedTime01%, смена локации в %FormattedTime011%
    }
Return

StopTimer1:
    ; Stop Timer 1 if it's running
    if (Timer1Running) {
        SetTimer, UpdateTimer1, Off
        Timer1Running := false
        WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
        SendInput, /cb Таймер приостановлен, оставшееся время в локации %FormattedTime1%
    }
Return

ResumeTimer1:
    if (!Timer1Running) {
        SetTimer, UpdateTimer1, 1000
        Timer1Running := true
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
		A_Min1 := A_Min + Minutes1
		A_Sec1 := A_Sec + Seconds1
		FormattedTime0111 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min1, A_Sec1)
        SendInput, /cb Таймер возобновлён, смена локации в %FormattedTime0111%
    }
Return

StartTimer2:
    ; Start Timer 2 if it's not running
    if (!Timer2Running) {
        SetTimer, UpdateTimer2, 1000
        Timer2Running := true
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
        A_Min1 := A_Min + 05 
		FormattedTime02 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min, A_Sec)
		FormattedTime022 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min1, A_Sec)
		SendInput, /cb Вы заняли зону в %FormattedTime02%, смена зоны в %FormattedTime022% {Left 39}
    }
Return

StopTimer2:
    ; Stop Timer 2 if it's running
    if (Timer2Running) {
        SetTimer, UpdateTimer2, Off
        Timer2Running := false
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
        SendInput, /cb Таймер приостановлен, оставшееся время в зоне %FormattedTime2%
    }
Return

ResumeTimer2:
    if (!Timer2Running) {
        SetTimer, UpdateTimer2, 1000
        Timer2Running := true
		WinActivate, Majestic RP
        SendInput, {T}
        Sleep, 500
        A_Min1 := A_Min + Minutes2 
		A_Sec1 := A_Sec + Seconds2 
		FormattedTime0222 := Format("{:02}:{:02}:{:02}", A_Hour, A_Min1, A_Sec1)
        SendInput, /cb Таймер возобновлён, смена зоны в %FormattedTime0222%
    }
Return

StartTimer3:
    ; Start Timer 3 if it's not running
    if (!Timer3Running) {
        SetTimer, UpdateTimer3, 1000
        Timer3Running := true
    }
Return

StopTimer3:
    ; Stop Timer 3 if it's running
    if (Timer3Running) {
        SetTimer, UpdateTimer3, Off
        Timer3Running := false
    }
Return

ResetTimer1:
    ; Reset Timer 1 to its default duration
    Timer1Seconds := DefaultTimer1Seconds
    Minutes1 := Floor(Timer1Seconds / 60)
    Seconds1 := Mod(Timer1Seconds, 60)
    FormattedTime1 := Format("{:02}:{:02}", Minutes1, Seconds1)
    GuiControl,, Timer1Text, %FormattedTime1% Время до свапа локи
    ; Stop the timer if it's running
    if (Timer1Running) {
        SetTimer, UpdateTimer1, Off
        Timer1Running := false
    }
Return

ResetTimer2:
    ; Reset Timer 2 to its default duration
    Timer2Seconds := DefaultTimer2Seconds
    Minutes2 := Floor(Timer2Seconds / 60)
    Seconds2 := Mod(Timer2Seconds, 60)
    FormattedTime2 := Format("{:02}:{:02}", Minutes2, Seconds2)
    GuiControl,, Timer2Text, %FormattedTime2% Время до свапа зоны
    ; Stop the timer if it's running
    if (Timer2Running) {
        SetTimer, UpdateTimer2, Off
        Timer2Running := false
    }
Return

ResetTimer3:
    ; Reset Timer 3 to its default duration
    Timer3Seconds := DefaultTimer3Seconds
    Minutes3 := Floor(Timer3Seconds / 60)
    Seconds3 := Mod(Timer3Seconds, 60)
    FormattedTime3 := Format("{:02}:{:02}", Minutes3, Seconds3)
    GuiControl,, Timer3Text, %FormattedTime3% Контроль трупов
    ; Stop the timer if it's running
    if (Timer3Running) {
        SetTimer, UpdateTimer3, Off
        Timer3Running := false
    }
Return

UpdateTimer1:
    ; Update Timer1
    if (Timer1Running && Timer1Seconds > 0) {
        Timer1Seconds -= 1
        Minutes1 := Floor(Timer1Seconds / 60)
        Seconds1 := Mod(Timer1Seconds, 60)
        FormattedTime1 := Format("{:02}:{:02}", Minutes1, Seconds1)
        GuiControl,, Timer1Text, %FormattedTime1% Время до свапа локи
    } else if (Timer1Seconds <= 0 && Timer1Running) {
        GuiControl,, Timer1Text, Time's up! Время свапа локи
        SetTimer, UpdateTimer1, Off
        Timer1Running := false
    }
Return

UpdateTimer2:
    ; Update Timer2
    if (Timer2Running && Timer2Seconds > 0) {
        Timer2Seconds -= 1
        Minutes2 := Floor(Timer2Seconds / 60)
        Seconds2 := Mod(Timer2Seconds, 60)
        FormattedTime2 := Format("{:02}:{:02}", Minutes2, Seconds2)
        GuiControl,, Timer2Text, %FormattedTime2% Время до свапа зоны
    } else if (Timer2Seconds <= 0 && Timer2Running) {
        GuiControl,, Timer2Text, Time's up! Время свапа зоны
        SetTimer, UpdateTimer2, Off
        Timer2Running := false
    }
Return

UpdateTimer3:
    ; Update Timer3
    if (Timer3Running && Timer3Seconds > 0) {
        Timer3Seconds -= 1
        Minutes3 := Floor(Timer3Seconds / 60)
        Seconds3 := Mod(Timer3Seconds, 60)
        FormattedTime3 := Format("{:02}:{:02}", Minutes3, Seconds3)
        GuiControl,, Timer3Text, %FormattedTime3% Вспомогательный таймер`nконтроль трупов
    } else if (Timer3Seconds <= 0 && Timer3Running) {
        GuiControl,, Timer3Text, Time's up! Вспомогательный таймер`nконтроль трупов
        SetTimer, UpdateTimer3, Off
        Timer3Running := false
    }
Return

AutoResetTimer1:
    ; Automatically reset Timer 1 after 1 second
    Gosub, ResetTimer1
Return

4

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

Все 100500 строк нужны, чтобы продемонстрировать проблему? Сократите код до минимума, который покажет, что именно не так, но при этом нормально запустится.

Разработка AHK-скриптов:
e-mail dfiveg@mail.ru
Telegram jollycoder

5

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

Попробуй использовать FormatTime и EnvAdd.

Win 10 x64
AHK v1.1.33.02
                       Справка тебе в помощь.

6

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

Alectric
Если я правильно понял в FormatTime и EnvAdd используются только числа либо выражение, но не переменные, мне это не подходит.

7

Re: AHK: Сложение секунд и минут с сохранением формата времени 00:00:00

Можно и переменные. Я пользовался этими функциями здесь.

+ открыть спойлер
  TimeSub(Time1,Time2)
  {
    T1:=Time1[1]
    T2:=Time2[1]
    ms:=Time1[2]-Time2[2]
    if (ms<0)
    {
      ms+=1000
      T2+=1,Seconds
    }
    T1-=T2,Seconds
    return T1*1000+ms
  }
  TimeSubMs(Time,ValueMs)
  {
    if (ValueMs=0)
      return Time
    m:=0
    if (ValueMs>0)
    {
      ms:=Time[2]-mod(ValueMs,1000)
      if (ms<0)
      {
        ms+=1000
        m:=1
      }
      t:=Time[1]
      t+=-(Floor(ValueMs/1000)+m),Seconds
    }
    else
    {
      ms:=mod(-ValueMs,1000)+Time[2]
      if (ms>999)
      {
        ms-=1000
        m:=1
      }
      t:=Time[1]
      t+=(Floor(-ValueMs/1000)+m),Seconds
    }
    return [t,format("{:03}",round(ms))]
  }
  TimeAddMs(Time,ValueMs)
  {
    T:=Time[1]
    T+=ValueMs//1000,Seconds
    ms:=Time[2]+mod(ValueMs,1000)
    if (ms>999)
    {
      ms-=1000
      T+=1,Seconds
    }
    return [T,format("{:03}",round(ms))]
  }
  TimeToMs(Time)
  {
    FormatTime,HH,% Time,HH
    FormatTime,MM,% Time,mm
    FormatTime,SS,% Time,ss
    return HH*3600000+MM*60000+SS*1000
  }
  MsToTime(Ms)
  {
    T:=1601
    T+=Ms//1000,Seconds
    Ms:=format("{:03}",mod(Ms,1000))
    return [T,Ms]
  }

Выражение - это и есть переменные и их комбинации.

Win 10 x64
AHK v1.1.33.02
                       Справка тебе в помощь.