Setting the clock on ESP-12F with micropython, will give the wrong time - micropython

Connect the ESP-12F to the Linux machine on USB0 port
From Linux console login into the ESP-12F
$ picocom /dev/ttyUSB0 -b115200
Try to set the clock under micropython
>>> import utime
>>> import machine
>>> time=(2021, 11, 21, 23, 42, 0, 6, 325)
>>> machine.RTC().datetime(time)
>>> utime.localtime()
(2021, 11, 22, 18, 0, 11, 0, 326)
As you can see, the time changed and consequently the day as well.
If I understand ~19 hours was added to the time what I wanted.
Can you tell me what did I wrong here?
Is not the right way to set the time? (I do not want to syc, I just want to set)

I found the answer for my question.
...The documentation is wrong... Parameter 4 is the day of week..."
The 8-tuple has the following format: (year, month, day, weekday,
hours, minutes, seconds, sub-seconds)
(0) year includes the century (for example 2020)
(1) month is 1-12
(2) mday is 1-31 or yday 1-365/366
(3) weekday is 0-6 for Mon-Sun ***Set to 0, being Auto Set by machine
(4) hour is 0-23
(5) minute is 0-59
(6) second is 0-59
(7) sub-seconds

Related

Alert for an action every 85 days

I am writing a script that shows the amount of days left until we should change a password. The problem is that right now, we are about 11 days until the password needs to change. I'm struggling with the logic to script this out. The beginning of the 85 day count down was May 16, 2021. The next 85 days starts August 9, 2021. I would like the days to reset after 85. I'm honestly not even sure how to start this. Maybe a for-loop that returns 85 - $currentDate and then resets after 85 but I'm not sure how to get it to start today in the middle of the 85 days.
In your script, you can create a starting date of May 16, 2021 and increment that date by 85 days as needed:
# Original Start Date at 12:00 AM
$85Date = [datetime]'May 16, 2021'
# Today's date
$Current = Get-Date
# checks if today is past the 85 day date
# if you didn't change the password with 0 days remaining, this logic fails
while ($Current.Date -gt $85Date) {
# increments $85Date by 85 days
$85Date = $85Date.AddDays(85)
}
# check when there are 0 days remaining
if ($Current.Date -eq $85Date) {
# Send alert here for 0 days remaining
}
# output how many days remain
'{0} days remaining until password expiration' -f ($85Date - $current.Date).Days
The code as it is won't know if the password was updated unless you add that logic. It will just assume you waited until day 85 to reset it each time. To add more intelligence, you would need to then do one of the following:
Add logic that tests the password was reset
Have the password reset update an attribute on the account that can be read from PowerShell. That date can be used instead of today.
Update some other artifact like a database table or file with the password change date. Then have the script read that each time it runs.
There are going to be many alternatives to making this work. As of now, we do not know what service provides the account, what mechanism of alerting will be used, or what the limitations are for a PowerShell solution in your environment.

looking for spark scala(java) code for date string with spaces in between with specific conditions

need some suggestions on below requirement.
Each response help a lot thanks in advance....
I have a date of type String with timestamp ex: Jan 8 2019 4:44 AM
My requirement is if the date is single digit I want date to be 1 space and digit
(ex: 8) and if the date is 2 digits which is dates from 10 to 31 I want date with no space(ex:10) and also same for hour in timestamp.
to summarize: if the date is 1 to 9 and hour in timestamp is 1 to 9 looking for below string
Jan 8 2019 4:44 AM
if the date is 10 to 31 and hour in timestamp is 10 to 12 looking for below string
Jan 18 2019 12:44 AM
right now I am creating a date in following way:
val sdf = new SimpleDateFormat("MMM d yyyy h:mm a")
but the above format satisfies only one condition which is dates from 1 to 9.
my application is spark with scala so looking for some spark scala code or java.
I appreciate your help...
Thanks..
java.time
Use p as a pad modifier in the format pattern string. In Java syntax (sorry):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"MMM ppd ppppu pph:mm a", Locale.ENGLISH);
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 8, 4, 44)
.format(formatter));
System.out.println(LocalDateTime.of(2019, Month.JANUARY, 18, 0, 44)
.format(formatter));
Jan 8 2019 4:44 AM
Jan 18 2019 12:44 AM
And do yourself the favour: Forget everything about the SimpleDateFormat class. It is notoriously troublesome and fortunately long outdated. Use java.time, the modern Java date and time API.
Link: Oracle tutorial: Date Time explaining how to use java.time.
To quote the DateTimeFormatter class documentation:
Pad modifier: Modifies the pattern that immediately follows to be padded with spaces. The pad width is determined by the number of pattern letters. This is the same as calling DateTimeFormatterBuilder.padNext(int).
For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.

Cron job to run on every LAST Friday of every month [duplicate]

I am using Bash on RedHat. I need to schedule a cron job to run at at 9:00 AM on first Sunday of every month. How can I do this?
You can put something like this in the crontab file:
00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script
The date +%d gives you the number of the current day, and then you can check if the day is less than or equal to 7. If it is, run your command.
If you run this script only on Sundays, it should mean that it runs only on the first Sunday of the month.
Remember that in the crontab file, the formatting options for the date command should be escaped.
It's worth noting that what looks like the most obvious approach to this problem does not work.
You might think that you could just write a crontab entry that specifies the day-of-week as 0 (for Sunday) and the day-of-month as 1-7, like this...
# This does NOT work.
0 9 1-7 * 0 /path/to/your/script
... but, due to an eccentricity of how Cron handles crontab lines with both a day-of-week and day-of-month specified, this won't work, and will in fact run on the 1st, 2nd, 3rd, 4th, 5th, 6th, and 7th of the month (regardless of what day of the week they are) and on every Sunday of the month.
This is why you see the recommendation of using a [ ... ] check with date to set up a rule like this - either specifying the day-of-week in the crontab and using [ and date to check that the day-of-month is <=7 before running the script, as shown in the accepted answer, or specifying the day-of-month range in the crontab and using [ and date to check the day-of-week before running, like this:
# This DOES work.
0 9 1-7 * * [ $(date +\%u) = 7 ] && /path/to/your/script
Some best practices to keep in mind if you'd like to ensure that your crontab line will work regardless of what OS you're using it on:
Use =, not ==, for the comparison. It's more portable, since not all shells use an implementation of [ that supports the == operator.
Use the %u specifier to date to get the day-of-week as a number, not the %a operator, because %a gives different results depending upon the locale date is being run in.
Just use date, not /bin/date or /usr/bin/date, since the date utility has different locations on different systems.
You need to combine two approaches:
a) Use cron to run a job every Sunday at 9:00am.
00 09 * * 7 /usr/local/bin/once_a_week
b) At the beginning of once_a_week, compute the date and extract the day of the month via shell, Python, C/C++, ... and test that is within 1 to 7, inclusive. If so, execute the real script; if not, exit silently.
A hacky solution: have your cron job run every Sunday, but have your script check the date as it starts, and exit immediately if the day of the month is > 7...
This also works with names of the weekdays:
0 0 1-7 * * [ "$(date '+\%a')" == "Sun" ] && /usr/local/bin/urscript.sh
But,
[ "$(date '+\%a')" == "Sun" ] && echo SUNDAY
will FAIL on comandline due to special treatment of "%" in crontab (also valid for https://stackoverflow.com/a/3242169/2919695)
Run a cron task 1st monday, 3rd tuesday, last sunday, anything..
http://xr09.github.io/cron-last-sunday/
Just put the run-if-today script in the path and use it with cron.
30 6 * * 6 root run-if-today 1 Sat && /root/myfirstsaturdaybackup.sh
The run-if-today script will only return 0 (bash value for True) if it's the right date.
EDIT:
Now with simpler interface, just one parameter for week number.
# run every first saturday
30 6 * * 6 root run-if-today 1 && /root/myfirstsaturdaybackup.sh
# run every last sunday
30 6 * * 7 root run-if-today L && /root/lastsunday.sh
There is a hacky way to do this with a classic (Vixie, Debian) cron:
0 9 1-7 * */7
The day-of-week field starts with a star (*), and so cron considers it "unrestricted" and uses the AND logic between the day-of-month and the day-of-week fields.
*/7 means "every 7 days starting from weekday 0 (Sunday)". Effectively, this means "every Sunday".
Here's my article with more details: Schedule Cronjob for the First Monday of Every Month, the Funky Way
Note – it's a hack. If you use this expression, make sure to document it to avoid confusion later.
maybe use cron.hourly to call another script. That script will then check to see if it's the first sunday of the month and 9am, and if so, run your program. Sounds optimal enough to me :-).
If you don't want cron to run your job everyday or every Sunday you could write a wrapper that will run your code, determine the next first Sunday, and schedule itself to run on that date.
Then schedule that wrapper for the next first Sunday of the month. After that it will handle everything itself.
The code would be something like (emphasis on something...no error checking done):
#! /bin/bash
#We run your code first
/path/to/your/code
#now we find the next day we want to run
nskip=28 #the number of days we want to check into the future
curr_month=`date +"%m"`
new_month=`date --date='$nskip days' +"%m"`
if [[ curr_month = new_month ]]
then
((nskip+=7))
fi
date=`date --date='$nskip days' +"09:00AM %D` #you may need to change the format if you use another scheduler
#schedule the job using "at"
at -m $date < /path/to/wrapper/code
The logic is simple to find the next first Sunday. Since we start on the first Sunday of the current month, adding 28 will either put us on the last Sunday of the current month or the first Sunday of the next month. If it is the current month, we increment to the next Sunday (which will be in the first week of the next month).
And I used "at". I don't know if that is cheating. The main idea though is finding the next first Sunday. You can substitute whatever scheduler you want after that, since you know the date and time you want to run the job (a different scheduler may need a different syntax for the date, though).
try the following
0 15 10 ? * 1#1
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
00 09 1-7 * 0 /usr/local/bin/once_a_week
every sunday of first 7 days of the month

Why different dates appear the same in JavaScript?

<script type="text/javascript">
alert(new Date(2010,8,31));
alert(new Date(2010,9,1));
</script>
Try the code above. The browser display the same date in both message. Why???
Date(2010,8,31) means "October 1, 2010" and
Date(2010,9,1) also means "October 1, 2010"
Because
in Date(yyyy,mm,dd), mm can be set from 0 to 11 not from 1 to 12
so that if mm is 8 means august and august have 30 days.
on this case, if you input 31 in dd, it points "August 30" + 1
Did you actually look at the alert? It displays a date in october. The months are zero-based. This means your first line is actually September 31 - which does not exist, and is wrapped to the next day, October 1. Your second line is also October 1.
Because javascript months are 0 based, like 0=Jan, 1=Feb
Since September 30 is the last day of the month, javascript corrects it to October 1.

Why does Perl's DateTime::Astro::Sunrise give me unexpected values?

Using the example code included in the man page for DateTime::Astro::Sunrise, I'm getting back ~14:00 for the sunrise and ~2:00 for the sunset.
My machine's time and timezone are set correctly (AFAIK).
Am I reading something wrong? 2am and 2pm are just so brutually wrong.
use DateTime;
use DateTime::Astro::Sunrise;
my $dt = DateTime->new( year => 2010,
month => 3,
day => 15,
);
my $sunrise = DateTime::Astro::Sunrise ->new('-117','33',undef,1);
my ($tmp_rise, $tmp_set) = $sunrise->sunrise($dt);
printf "%s\n", $tmp_rise;
printf "%s\n", $tmp_set;
At a guess, you've got the wrong sign on your longitude, so you're getting the sunrise/sunset times for Shanghai (which are about 6AM and 6PM Shanghai time right now), but you're getting the times in California time that the sun rises and sets in Shanghai, since that's your local timezone. Difference is 16 hours, so you get 2PM and 2AM.
Sorry, without any sample code, I can't say what you are doing wrong.
An alternative, Astro::Sunrise has been working well for me, and appears to be slightly more mature.