How to display date as YYYYMMDD_HH24 using Solaris shell command? - solaris

20110216_00
20110216_01
...
20110216_23
20110217_00
..
and so on
I have tried with
date +'%Y%m%d_%H'
but it never starts with 00-23 format but from 01-24 like format, hence I get hour part always incorrect.
Can anybody suggest, how can I get above o/p

You can do it by manupulating the hour part. Check the snip below.
#/bin/ksh
s=`date +'%Y%m%d_'`
t=` date +'%H'`
let t=$t+1
echo "Required date is " $s$t
It gives
Required date is 20110316_16

I tried it on SunOS 5.10 This works !
date +%Y%m%d_%H
-> 20130912_02
date +'%Y%m%d_%H'
-> 20130912_02
Can you tell us which Solaris are you using ?
uname -a
Cheers !

What revision of Solaris are you using? It is roughly 22:30 locally and I see:
mph#sol11express:~$ date +'%Y%m%d_%H'
20110216_22
mph#sol11express:~$ uname -a
SunOS sol11express 5.11 snv_151a i86pc i386 i86pc Solaris
mph#sol11express:~$ echo $SHELL
/bin/bash
which looks to me like it is using 0-23 for hours.

you can use date -u +'%Y-%m-%d-%H'

Related

Read and interpret crontab with Perl

I'm writing a Perl program to check if and make sure that some system backup tasks were executed from crontab.
I need to read the crontab and interpret when it was supposed to run, in order to check if that backup was done. Here is an example.
00 03 * * 6 system_backup.sh
Let's suppose this task will generate a file called system_backup_20180510.iso
Then my idea was to store a "desirable date" into a var then compare with that date in the filename. The biggest issue is how to build that desirable date with crontab's day of month field filled up.
What you guys can suggest? Thanks
Note the 5th field: this only runs on Saturday.
Here's something you can do with GNU date:
$ date -d "-1 week saturday 03:00" "+%Y%m%d %H:%M:%S"
20180505 03:00:00
$ date -d "-w week saturday 03:00" "+%Y%m%d %H:%M:%S"
20180518 13:00:00
$ date -d "-2 week saturday 03:00" "+%Y%m%d %H:%M:%S"
20180428 03:00:00
the Schedule::Cron::Events suggestion implemented:
crontab -l | perl -MPOSIX=strftime -MSchedule::Cron::Events -lne '
$cron = Schedule::Cron::Events->new($_);
$prev = strftime("%F %T", $cron->previousEvent());
print "$prev => ", $cron->commandLine();
'

Convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, without GNU date

Could someone please suggest a simple and short approach to convert "Thu Sep 22 3:50 2016" to "2016-09-22" in Solaris, through a shell script?
I do not have GNU date available on Solaris as discussed in below post:
Convert date String to number on Solaris shell script gives No such file or directory
I need to query an sql server db, where date is saved in the format, "2016-09-06", hence I need to convert it
Actually, you do have GNU date available but here is anyway one way to achieve this by scripting:
#!/bin/ksh
a="Thu Sep 22 3:50 2016"
echo $a | nawk '
BEGIN {
m=1
m2m["Jan"]=m++;
m2m["Feb"]=m++;
m2m["Mar"]=m++;
m2m["Apr"]=m++;
m2m["May"]=m++;
m2m["Jun"]=m++;
m2m["Jul"]=m++;
m2m["Aug"]=m++;
m2m["Sep"]=m++;
m2m["Oct"]=m++;
m2m["Nov"]=m++;
m2m["Dec"]=m++;
}
{
printf("%s-%02d-%02d\n",$5,m2m[$2],$3)
}'
output:
2016-09-22
Why not use Oracle's sysdate?
select * from your_table where saved_date >= to_char(sysdate,'yyyy-MM-dd')

Sed add text after match

I have a xmltv file that has the following style lines for program start/stop times
<programme start="20150914003000" stop="20150914020000" channel="Noor TV">
I want to add +0000 to the end of the start/stop time like the following
<programme start="20150914003000 +0000" stop="20150914020000 +0000" channel="Noor TV">
I am using windows sed and got this far
sed -r "/<programme start=\"/ s/^([0-9]{14})/\1 +0000/g" < "xml.xml" > "xml2.xml"
its giving me sed cant read >: invalid argument
in the dos windows I can see its adding the +0000 but not writing the new file
I know its something dumb but I just cant figure it out.
thks.
Try this
sed -r "/<programme start=/ s/^([0-9]{14})/\1 +0000/g" "xml.xml" > "xml2.xml"
or (posix version)
sed "/<programme start=/ s/^([0-9]\{14\})/\1 +0000/g" "xml.xml" > "xml2.xml"
$cat xml.xml
<programme start="20150914003000" stop="20150914020000" channel="Noor TV">
$sed -r 's/start="([0-9]{14})" stop="([0-9]{14})"/start="\1 +0000" stop="\2 +0000"/' xml.xml >xml2.xml
$cat xml2.xml
<programme start="20150914003000 +0000" stop="20150914020000 +0000" channel="Noor TV">
had tried it by online linux
The first < needs a backslash, not a forward slash.
sed -r "\<programme start=\"/ s/^([0-9]{14})/\1 +0000/g" < "xml.xml" > "xml2.xml"

Setting Date format for autoprop 'DATE' in SVN

I have some files that use the autoproperty $Date of SVN.
When some colleagues in France do a checkout of the repository, the date is in French. When they do the verification of checksum, these files are KO because of this difference of format for the date.
Note: we are using Windows and our client of choice is TortoiseSVN but are open to use command line clients.
Question 1: Is there any way to force the format for the date during the checkout?
We tried the following:
Setting "English" in Tortoise SVN
Setting environment variable LANG to EN_US and doing a checkout with both TortoiseSVN and svn commandline
None of these solutions is working.
Question 2: Would the time zone impact the date in the header as well?
Thanks
NB: This is the header of our source code, for what it matters.
/*==============================================================================================
* FILENAME : Source.h
* VERSION : $Revision: 85911 $
* MODIFICATION DATE : $Date: 2015-06-12 18:26:22 +0800 (Fri, 12 Jun 2015) $
*============================================================================================*/
A1: I don't know, how to manipulate locale-setting in Windows in easy and automated style - inspired by this old answer in Subversion maillist - (better to ask it SU) and can suggest now only dirty hack: import reg-files for FR and EN locales before and after checkout (how to prepare: switch to FR in Control Panel, save Current Control Set part of registry, return to EN, save CCS again, leave in .reg only mutable part), checkout in bat-file only (change locale around checkout)
A2: Sad, but yes
$Date: 2015-06-19 19:45:22 +0500 (Пт, 19 июн 2015) $
inserts not only language-specific date (trailing part), but also TZ of client
1.8.*-specific solution: you can create and use replacement for $Date keyword, which use UTC-time instead of local (as $Id do for time-part now) with %d variable it it
>svn pl -v file.txt
Properties on 'file.txt':
svn:keywords
Author Date Id Revision URL Header IntDate=%d
and IntDate will expand location-independent
$IntDate: 2015-06-19 14:45:22Z $

perl DateTime incorrect timezone offset

I have several servers running under centos 6.3 and I faced issue that perl module DateTime treats Europe/Moscow timezone as UTC+3
[ulan#rt-virtual ~]$ perl -MDateTime -e 'print DateTime->now()->set_time_zone("Europe/Moscow"), "\n";'
2013-12-19T11:11:38
but in fact it is UTC+4 and system tools like zdump or date work correctly
[ulan#rt-virtual ~]$ zdump Europe/Moscow
Europe/Moscow Thu Dec 19 12:11:47 2013 MSK
I updated tzdata and DateTime module but it didn't help.
How can I amend this?
Thanks.
Well, DateTime module is doing its magic by following the rules specified in the TimeZone modules specific for each timezone. For Europe/Moscow, the module's is DateTime::TimeZone::Europe::Moscow. The problem is all the files are generated automatically corresponding to the rules existing when a specific version of DateTime module is released.
In this case one very important change - Russia's stopping following DST routines in 2011 - wasn't obviously reflected in that file. So updating - either the whole module or only the relevant TimeZone part - should have fixed the issue.
You can use your systems tzfile(5), using DateTime::TimeZone::Tzfile. Not only does it perform better than DateTime::TimeZone it also removes the need to have redundant data that needs to be in sync.
$tz = DateTime::TimeZone::Tzfile->new('/etc/localtime');
$dt = DateTime->now(time_zone => $tz);