I'd like to know if it's possible to change the time on a Unix box temporarily, do some stuff, and then set it back to the correct time.
I'm testing some code that reads a time (in hours) from a config file, and does something at that time. I want to be able to run regression test scripts at any time.
I envisage doing something like this:
config file:
trigger.hour=7 // do something at 07:00
Invoke regression test script at any time, eg 14:57
- Test script uses date to set the time to 06:59:59
- The code gets invoked when the system time reaches 07:00:00
- Check the right thing happened (test = pass or fail)
- Set the time back to the correct current time (14:57 + ???)
The simplest thing would be if there's a command that goes and finds out what the time is that I can use to reset the clock after my test.
An alternative would be to save the current time, set it to the fake value, run the test, calculate how long the test lasted, and then set the time to (saved_time + elapsed_time) -- but I'm not sure how I would do all that in a shell script.
The date command will reset date/time if you run as root. Not always the best idea.
A safer approach is to set the date/time the process and it's children see.
An example in the MST timezone.
By diddling with the n in XXXn.nXXX of a TZ setting it changes the effective time.
If your app uses any of the time.h functions, like localtime, this changes date date the application sees. The MST part can be any letters e.g., PSQ5PDQ. Just be sure to reset it after testing.
Changing TZ, I only seem to be able to shift the time by whole hours (if I use a value with a decimal point the time seems to resort to UTC). Is this what you'd expect here?
e.g.:
I do need to be able to set the offset more preciesly (to the second if possible).
Is there a way around this? I can see that this way is going to be a lot nicer in terms of not messing with the actual system time, and being easier to reset afterwards.
What OS? TZ settings are defined to have to allow one decimal, XXX1.5YYY
Each OS also has a tztab file or whatever that you can manipulate. Don't manipulate the real one.... we'll have to dummy it so you don't screw up the whole system.
Got it - the TZ variable does work, but I was using the wrong syntax - my system seems to prefer a: b :c time to x.y.
The command shown sets my user's clock 2hr, 2min and 2sec ahead of UTC (winter), 1hr, 2min, 2sec ahead of UTC (summer), and is being run in summer.
As you can see above, I'm running Linux.
The format for TZ on my system is:
(3 letter identifier)(offset from UTC h:m:s)[(summer time identifier)(summer offset from UTC h:m:s)]
h:m:s can be shortened to just hours, hours:minutes or hours:min:sec, with zero-padding optional. There's more stuff available too, eg to change when summertime starts. Do
Thanks for all the help from everyone
Last edited by samgdavies; 09-15-2008 at 07:55 AM..
Hi,
If I change date and time in global zone, then it will affect in non global zones.
During this process what files will get affect in non global zones and which mechanism it's using to change.
gloabl zone:Solaris 11.3 X86
TIA (1 Reply)
I have a file hello.txt which was created today (today's date timestamp)
I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now.
uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v
Can you please suggest a easy way to do that ? (12 Replies)
Hi everybody!
I need to perform a task with a script but I have no idea how to do it, I hope someone could help me:
- on my linux pc I have many folders with movies, tv shows, toons, ecc.
They are shared by a dlna server to my panasonic tv where I can browse and see them.
The problem is that... (6 Replies)
Hi, I am extracting a date string from the source file like this :
06/05/2014 16:04:00
I want to change it to 05-JUN-14 04.05.00.000000000 PM
I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (7 Replies)
I have 100 files in a directory , all the files have a word "error" and they are created in different date . Now I would like to change the word from "error" to "warning" , and keep the date of the files ( that means do not change the file creation date after change the word ) , can advise what can... (0 Replies)
Hi
Summary:
- Script will take 2 arguments
- Argument 1: Date/Time value in a particular format
- Argument 2: TimeZone value
Examples:
Argument 1:
a. "May 11, 2012 08:00:00 AM"
b. "Dec 21, 2012 12:21:12 PM"
c. "Oct 2, 2012 05:00:00 PM"
Argument 2:
a. MT or MST or MDT
b. ET
c.... (4 Replies)
Can someone please tell me how to change the time and date format in the menu bar. I get to System Preferences -- Language and Text, format, customize, but I can't figure out what to do next. I want to change, say from Tue May 4 to 5 4 2010. How the heck do I do this? (5 Replies)
Hi,
Could you please let me know the command to change my date /time beyond few days back. Currently when i am trying the below commands able to go beyond one day at max.
-->date
-->Mon Sep 29 19:31:58 EST 2008
-->export TZ=TMP40:00:00 (Changing the date beyond 40 hours)
-->Sun Sep 28... (4 Replies)
Assume, I created one file three years back and I like to move the file to some other directory with the old date (Creation date)? Is it possible? Explain? (1 Reply)