Temporarily change the date / time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Temporarily change the date / time
# 1  
Old 09-12-2008
Temporarily change the date / time

Hi -

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.

Any ideas welcome!
Thanks,
Sam
# 2  
Old 09-12-2008
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.
Code:
export TZ=MST6.5MDT
date
export TZ=MST7MDT
date

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.
# 3  
Old 09-12-2008
# 4  
Old 09-12-2008
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.:
Code:
~> date
Fri Sep 12 14:30:26 BST 2008
~> export TZ=AAA3BBB
~> date
Fri Sep 12 11:30:39 BBB 2008 // 3hours behind UTC as expected
~> export TZ=AAA3.2BBB
~> date
Fri Sep 12 13:30:45  2008 // doesn't seem to like '3.2' so it's reverted to UTC

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.

Thanks,
Sam
# 5  
Old 09-13-2008
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.
# 6  
Old 09-15-2008
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.
Code:
 ~> date;export TZ=AAA-02:02:02BBB-1:2:2;date;export TZ=;date
Mon Sep 15 09:04:02 UTC 2008
Mon Sep 15 10:06:04 BBB 2008
Mon Sep 15 09:04:02 UTC 2008
 ~> uname -a
Linux 2.6.18-53.el5

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
Code:
man tzset

Thanks for all the help from everyone Smilie

Last edited by samgdavies; 09-15-2008 at 07:55 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Date and time change in global and non global zone

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)
Discussion started by: Sumanthsv
1 Replies

2. Shell Programming and Scripting

Change date time stamp of existing file

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)
Discussion started by: mohtashims
12 Replies

3. Shell Programming and Scripting

Script to change date/time

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)
Discussion started by: Torquemada
6 Replies

4. Shell Programming and Scripting

Change the date and time format in UNIX script.

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)
Discussion started by: Varshha
8 Replies

5. Shell Programming and Scripting

Change the content of files but not change the date

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)
Discussion started by: ust3
7 Replies

6. Shell Programming and Scripting

Change the content of files but not change the date

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)
Discussion started by: ust3
0 Replies

7. Shell Programming and Scripting

Date Time Change for a particular target Zone

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)
Discussion started by: anuragpgtgerman
4 Replies

8. OS X (Apple)

how to change date and time format in menu bar

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)
Discussion started by: Straitsfan
5 Replies

9. UNIX for Dummies Questions & Answers

Date& Time change in linux beyond few days back

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)
Discussion started by: komala
4 Replies

10. UNIX for Dummies Questions & Answers

Move A File With Same Date,don't Change The Desitination Dir Date

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)
Discussion started by: jee.ku2
1 Replies
Login or Register to Ask a Question