Community discussions

MikroTik App
 
ColinSlater
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 59
Joined: Sun Sep 12, 2021 2:32 pm

Date Parts --> Weekday?

Thu Dec 08, 2022 9:15 am

Good Morning Mikrotik Community,
Does anyone know if there's a way to get weekday (either as a number or a word, I don't really care at this stage) from the current date in a MikroTik script.
In my current automatic backup script, I have:
:local theDate [/system clock get date]
:local theDay [ :pick \$theDate 4 6 ]
I can see that the :pick just extracts parts of the date string.

At the moment, my automatic backup script just creates a new config export every day and then I have a small .NET program running on server on our network which logs in with SSH and downloads it.
What I would like to have is something a bit more like the backup regime that I have for other systems:
  • A backup is taken every day, but these are only kept for 1 week, i.e. the Monday config export would be overwritten every Monday, the Tuesday overwritten every Tuesday etc.
  • Additionally, a backup is taken every Monday, but this time kept for 1 week, and the files are recycled every month, giving me Week1, Week2 etc.
  • Additionally, a backup is taken on the 1st of every month, and these backups are kept for 1yr before being recycled.
I can work out how to do the "1st of the Month" bit, but can't find anything in the help.mikrotik or wiki.mikrotik on how to get the Weekday out of the date.

If it's not possible to get Weekday, then I will just edit the script to do a daily backup each day of the month and then one on the first of the month and recycle these files acordngly, as I can work out how to do this from what I've already got.

Any help gratefully received.

Thanks

Colin
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date Parts --> Weekday?  [SOLVED]

Thu Dec 08, 2022 10:43 am

viewtopic.php?f=9&t=75555#p876568

You must calculate the weekday starting from one fixed date you know the right value, and calculating the day passed since that date,
including for each month add the right number of days, and 29 feb when is needed...
{
/system clock
:local strDate  [get date]
:local intYear  [:tonum [:pick $strDate 7 11]]
:local strMonth [:pick $strDate 1 3]
:local intDay   ([:tonum [:pick $strDate 4 6]] - 1)

:local arrPreMonDays {an=0;eb=31;ar=59;pr=90;ay=120;un=151;ul=181;ug=212;ep=243;ct=273;ov=304;ec=334}
# 1970-01-01 is a Thursday
:local arrWeekDays   {"Thu";"Fri";"Sat";"Sun";"Mon";"Tue";"Wed"}
# first bixestile year just before 1970 is 1968
:local numTotalDays  (($intYear - 1968) / 4)
:if ((($intYear - 1968) % 4) = 0) do={:set ($arrPreMonDays->"an") -1; :set ($arrPreMonDays->"eb") 30}
:set numTotalDays  ($numTotalDays + (($intYear - 1970) * 365))
:set numTotalDays  ($numTotalDays + ($arrPreMonDays->$strMonth))
:set numTotalDays  ($numTotalDays + $intDay)
:local strWeekDay ($arrWeekDays->($numTotalDays % 7))
:put "Week Day is $strWeekDay"
}
(EDIT later: upgraded version of this script viewtopic.php?p=960070#p960070 )

This calculation isn't accurate before Jan/01/1970, because I took this snippet from the code that calculates the unix epoch,
and it isn't accurate after Feb/28/2100 because I didn't consider the correction for leap day on century.


REFRESHED for 7.10+
viewtopic.php?t=191520#p1010947
Full version of this script: viewtopic.php?p=960070#p960070
Last edited by rextended on Sun Jul 02, 2023 2:00 pm, edited 6 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 1:42 pm

Hi,

The forum administrators should enable a like on each post to let the authors know that we like (and like a lot) these helpful posts.

Thank you for your contributions @rextended, @Amm0, @eworm, @Jotne, etc..

BR.
 
User avatar
normis
MikroTik Support
MikroTik Support
Posts: 26287
Joined: Fri May 28, 2004 11:04 am
Location: Riga, Latvia

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 4:05 pm

There is a button for this. Next to the post, see Checkmark icon
 
ColinSlater
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 59
Joined: Sun Sep 12, 2021 2:32 pm

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 4:19 pm

Thanks very much everyone.
Very helpful as always.

Colin
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 4:43 pm

There is a button for this. Next to the post, see Checkmark icon
I don't see it :(

Image

BR.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3279
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 5:45 pm

In "ProSilver" I can Quote or Report a post, not like.
.
post.png
You do not have the required permissions to view the files attached to this post.
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Date Parts --> Weekday?

Thu Dec 08, 2022 5:58 pm

+1

BR.
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Date Parts --> Weekday?

Sun Jul 02, 2023 2:35 am

Hi @rextended,

Since the release of ROS v7.10 the date changed to 2023-07-02 format. So this function should change the months array, right?
Would it be something like this?
/system clock
:local strDate  [get date]
:local intYear  [:pick $strDate 0 4]
:local strMonth [:pick $strDate 5 7]
:local intDay   [:pick $strDate 8 10]

:local arrPreMonDays {01=0;02=31;03=59;04=90;05=120;06=151;07=181;08=212;09=243;10=273;11=304;12=334}
# 1970-01-01 is a Thursday
:local arrWeekDays   {"Thu";"Fri";"Sat";"Sun";"Mon";"Tue";"Wed"}
# first bixestile year after 1970 is 1972
:local numTotalDays  (($intYear - 1972) / 4)
:if ((($intYear - 1972) % 4) = 0) do={:set ($arrPreMonDays->01) -1; :set ($arrPreMonDays->02) 30}
:set numTotalDays  ($numTotalDays + (($intYear - 1970) * 365))
:set numTotalDays  ($numTotalDays + ($arrPreMonDays->$strMonth))
:set numTotalDays  ($numTotalDays + $intDay)
:local strWeekDay  ($arrWeekDays->($numTotalDays % 7))
:put "Week Day is $strWeekDay"
I have tested it and it works fine.

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date Parts --> Weekday?

Sun Jul 02, 2023 1:45 pm

The full version, which also indicates, for example, the day of the year (from 1 to 365/366), how many days there are in the month (from 28 to 31),
current day of week number from 1 (mon) to 7 (sun), if the yoear is a leap year or not, etc...
viewtopic.php?p=960070#p960070



This work regardless the RouterOS version 6, 7 and 7.10:

extracted function code

:global getweekday do={
    :local vdate     $1
    :local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local daysOnMnt [:toarray "0,31,28,31,30,31,30,31,31,30,31,30,31"]
    :local vdoff     [:toarray "0,4,5,7,8,10"]
    :local M         [:tonum [:pick $vdate ($vdoff->2) ($vdoff->3)]]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
    :if ((($yyyy - 1968) % 4) = 0) do={:set ($prMntDays->1) -1; :set ($prMntDays->2) 30; :set ($daysOnMnt->2) 29}
    :local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($prMntDays->$M) + ([:tonum [:pick $vdate ($vdoff->4) ($vdoff->5)]] - 1))
    :return ([:toarray "Thu,Fri,Sat,Sun,Mon,Tue,Wed"]->($totd % 7))
}

:put [$getweekday [/sys clock get date]]
Last edited by rextended on Sun Jul 02, 2023 2:11 pm, edited 3 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Date Parts --> Weekday?

Sun Jul 02, 2023 2:02 pm

Oh, very complete. Thanks Rex!!
Happy summer!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Date Parts --> Weekday?

Sun Jul 02, 2023 2:12 pm

Oh, very complete. Thanks Rex!!
Happy summer!
Thanks, also to you.

On the previous post "extracted function code" is missing one ":tonum": fixed.

Who is online

Users browsing this forum: ko00000000001 and 17 guests