I pulled the following code from one of Jim's threads. Since my server time is GMT, I need to make sure the output is GMT-4. Does anyone know how to tweak Perl to reflect this?
Thanks
Scott
#!/bin/ksh
# input format YYYY-MM-DD prints day name
dow()
{
perl -e '
use POSIX qw(strftime);
$fmt = "%A"; # %a = abbreviated weekday %u = weekday number
$mday = substr("$ARGV[0]", 8, 2);
$mon = substr("$ARGV[0]", 5 ,2);
$year = substr("$ARGV[0]", 0 ,4);
$weekday =
strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
print "$weekday";
' "$1"
}
echo "$(dow `date "+%Y-%m-%d"` )"
This is what the TZ variable does
#!/bin/ksh
# input format YYYY-MM-DD prints day name
dow()
{
perl -e '
use POSIX qw(strftime);
$fmt = "%A"; # %a = abbreviated weekday %u = weekday number
$mday = substr("$ARGV[0]", 8, 2);
$mon = substr("$ARGV[0]", 5 ,2);
$year = substr("$ARGV[0]", 0 ,4);
$weekday =
strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
print "$weekday";
' "$1"
}
for foo in -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11
do
FOO="FOO"${foo}
export TZ=$FOO
echo "TZ=${FOO}: `date`"
echo "$(dow `date "+%Y-%m-%d"` )"
done
echo '***************** GMT -4'
export TZ=GMT-4
echo "TZ=${TZ}: `date`"
echo "$(dow `date "+%Y-%m-%d"` )"
So, if you do this
dow()
{
export TZ=GMT-4
perl -e '
use POSIX qw(strftime);
$fmt = "%A"; # %a = abbreviated weekday %u = weekday number
$mday = substr("$ARGV[0]", 8, 2);
$mon = substr("$ARGV[0]", 5 ,2);
$year = substr("$ARGV[0]", 0 ,4);
$weekday =
strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
print "$weekday";
' "$1"
}
you are assured of the time zone setting.
1 Like
getmmg
June 13, 2011, 10:43am
3
This should do it, replace the perl part in your code.
perl -e '
use POSIX qw(strftime);
use Time::Local;
@inp = split(/-/,$ARGV[0]);
$epochTime = timegm(0,0,0,$inp[2],$inp[1],$inp[0]);
@time=gmtime($epochTime -(4*3600)); #=> GMT-4
$day = strftime("%A",0,0,0,$time[3],$time[4],$time[5],-1,-1,-1);
print $day'
1 Like
Guys,
Something doesn't appear to be working... The following should echo "Tuesday" if I provide GMT+11.
#!/bin/ksh
dow()
{
export TZ=GMT+11
perl -e '
use POSIX qw(strftime);
$fmt = "%A"; # %a = abbreviated weekday %u = weekday number
$mday = substr("$ARGV[0]", 8, 2);
$mon = substr("$ARGV[0]", 5 ,2);
$year = substr("$ARGV[0]", 0 ,4);
$weekday =
strftime($fmt, 0, 0, 0, $mday , $mon - 1, $year - 1900, -1, -1, -1);
print "$weekday";
' "$1"
}
echo "$(dow `date "+%Y-%m-%d"` )"
Yes, got the following error.
Day '' out of range 1..31 at -e line 5
getmmg
June 13, 2011, 11:12am
7
Try this,
#!/bin/ksh
# input format YYYY-MM-DD prints day name
dow()
{
perl -e '
use POSIX qw(strftime);
use Time::Local;
@inp = split(/-/,$ARGV[0]);
$epochTime = timegm(0,0,0,$inp[2],$inp[1]-1,$inp[0]-1900);
@time=gmtime($epochTime +(11*3600)); #=> GMT -4
$day = strftime("%A",0,0,0,$time[3],$time[4],$time[5],-1,-1,-1);
print $day
' "$1"
}
echo "$(dow `date "+%Y-%m-%d"` )"
It told me today was "Wednesday". Copied the exact code you wrote.
For Jim's code, I think the signs are reversed... the ( - negative) provides the ( + positive) value and visa versa.
=> . test.sh
TZ=FOO-12: Tue Jun 14 03:03:51 FOO 2011
Tuesday
TZ=FOO-11: Tue Jun 14 02:03:51 FOO 2011
Tuesday
TZ=FOO-10: Tue Jun 14 01:03:51 FOO 2011
Tuesday
TZ=FOO-9: Tue Jun 14 00:03:51 FOO 2011
Tuesday
TZ=FOO-8: Mon Jun 13 23:03:51 FOO 2011
Monday
TZ=FOO-7: Mon Jun 13 22:03:52 FOO 2011
Monday
TZ=FOO-6: Mon Jun 13 21:03:52 FOO 2011
Monday
TZ=FOO-5: Mon Jun 13 20:03:52 FOO 2011
Monday
TZ=FOO-4: Mon Jun 13 19:03:52 FOO 2011
Monday
TZ=FOO-3: Mon Jun 13 18:03:52 FOO 2011
Monday
TZ=FOO-2: Mon Jun 13 17:03:52 FOO 2011
Monday
TZ=FOO-1: Mon Jun 13 16:03:52 FOO 2011
Monday
TZ=FOO0: Mon Jun 13 15:03:52 FOO 2011
Monday
TZ=FOO+1: Mon Jun 13 14:03:52 FOO 2011
Monday
TZ=FOO+2: Mon Jun 13 13:03:52 FOO 2011
Monday
TZ=FOO+3: Mon Jun 13 12:03:52 FOO 2011
Monday
TZ=FOO+4: Mon Jun 13 11:03:52 FOO 2011
Monday
TZ=FOO+5: Mon Jun 13 10:03:52 FOO 2011
Monday
TZ=FOO+6: Mon Jun 13 09:03:52 FOO 2011
Monday
TZ=FOO+7: Mon Jun 13 08:03:52 FOO 2011
Monday
TZ=FOO+8: Mon Jun 13 07:03:52 FOO 2011
Monday
TZ=FOO+9: Mon Jun 13 06:03:52 FOO 2011
Monday
TZ=FOO+10: Mon Jun 13 05:03:52 FOO 2011
Monday
TZ=FOO+11: Mon Jun 13 04:03:52 FOO 2011
Monday
***************** GMT -4
TZ=GMT-4: Mon Jun 13 19:03:52 GMT 2011
Monday
getmmg
June 13, 2011, 11:23am
9
Some prob with conversion logic, updated the code now.
Before that I need to clarify one thing. Do you want to convert the current time in GMT i.e (Mon, 13 Jun 2011 15:19) to GMT+11 and return the date.
Is this is your requirement?
I just need the day of week based on GMT-4.
getmmg
June 13, 2011, 11:28am
11
In that case can you try this,
perl -e '
use POSIX qw(strftime);
@time=gmtime(time -(4*3600)); #=> GMT -4
$day = strftime("%A",0,0,0,$time[3],$time[4],$time[5],-1,-1,-1);
print "$day\n"'
Run this in the console without anything. This script gets the current time and converts it into GMT-4 and returns the day.
Getting warmer....but the following should echo "Tuesday" when I change to +11.
#!/bin/ksh
# input format YYYY-MM-DD prints day name
dow()
{
perl -e '
use POSIX qw(strftime);
@time=gmtime(time -(4*3600)); #=> GMT +11
$day = strftime("%A",0,0,0,$time[3],$time[4],$time[5],-1,-1,-1);
print "$day\n"'
}
echo "$(dow `date "+%Y-%m-%d"` )"
getmmg
June 13, 2011, 11:41am
13
#=> GMT+11 is the comment part. If you want to change the actual time zone change it like this
@time=gmtime(time +(11*3600)); #=> GMT +11
Also you dont the shell scripts.
Just run only this
perl -e '
use POSIX qw(strftime);
@time=gmtime(time +(11*3600)); #=> GMT +11
$day = strftime("%A",0,0,0,$time[3],$time[4],$time[5],-1,-1,-1);
print "$day\n"'
1 Like
GETMMG,
Thanks for your help. I'll be adding this Perl into my shell script to get my local time when running my scripts (GMT-4).
Thanks again.
Scott