Help with touch: bad time specification


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help with touch: bad time specification
# 1  
Old 07-23-2013
Oracle Help with touch: bad time specification

Here is the part of the script: I have modified the file name.Smilie

Code:
SSFILE=${My_HOME_DIR}/log/my_file_ss.log
export MM=`date '+%m'`
export DD=`date '+%d'`
export HH=`date '+%H'`
export MIN=`date '+%M'`
export HOURAGO=`echo ${HH} -1 |bc `
echo $HOURAGO
export TTIME=${MM}${DD}${HOURAGO}00
echo $TTIME
touch -t ${TTIME} ${SSFILE}
export MIN=`date '+%M'`
export HOURAGO=`echo ${HH} -1 |bc `
echo $HOURAGO
export TTIME=${MM}${DD}${HOURAGO}00
echo $TTIME
touch -t ${TTIME} ${SSFILE}

I get this message in error log file: touch: bad time specification
I have solaris 10 and this is .ksh file.

Can anyone tell me what is wrong with this commands?

Last edited by Scott; 07-23-2013 at 11:20 AM.. Reason: Please use code tags
# 2  
Old 07-23-2013
Can you show us the output from
Code:
echo ${TTIME}

You could also adjust the timezone value $TZ This will adjust your current session to give you a variation of the clock based on the real time your server holds (actually against GMT / UTC / Zulu depending how you see it)

So if your time zone is:-
Code:
echo $TZ
GMT0BST

You can set it to:-
Code:
TZ=GMT1BST

Then run your date commands to get the values and then reset the original TZ before running touch. You will probably have a different TZ to mine, but add one to the number early on.

Code:
Orig_TZ="$TZ"
TZ=GMT1BST               # Adjust to suit, of course
date '+%m %d %H'|read MM DD HH
TZ=$Orig_TZ
touch -t ${MM}${DD}${HH}00 ${SSFILE}



I hope that this helps


Robin
Liverpool/Blackburn
UK
# 3  
Old 07-26-2013
touch needs the year!
I suggest to run date once with shell style format, and get all variables with eval
Code:
eval `
date +'YY=%Y MM=%m DD=%d HH=%H MIN=%M'
`
HOURAGO=`echo ${HH}-1 | bc `
echo $HOURAGO
TTIME=$YY$MM$DD${HOURAGO}00
echo $TTIME

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 07-27-2013
touch does not require a year:

Code:
           [[CC]YY]MMDDhhmm[.SS]

And your solution does not work if hour is "00".
Code:
$ date +'YY=%Y MM=%m DD=%d HH="00" MIN=%M'
...
$ echo $TTIME
0723-100

This User Gave Thanks to Scott For This Post:
# 5  
Old 07-28-2013
Quote:
Originally Posted by Scott
touch does not require a year:

Code:
           [[CC]YY]MMDDhhmm[.SS]

And your solution does not work if hour is "00".
Code:
$ date +'YY=%Y MM=%m DD=%d HH="00" MIN=%M'
$ echo $TTIME
0723-100

It is worse than that. Using:
Code:
HOURAGO=`echo ${HH}-1 | bc `

to set HOURAGO turns a 2 digit hour string into a single digit hour string for any time before 11am (thereby producing a -t option string in the wrong format). And even if HOURAGO was set to 23 instead of -1, the HH and possibly DD and MM values will be off as well. And, if the code is fixed to set HOURAGO to 23 and 00 through 09 (rather than -1 and 0 through nine), it needs to use CCYY in case the script is run in the 1st hour of New Year's day by a cron job (or some poor operator stuck in the office on New Year's Day's early morning shift) or the touch (after adjusting MM and DD will set the timestamps to the 11pm hour of December 31st in the new year instead of in the previous year).

And, using multiple calls to the date utility to get individual portions of the desired date is a recipe for generating wrong times that are difficult to diagnose. If the calls to date happen during different seconds on the clock, the minute, hour, day, month, and year could all be off. For example, if the first of the calls to date in this example starts just before midnight on December 31th, the resulting calculated date (before subtracting 1 from the hour) could be any of:
Code:
12312359
01312359
01012359
01010059
   or
01010000

With scheduling delays the differences could be even more unpredictable. (In other words; NEVER use multiple calls to date to get various pieces of what is supposed to be a single timestamp.)

Robin's (rbatte1) proposal will work in the US except for a couple of hours twice a year (on days when we switch to and from daylight savings time). Using TZ=GMT1BST will be off not only on daylight savings time switch days, but will also be off by an hour all day on days between the switch to daylight savings time in the UK and the switch to daylight savings time in the US. This can be fixed outside the US by using the full capabilities of the TZ variable format stdoffset[dst[offset[,start[/time],end[/time]]] which can specify not only the names of the standard and daylight savings time zone abbreviations (std and dst, respectively, and the offset from Greenwich, but also the start and end dates for daylight savings time, and the times at which the shift takes place; but it still leaves you with a result that is an hour off for a couple of hours around the switchover two times per year in areas that observe daylight savings time.

To accurately get the time one hour ago at all times of the day every day of the year on Solaris systems that don't have the GNU date utility installed when you are in an area of the world where daylight savings time is observed, you can either use perl (as has been shown in these forums many times before) or write a simple C program to get the current number of seconds since the Epoch, subtract 3600 (the number of seconds in an hour) and format that value using a function to print a seconds since the Epoch value that adjusts for the current system default timezone as modified by the setting of TZ. In C, the following should work:
Code:
#include <libgen.h>
#include <stdio.h>
#include <time.h>

int
main(int argc, char *argv[]) {
        time_t          modtime;        // modified seconds since the Epoch
        char            out[1024];      // output buffer
        struct tm       *timep;         // broken down adjusted local time

        // Verify number of arguments
        if(argc != 3) {
                (void)fprintf(stderr, "Usage: %s offset fmt\n",
                        basename(argv[0]));
                return 1;
        }

        // Get desired time as a value in seconds since the Epoch as
        // specified by the "offset" operand.
        modtime = time(NULL) + atol(argv[1]);

        // Convert that time to a struct tm as adjusted by current $TZ setting.
        timep = localtime(&modtime);

        // Format the output as specified by the given "fmt" operand.
        (void)strftime(out, sizeof(out) - 1, argv[2], timep);
        (void)printf("%s", out);
}

although the above code doesn't check for errors (other than wrong number of arguments. For this to be put into a production environment; at the least, the calls to atol(), localtime(), and strftime() should be checked for errors. The output is also unspecified if the fmt operand given causes strftime() to produce more that 1024 bytes of output (including the terminating NUL byte).

If you compile the above code and mv the a.out to a file named timeoffset, the following commands will create files with timestamps set to the time one day ago and one hour ago, respectively:
Code:
touch -t $(timeoffset $((-24*60*60)) "%Y%m%d%H%M") now-1d
touch -t $(timeoffset -3600 "%Y%m%d%H%M") now-1h

Are we having fun yet?
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 07-28-2013
Thanks Scott and Don for pointing out the problems with date calculations!
Another thanks to Don for a general C solution!
Here is a (not so general) native Perl solution
Code:
TTIME=`perl -e '
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time-3600);
printf "%04d%02d%02d%02d%02d\n",$year+1900,$mon+1,$mday,$hour,$min;
'`
echo $TTIME

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Touch - changing date and time

Hi, I am facing a problem with the command - TOUCH on Linux. See the example below: File on Linux: rw-rw-r-- user1 user1 Jan 01 09:00 test.txt The file - test.txt was created by the user - user1. Now, I want to change the date and time, but using other user - user2 The user2... (12 Replies)
Discussion started by: brjohnsmith
12 Replies

2. Shell Programming and Scripting

How to change time stamp with touch command?

Hi, I wish to change time stamp of a directory with all its subdirectories and files on server. I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data: find * -type d -exec touch... (5 Replies)
Discussion started by: bioinfo
5 Replies

3. Shell Programming and Scripting

Help with column specification

Hi I am working on a program that reads a file with multiple columns and was curious how to specify the columns to be manipulated in the command line. For example the file may look something like: Column1 Column2 Column3 Column4 Column5 Column6 AC 82542 3525 ... (1 Reply)
Discussion started by: drossy
1 Replies

4. UNIX for Dummies Questions & Answers

touch -t time, using different userid

Hi, I am reciveing files from a remote system on my linux box. These files are named based on time, which I can use to 'touch' the time . I can access/modify these files using my id. but when I tried touching time using my id I am getting error; touch -t 1001261234 1001261234_job2333... (15 Replies)
Discussion started by: rajivbravo
15 Replies

5. Solaris

Getting server specification

Hi all, Can anyone help me to get the server specifications like the following? eg. SUN Fire XXX X UltraSPARC X MHZ X MB Memory X GB od hard disk X On board PCI IO card X PCI HNI card Thanks. (4 Replies)
Discussion started by: beginningDBA
4 Replies

6. Shell Programming and Scripting

how to make a log.txt and add date and time when use ls,touch and find

Hey guy, how to make the log.txt file and record date and time when ls, touch and find command run? Thanks Boly (13 Replies)
Discussion started by: chenboly
13 Replies

7. UNIX for Dummies Questions & Answers

Touch all files and subdirectories (recursive touch)

I have a folder with many subdirectories and i need to set the modified date to today for everything in it. Please help, thanks! I tried something i found online, find . -print0 | xargs -r0 touch but I got the error: xargs: illegal option -- r (5 Replies)
Discussion started by: glev2005
5 Replies

8. Shell Programming and Scripting

how to touch a file with prev time stamp

i want to find the files which are modified in last 30 to 120 minutes i am using "find . -mmin +30 -mmin -120 " it is giving me the error find: bad option -mmin find: path-list predicate-list can somebody help me out . Thank you (5 Replies)
Discussion started by: Prat007
5 Replies

9. Solaris

System specification checking

Hi, anybody know how to check the system specification for the unix by using the command like the memory size, cpu's speed and etc. Thanks. (3 Replies)
Discussion started by: efang
3 Replies

10. UNIX for Dummies Questions & Answers

HD specification

What command do I use to determine how many/what kind/how big the hard drives on a box are? (2 Replies)
Discussion started by: abolshoun
2 Replies
Login or Register to Ask a Question