SUMMARY: modify system time

From: <Werner.Rost_at_zfboge.com>
Date: Mon, 28 Jul 2003 16:24:58 +0200

Thanks a lot to
   Jim Fitzmaurice [jpfitz_at_fnal.gov]
   Jay Leafey [jleafey_at_utmem.edu]
   Mark Scarborough [mscar-osf_at_cactus.org]
for their hints. This list is very helpful.


I realized Jays solution using a perl script:

#!/usr/bin/perl -w
($mon,$day,$year,$hour,$min) = (localtime(time()+(60*60*2)))[4,3,5,2,1];
$newdate = sprintf "%02u%02u%02u%02u%04u",
        $mon+1, $day, $hour, $min, $year+1900;

print "date $newdate\n";
exec "date $newdate";


Jims solution:

#!/bin/ksh
DAY=`date +"%d"`
MON=`date +"%m"`
MIN=`date +"%M"`
YEA=`date +"%Y"`
HOR=`date +"%H"`
HOU=`expr $HOR + 2`
#
# Make sure hour doesn't exceed 24.
#
if [ $HOU -ge 24 ]
then
    HOU=`expr $HOU - 24`
    DAY=`expr $DAY + 1`
fi
#
# Ensue HOU and DAY are two digit numbers.
#
case $HOU in
    ?) HOU=`echo 0$HOU` ;;
esac
#
case $DAY in
    ?) DAY=`echo 0$DAY` ;;
esac
#
SEC=`date +"%S"`
#
# Set date
#
/bin/date $MON$DAY$HOU$MIN$YEA.$SEC
#
exit
#


Marks two solutions:

1) get 'gnudate' and use:

$ gnudate --date="+2 hours"

2) do the "timezone trick":

(for me, in CST6CDT)
$ date
Fri Jul 25 10:10:09 CDT 2003
$ TZ=CST4CDT date
Fri Jul 25 12:10:24 CDT 2003

It looks to me like you are probably in GMT-2 timezone. If so, you could
do this:
$ TZ=GMT-2 date
Fri Jul 25 17:11:42 GMT 2003
$ TZ=GMT-4 date
Fri Jul 25 19:11:46 GMT 2003

This trick very much depends on what timezone your system is set for so it
is not exactly the best, most portable solution, but it does work if you
are careful.



----------------------------------------------

Original problem:

I need a script which add 2 hours to the actual system time and then
"correct" the system time:

1. t = date (actual system time say 9:13h)

2. t := t + 2 hours 9:13h --> 11:13h

3. date = t (set system time to 11:13h)

Maybe somebody has done this and can tell me how to do.
Received on Mon Jul 28 2003 - 14:25:19 NZST

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:44 NZDT