Community discussions

MikroTik App
 
jdg
just joined
Topic Author
Posts: 9
Joined: Tue Aug 06, 2013 4:20 pm

Change script to new time format [SOLVED]

Wed Feb 28, 2024 8:23 am

Hi, everyone:)
Who can help me to transform this script for delete expire hotspot users to new ISO time format(I made a mistake to update Router OS to 7.12.1 :D )?

:local dateint do={:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" );:local days [ :pick $d 4 6 ];:local month [ :pick $d 0 3 ];:local year [ :pick $d 7 11 ];:local monthint ([ :find $montharray $month]);:local month ($monthint + 1);:if ( [len $month] = 1) do={:local zero ("0");:return [:tonum ("$year$zero$month$days")];} else={:return [:tonum ("$year$month$days")];}};:local timeint do={:local hours [ :pick $t 0 2 ];:local minutes [ :pick $t 3 5 ];:return ($hours * 60 + $minutes) ;};:local date [ /system clock get date ];:local time [ /system clock get time ];:local today [$dateint d=$date] ;:local curtime [$timeint t=$time] ;:local tyear [ :pick $date 7 11 ];:local lyear ($tyear-1);:foreach i in [ /ip hotspot user find where comment~"/$tyear" || comment~"/$lyear" ] do={:local comment [ /ip hotspot user get $i comment]; :local limit [ /ip hotspot user get $i limit-uptime]; :local name [ /ip hotspot user get $i name]; :local gettime [:pic $comment 12 20];:if ([:pic $comment 3] = "/" and [:pic $comment 6] = "/") do={:local expd [$dateint d=$comment] ;:local expt [$timeint t=$gettime] ;:if (($expd < $today and $expt < $curtime) or ($expd < $today and $expt > $curtime) or ($expd = $today and $expt < $curtime) and $limit != "00:00:01") do={ :if ([:pic $comment 21] = "N") do={[ /ip hotspot user set limit-uptime=1s $i ];[ /ip hotspot active remove [find where user=$name] ];} else={[ /ip hotspot user remove $i ];[ /ip hotspot active remove [find where user=$name] ];}}}}}

Active hotspot users are with comment in format yyyy-mm-dd hh:mm:ss, where comment is expire time(for example 2024-03-04 18:59:40).
Before update comment was "name of mont/dd/yyyy hh:mm:ss" (for example mar/07/2024 16:25:44).
Last edited by jdg on Fri Mar 01, 2024 9:31 am, edited 1 time in total.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3346
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Change script to new time format

Wed Feb 28, 2024 2:11 pm

First of all, start by reformatting your ugly non readable one liner. You will never ever be able to see what is wrong in this mess.
:local dateint do={
	:local montharray ( "jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec" )
	:local days [ :pick $d 4 6 ]
	:local month [ :pick $d 0 3 ]
	:local year [ :pick $d 7 11 ]
	:local monthint ([ :find $montharray $month])
	:local month ($monthint + 1)
	:if ( [len $month] = 1) do={
		:local zero ("0")
		:return [:tonum ("$year$zero$month$days")]
	} else={
		:return [:tonum ("$year$month$days")]
	}
}
:local timeint do={
	:local hours [ :pick $t 0 2 ]
	:local minutes [ :pick $t 3 5 ]
	:return ($hours * 60 + $minutes) 
}
:local date [ /system clock get date ]
:local time [ /system clock get time ]
:local today [$dateint d=$date] 
:local curtime [$timeint t=$time] 
:local tyear [ :pick $date 7 11 ]
:local lyear ($tyear-1)
:foreach i in [ /ip hotspot user find where comment~"/$tyear" || comment~"/$lyear" ] do={
	:local comment [ /ip hotspot user get $i comment]
	:local limit [ /ip hotspot user get $i limit-uptime]
	:local name [ /ip hotspot user get $i name]
	:local gettime [:pic $comment 12 20]
	:if ([:pic $comment 3] = "/" and [:pic $comment 6] = "/") do={
		:local expd [$dateint d=$comment] 
		:local expt [$timeint t=$gettime] 
		:if (($expd < $today and $expt < $curtime) or ($expd < $today and $expt > $curtime) or ($expd = $today and $expt < $curtime) and $limit != "00:00:01") do={
			:if ([:pic $comment 21] = "N") do={
				[ /ip hotspot user set limit-uptime=1s $i ]
				[ /ip hotspot active remove [find where user=$name] ]
			} else={
				[ /ip hotspot user remove $i ]
				[ /ip hotspot active remove [find where user=$name] ]
			}
		}
	}
}
Same script, easier to read and understand.

There seems also to be one to may } at end of your script.

Script seems to be a copy of this one. viewtopic.php?t=188446
 
jdg
just joined
Topic Author
Posts: 9
Joined: Tue Aug 06, 2013 4:20 pm

Re: Change script to new time format [SOLVED]

Fri Mar 01, 2024 9:25 am

Solved:

:local dateint do={:local montharray ( "01","02","03","04","05","06","07","08","09","10","11","12" );:local days [ :pick $d 8 10 ];:local month [ :pick $d 5 7 ];:local year [ :pick $d 0 4 ];:local monthint ([ :find $montharray $month]);:local month ($monthint + 1);:if ( [len $month] = 1) do={:local zero ("0");:return [:tonum ("$year$zero$month$days")];} else={:return [:tonum ("$year$month$days")];}};:local timeint do={:local hours [ :pick $t 0 2 ];:local minutes [ :pick $t 3 5 ];:return ($hours * 60 + $minutes) ;};:local date [ /system clock get date ];:local time [ /system clock get time ];:local today [$dateint d=$date] ;:local curtime [$timeint t=$time] ;:local tyear [ :pick $date 0 4 ];:local lyear ($tyear-1);:foreach i in=[ /ip hotspot user find where comment~"$tyear" || comment~"$lyear" ] do={:local comment [ /ip hotspot user get $i comment]; :local limit [ /ip hotspot user get $i limit-uptime]; :local name [ /ip hotspot user get $i name]; :local gettime [:pic $comment 11 19];:if ([:pic $comment 4] = "-" and [:pic $comment 7] = "-") do={:local expd [$dateint d=$comment] ;:local expt [$timeint t=$gettime] ;:if (($expd < $today and $expt < $curtime) or ($expd < $today and $expt > $curtime) or ($expd = $today and $expt < $curtime) and $limit != "00:00:01") do={ :if ([:pic $comment 21] = "N") do={[ /ip hotspot user set limit-uptime=1s $i ];[ /ip hotspot active remove [find where user=$name] ];} else={[ /ip hotspot user remove $i ];[ /ip hotspot active remove [find where user=$name] ];}}}}}
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3346
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Change script to new time format [SOLVED]

Fri Mar 01, 2024 11:37 am

Again a bad formatted script, with same error as the last one. You NEED to remove one } at the end of the script.

Same script better formatted (without the } error).
:local dateint do={
	:local montharray ( "01","02","03","04","05","06","07","08","09","10","11","12" )
	:local days [ :pick $d 8 10 ]
	:local month [ :pick $d 5 7 ]
	:local year [ :pick $d 0 4 ]
	:local monthint ([ :find $montharray $month])
	:local month ($monthint + 1)
	:if ( [len $month] = 1) do={
		:local zero ("0")
		:return [:tonum ("$year$zero$month$days")]
	} else={
		:return [:tonum ("$year$month$days")]
	}
}
:local timeint do={
	:local hours [ :pick $t 0 2 ]
	:local minutes [ :pick $t 3 5 ]
	:return ($hours * 60 + $minutes) 
}
:local date [ /system clock get date ]
:local time [ /system clock get time ]
:local today [$dateint d=$date] 
:local curtime [$timeint t=$time] 
:local tyear [ :pick $date 0 4 ]
:local lyear ($tyear-1)
:foreach i in=[ /ip hotspot user find where comment~"$tyear" || comment~"$lyear" ] do={
	:local comment [ /ip hotspot user get $i comment]
	:local limit [ /ip hotspot user get $i limit-uptime]
	:local name [ /ip hotspot user get $i name]
	:local gettime [:pic $comment 11 19]
	:if ([:pic $comment 4] = "-" and [:pic $comment 7] = "-") do={
		:local expd [$dateint d=$comment] 
		:local expt [$timeint t=$gettime] 
		:if (($expd < $today and $expt < $curtime) or ($expd < $today and $expt > $curtime) or ($expd = $today and $expt < $curtime) and $limit != "00:00:01") do={
			:if ([:pic $comment 21] = "N") do={
				[ /ip hotspot user set limit-uptime=1s $i ]
				[ /ip hotspot active remove [find where user=$name] ]
			} else={
				[ /ip hotspot user remove $i ]
				[ /ip hotspot active remove [find where user=$name] ]
			}
		}
	}
}
 
jdg
just joined
Topic Author
Posts: 9
Joined: Tue Aug 06, 2013 4:20 pm

Re: Change script to new time format [SOLVED]

Fri Mar 01, 2024 12:30 pm

More important that it works;)