I found a bug about date - date

I am testing the date utility, and have found some unexpected behaviour. My test is to set the date to a given day/month/year, add 24 hours to it, and confirm that it returns the following date.
In my tests below, this works for November 4, 2017, and November 6, 2017, but not for November 5, 2017.
This is clearly a bug, isn't it? How could there be such a fundamental flaw in a key GNU utility?
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-04" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171105
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-06" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171107
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date=`date -d "2017-11-05" +%s`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date_new=`expr $date + 86400`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ req_t=`date -d #$date_new "+%Y%m%d"`
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ echo $req_t
20171105
[pp_dt_risk_batch#lvsdmetl05 creditsafe]$ date --version
date (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David MacKenzie.

It appears you are concerned with the apparent discrepancy suggested by the following (noting that 86400 seconds = 24 hours)
2017-11-04 00:00 + 86400 seconds = 2017-11-05 00:00
2017-11-05 00:00 + 86400 seconds = 2017-11-05 24:00 (see below!)
2017-11-06 00:00 + 86400 seconds = 2017-11-07 00:00
This is not a bug.
You don't mention what timezone you are in, but in most of North America, 2017-11-05 was the day on which Daylight Saving Time ended, so it was 25 hours long (90000 seconds).

Related

is there the way to get environment variable in HP-UX by pid

I want to know the method the way to get environment variable in HPUX from pid
by ps command, file, or programming.
it is possible to get variable
# /proc/$pid/environ in environ or ps e -ww -p $pid in linux
# ps ewww pid in aix
# pargs in solaris
HP-UX : use gdb to track but there is no gdb on a server(HPUX) and it's impossible to install it.
let me know that.
If you can install software onto this host, the latest HP-UX Linker, Libraries and Tools patch should give you the pargs(1) command:
[ hp-ux_ia64 sw ] $ /usr/ccs/bin/pargs -v
HP pstack/pldd/pargs version B.12.67 for HP Itanium(R) Systems.
[ hp-ux_ia64 sw ] $ /usr/ccs/bin/pargs -h
usage: pargs [-h] [-v] {-a pid | -e pid}
Given the pid of a running process, pargs prints process arguments and all
environment variables and its values.
pargs works by attaching to the process to read its memory.
[ hp-ux_ia64 sw ] $ ps -fu ranga
UID PID PPID C STIME TTY TIME COMMAND
ranga 9949 9923 0 Mar 17 pts/3 0:00 /usr/bin/sh /home/ranga/bin/tmux
ranga 16795 10007 0 10:40:06 pts/7 0:00 ssh hp-ux_ia64
ranga 9952 9949 0 Mar 17 pts/3 0:00 tmux
ranga 16538 16376 1 21:35:16 pts/4 0:00 ps -fu ranga
ranga 9918 9916 0 Mar 17 ? 0:04 sshd: ranga#pts/3
ranga 9954 1 2 Mar 17 ? 1:15 tmux
[ hp-ux_ia64 sw ] $ PHSS_44731/C-MIN/usr/ccs/bin/pargs -e 9949
SOCKS_CONF=/home/ranga/etc/socks.conf
MAIL=/var/mail/ranga
PATH=/usr/bin:/usr/ccs/bin:/usr/contrib/bin:/opt/langtools/bin:/usr/local/bin
PWD=/home/ranga
EDITOR=vim
TZ=IST-5:30
ERASE=^H
PS1=[ \h \W ] \$
SHLVL=1
SHELL=/usr/bin/bash
SFTP_PERMIT_CHMOD=1
HOME=/home/ranga
TERMINFO=/home/ranga/lib/terminfo
LOGNAME=ranga
SSH_CONNECTION=1.4.5.1 44584 1.2.2.2 22
SSH_CLIENT=1.1.0.6 44584 22
SHLIB_PATH=/home/ranga/local/lib
SFTP_UMASK=
_=/home/ranga/bin/tmux
USER=ranga
TERM=rxvt-256color
SOCKS5_SERVER=socks-server.ranga.com
LINES=70
Even if you can't install the patch, the pargs executable can be extracted from it and used.
If you can copy files out of this host, you could
use gcore(1) to generate a core file of the process
copy this core file along with the executable and the appropriate version of
libc (32-bit or 64-bit, use pldd(1) to confirm) to an environment
where gdb is available
use gdb to hack into the __envp string table
[ hp-ux-ia64 ~ ] $ ps -f
UID PID PPID C STIME TTY TIME COMMAND
ranga 5779 4411 0 13:12:47 pts/0 0:00 ps -f
ranga 4411 4403 0 12:45:42 pts/0 0:00 -bash
[ hp-ux-ia64 ~ ] $ pldd 4411
4411: /usr/bin/bash
/usr/bin/bash
/usr/lib/hpux32/dld.so
/usr/local/lib/hpux32/libtermcap.so
/usr/local/lib/hpux32/libintl.so
/usr/local/lib/hpux32/libiconv.so
/usr/lib/hpux32/libdl.so.1
/usr/lib/hpux32/libc.so.1
[ hp-ux-ia64 ~ ] $ gcore 4411
[ hp-ux-ia64 ~ ] $ gdb -q /usr/bin/bash core.4411
warning: Load module /usr/bin/bash has been stripped.
Debugging information is not available.
(no debugging symbols found)...Core was generated by `bash'.
(no debugging symbols found)...
warning: Load module /usr/local/lib/hpux32/libtermcap.so has been stripped.
Debugging information is not available.
(no debugging symbols found)...
#0 0x60000000c05660f0:0 in _waitpid_sys+0x30 () from /usr/lib/hpux32/libc.so.1
(gdb) x/s *(char**)__envp
0x200000007ffffeae: "USER=ranga"
(gdb)
:
0x200000007fffff45: "SSH_CLIENT=3.3.3.3 50072 22"
:
0x200000007fffffe4: "SFTP_PERMIT_CHOWN=1"
(gdb)
0x200000007ffffff8: ""

SSLError while installing opencv on raspi 3

I was trying to install OpenCV on my raspberry pi 3 and was following the tutorial:
https://www.learnopencv.com/install-opencv-4-on-raspberry-pi/
Everything was going well until the command:
sudo -H pip3 install -U pip numpy
After I executed the above command I got the following error(last part of the error):
request.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate','certificate verify failed')],)",)
I have been searching for a solution for 2-3 days now. Any help would be very well appreciated.
Ok, my problem is solved. In my case, my raspberry pi 3's date and time settings were incorrect and therefore SSL certificate error was coming.
I changed the date using the command (Time in 24hr format):
sudo date -s "Day Mon date hh:mm:ss Timezone Year"
e.g for IST use timezone= UTC+5:30
sudo date -s "Thu Aug 9 21:31:26 UTC+5:30 2019"

Changing a Unix server date with a scheduled job

I am looking for a way to schedule a set of commands on a unix server. The server time is in UTC. I essentially want to perform the below steps automatically every Wednesday at 4pm UK time:
Change the server date to the next time it is UK midnight (to avoid timezone change issues)
Restart the tomcat server
If tomcat is running, run a jar
Change the server date to the correct present UTC time
Restart the tomcat server
If tomcat is running, we are done
The below commands are what I currently run manually:
date -s "Thu Feb 09 00:01:00 UTC 2017" (represents the next day at 1 minute past midnight)
service tomcat restart
sudo -u tomcat java -jar Test.jar -type "Major" -status "Active"
date -s "<the current UTC time>"
service tomcat restart
I understand we can use cron to schedule the running of a script, but unsure how to do this. Any help is appreciated.
Create a file called called unix-server-date.sh and save it in /opt for example. The script will have this content:
#!/bin/bash
date -s "Thu Feb 09 00:01:00 UTC 2017" (represents the next day at 1 minute past midnight)
service tomcat restart
sudo -u tomcat java -jar Test.jar -type "Major" -status "Active"
date -s "<the current UTC time>"
service tomcat restart
Make the script executable:
chmod +x /opt/unix-server-date.sh
Then issue crontab -e to edit the crontab entries and add an entry like:
00 16 * * 3 /opt/unix-server-date.sh
Depending on your editor, after you have added the crontab entry please save the file and the crontab will be automatically installed.
That would be the basics!
If you run those commands from a specific user account you should add the cronjob to that user's crontab. Change crontab -e to crontab -e -u user

How to correct system clock in vagrant automatically

How to Correct Timezone
Last time, I figured out how to adjust a system clock in vagrant server. However, when I halt the vagrant and start it again, the system clock is always 9 hours late. I can adjust by using ntp command manually, but I'd like to know how to adjust the system clock automatically.
I have tried the below, but it still doesn't work. Are there any suggestions?
How to sync time on host wake-up within VirtualBox?
The method I use and it should not be provider specific is to add the following in my Vagrantfile
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime", run: "always"
you would need to replace '/Europe/Paris' with the timezone you want to set
The simplest way is to set the timezone automatically is to use the vagrant-timezone plugin.
Install it once with:
vagrant plugin install vagrant-timezone
After that, add the below to your Vagrantfile:
if Vagrant.has_plugin?("vagrant-timezone")
config.timezone.value = "UTC"
end
You may replace "UTC" with any of the tz values listed here.
For example: "Asia/Kolkata".
Or you can use your host's timezone with this entry in your Vagrantfile:
if Vagrant.has_plugin?("vagrant-timezone")
config.timezone.value = :host
end
Accepted answer is not robust enough, as it does not account for people who travel between timezones, and requires end users to modify Vagrantfile instead of just doing vagrant up.
Building up on Scott P.'s answer, here's a better more flexible solution that matches VM timezone to host's tz automatically. There's a typo/mistake in his snippet's Etc/GMT time zone selection, as per POSIX GMT+7 sets clock 7 hours behind (see Wiki explanation), hence we need to swap offsets:
Vagrant.configure("2") do |config|
require 'time'
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60)
timezone_suffix = offset >= 0 ? "-#{offset.to_s}" : "+#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/" + timezone + " /etc/localtime", run: "always"
end
I got:
[vagrant#ansiblecontrol ~]$ date -s \"$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z\"
date: extra operand ‘2018’
Try 'date --help' for more information.
This works for me:
sudo date -s "$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z"
Sun Apr 1 16:36:59 CEST 2018
So removed the "\" escape character.
A slightly improved version that auto-detects timezone:
The auto-detect portion came from here.
Vagrant.configure("2") do |config|
require 'time'
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60)
timezone_suffix = offset >= 0 ? "+#{offset.to_s}" : "#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix
config.vm.provision :shell, :inline => "sudo rm /etc/localtime && sudo ln -s /usr/share/zoneinfo/" + timezone + " /etc/localtime", run: "always"
end
My Vagrant Guest OS time was out of sync by 7 days. The above methods did not work for me, since Guest additions and ntp were not installed in my Guest machine.
I finally solved the issue by using the hack from
https://askubuntu.com/a/683136/119371
cfg.vm.provision "shell", inline: "date -s \"$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z\"", run: "always", privileged: true, upload_path: "/home/vagrant/tmp/vagrant-shell"
The above method does not sync the Guest OS time with your host machine or any NTP server. It sends an HTTP request to google.com and updates the system time with the time in the HTTP response header field.
Hence, depending on your internet connection speed and latency, the updated time could be off by several milliseconds to a few seconds (usually < 100ms). But it shouldn't matter for most cases.
Following is the curl version, if you don't want to use wget for any reason
cfg.vm.provision "shell", inline: "date -s \"$(curl -I google.com 2>&1 | grep Date: | cut -d' ' -f3-6)Z\""
#Benny K and #Scott P.'s solution giving me the negative value of a timezone, like in #rubo77's case. Worth to note that my host OS is Windows. If timedatectl is present on your guests (like Debian 9+), this is what I used to change timezone settings:
config.vm.provision "shell",
inline: "timedatectl set-timezone Europe/Budapest",
run: "always"
This one returns the expected timezone, not the negative value:
# before timedatectl
vagrant#master:~$ timedatectl
Local time: Fri 2020-07-03 11:52:31 -02
Universal time: Fri 2020-07-03 13:52:31 UTC
RTC time: Fri 2020-07-03 13:52:31
Time zone: Etc/GMT+2 (-02, -0200)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
# after timedatectl
vagrant#master:~$ timedatectl
Local time: Fri 2020-07-03 15:53:24 CEST
Universal time: Fri 2020-07-03 13:53:24 UTC
RTC time: Fri 2020-07-03 13:53:24
Time zone: Europe/Budapest (CEST, +0200)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
Based on #Benny K.'s answer (https://stackoverflow.com/a/46778032/3194807), with the daylight saving time taken into account:
require "time"
offset = ((Time.zone_offset(Time.now.zone) / 60) / 60) + (Time.now.dst? ? 1 : 0)
timezone_suffix = offset >= 0 ? "-#{offset.to_s}" : "+#{offset.to_s}"
timezone = 'Etc/GMT' + timezone_suffix
tzShellProvision = <<_SHELL_
ln -fs /usr/share/zoneinfo/#{timezone} /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
_SHELL_
default.vm.provision :shell, inline: tzShellProvision, run: "always"
The way is to set the timezone automatically same like host using
the vagrant-timezone plugin.
Install the vagrant-timezone plugin with
vagrant plugin install vagrant-timezone
After that, add the below to your Vagrantfile
config.timezone.value = :host
Note that this functionality has only been tested with an OS X host and Linux guest.

unix date format converting HKT or JST to EST timezone

While converting the date to +%-m/%-d/%Y using date format in unix, I'm getting dates converted to timezones. HKT or JST timezones to EST, I want the date as is to be converted as mm/dd/yy without this timezone conversion
Example:
$ date -u '--d=Sun Jan 25 08:39:12 JST 2015' +%-m/%-d/%Y
1/24/2015
I also tried with -u -d options
$ date -u -d 'Sun Jan 25 08:39:12 JST 2015' +%-m/%-d/%Y
1/24/2015
Can someone please help me with the solution.
To change the output timezone, you could set TZ envvar:
$ TZ=EST date -d 'Sun Jan 25 08:39:12 JST 2015'
Sat Jan 24 18:39:12 EST 2015
$ TZ=JST date -d 'Sun Jan 25 08:39:12 JST 2015'
Sun Jan 25 08:39:12 JST 2015
To ignore the input timezone or to detect it automatically, a Python script could be used:
#!/usr/bin/env python
"""Change date/time format.
Usage: $ {prog} <input-date-time> <output-format>
Example: $ ./change-date-format 'Sun Jan 25 08:39:12 JST 2015' %-m/%-d/%Y
Output: 1/25/2015
"""
import sys
from dateutil.parser import parse # apt-get install python-dateutil
try:
date_string, date_format = sys.argv[1:]
except ValueError:
sys.exit(__doc__.format(prog=sys.argv[0])) # print usage info
else:
datetime_object = parse(date_string)
print(datetime_object.strftime(date_format))