Strange behavior manipulating dates

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange behavior manipulating dates
# 1  
Old 09-28-2009
Strange behavior manipulating dates

Hi all!
I need to manipulate dates, but my system has a strange behavior with dates. Running this script at "Mon Sep 28 18:03:59 CEST 2009":
current_time=`date +%s`
echo "Time= $current_time"

formatDate=`date --date "1970-01-01 +$current_time sec" "+%Y-%m-%d  %H:%M:%S "`
echo "Format= $formatDate"

newDate=`date --date "$formatDate" +%s`
echo "newDate= $newDate"

formatDate2=`date --date "1970-01-01 +$newDate sec" "+%Y-%m-%d  %H:%M:%S "`
echo "Format2= $formatDate2"

it prints:
Time= 1254153839
Format= 2009-09-28 17:03:59
newDate= 1254150239
Format2= 2009-09-28 16:03:59
So, every time I convert time from seconds since Epoch to human readable format it subtracts an hour. Does anybody knows why?
I don't know if this is important, but I'm in Spain, which is usually UTC+1, but now, in summer, we are UTC+2.

OS: CentOS
Kernel: 2.6.18-92.1.18.el5 x86_64


Last edited by AlbertGM; 09-28-2009 at 01:36 PM..
# 2  
Old 09-28-2009
Hi AlbertGM,

Here you are outputting the date in seconds since 1970-01-01 UTC.

current_time=`date +%s`
echo "Time= $current_time"

Here you are adding those seconds to 1970-01-01 in your local Time Zone (not UTC)
formatDate=`date --date "1970-01-01 +$current_time sec" "+%Y-%m-%d  %H:%M:%S "`
echo "Format= $formatDate"

# 3  
Old 09-29-2009
Thanks Scrutinizer,

I thought to get seconds from Epoch in UTC the command should be:
date --utc +%s

I just try it and both commands (with and without `--utc`) prints the same.
Although both commands returns time from UTC, there's a strange behavior, too. If I run `date`without parameters it tells me my local Time zone is CEST (Central European Summer Time), ie: UTC +2.
So, I retrieve seconds since Epoch (which are returned in UTC) I should add 2 hours to get the seconds in my time zone, right?
However If I convert this seconds variable to human readable format, the time has one hour more than correct.
For example this script is run at Tue Sep 29 14:19:51 CEST 2009:
currentTime=`date +%s` #Seconds since Epoch in UCT
echo "currentTime = $currentTime"
currentTime=$(( $currentTime + 7200 )) #Add two hours
echo "currentTime = $currentTime"
dateFormat=`date -d "1970-01-01 +$currentTime sec" "+%Y-%m-%d  %H:%M:%S "`
echo "dateFormat = $dateFormat"

It prints:
currentTime = 1254226791
currentTime = 1254233991
dateFormat = 2009-09-29 15:19:51
You see they differ in one hour. You know why? There's something wrong in my script?

Thanks and sorry for my english Smilie
# 4  
Old 09-29-2009
I ran these commands on a machine that is set to CEST.
[me@somehost ~]$ date --date "1970-01-01" +%s
[me@somehost ~]$ date --date "1970-01-01"
Thu Jan  1 00:00:00 CET 1970
[me@somehost ~]$ date -u --date "1970-01-01"
Thu Jan  1 00:00:00 UTC 1970
[me@somehost ~]$ date -u --date "1970-01-01" +%s

I think what happens is dat when you ask for the date on 1970-01-01 it reports the time as it was on 1970-01-01, which is winter time and hence the time difference with UTC is only one hour. Note that it reports the time in CET (Central European Time), not in CEST (Central European Summer Time).
# 5  
Old 09-29-2009
That makes sense.
Any proposal to solve it, that works in summer as well as in winter time?
For example always ask for time zone. So, if CEST substract 1 hour, and if CET time is right. What do you think?

Thanks a lot!!!
# 6  
Old 09-29-2009
I don't think that should be necessary. What are you trying to do?
# 7  
Old 09-29-2009
Originally Posted by Scrutinizer
I don't think that should be necessary. What are you trying to do?
I'm, trying to make some scripts that monitor system information using free, iostat, mpstat, vmstat.... and maybe sar/sadc commands.
That scripts must be launched with some delay between each execution.
Then, I will recollect such information with others scripts that will be ploted with gnuplot using dates on X axis.
To do some arithmetic (add the delay) with dates I work with seconds since Epoch, because it's easier to use with variables. However to plot graphics I need to convert seconds to a human readable format, to show them. Dates must be correct to match with other monitor processes (oracle, Weblogic, etc...)

Anyway, you helped me a lot to know how dates work and now I think I'm able to find out a way to get what I need.

Of course, any other help or advice will be very welcome Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strange Ctrl+C behavior

Hello All, I have a strange issue. I've created a shell script which connects to RMAN (Oracle Recovery Manager) and executes full DB backup. I then executed this script with nohup and in the background: $ nohup > logfile.log 2>&1 &The issue is that when I tried to take a look into... (6 Replies)
Discussion started by: JackK
6 Replies

2. Shell Programming and Scripting

Strange behavior of grep

Hi All, I am facing a strange problem while grepping for a process. Here is the small script that i have written. It will look for any process running with the parameter passed to the script. If no process is running it should print appropriate message. $ cat t.ksh #!/bin/ksh set -x ... (9 Replies)
Discussion started by: veeresh_15
9 Replies

3. AIX

Strange behavior with tar

I am trying to create an archive using tar. I am specifying a list of directories using the -L option. For testing purposes I created a simple directory structure: /backup/test /backup/test/test1 /backup/test/test2 The file specified by the -L option, named files.txt, contains:... (8 Replies)
Discussion started by: judykstra
8 Replies

4. AIX

Strange memory behavior

Hello together, i have a strange memory behavior on a AIX 7.1 System, which i cannot explain. The Filesystem-Cache will not be grow up and drops often after few minutes. I know if a file was deleted, that the same segment in the FS-Cache will also be cleared. But i am not sure if this is the... (8 Replies)
Discussion started by: -=XrAy=-
8 Replies

5. Red Hat

strange mail behavior

Hi I have script to to take backup and send mail to a group once a day. One strange behavior I have observed recently is that most of the time the mail we receive is fine . But someday it just sends out mail without any subject with undisclosed recipients. I dont know how to find the cause... (0 Replies)
Discussion started by: ningy
0 Replies

6. Ubuntu

Ubuntu strange behavior

It is so till login screen. I mean that when I boot my computer, Ubuntu shows a splash screen with mouse instead of Ubuntu logo and in the login screen it shows XUbuntu login screen... It began when I upgraded to previous kernel, I suppose, but I'm not sure... I can't say that it annoys me very... (6 Replies)
Discussion started by: Sapfeer
6 Replies

7. Programming

Strange behavior in C++

I have the following program: int main(int argc, char** argv){ unsigned long int mean=0; for(int i=1;i<10;i++){ mean+=poisson(12); cout<<mean<<endl; } cout<<"Sum of poisson: "<< mean; return 0; } when I run it, I get the... (4 Replies)
Discussion started by: santiagorf
4 Replies

8. Shell Programming and Scripting

nawk strange behavior

Dear guys; when deleting repeated lines using nawk as below ; Why the below syntax works? nawk ' !a++' infile > outfile and when using the other below syntax the nawk doesn't work? nawk ' { !a++ } ' infile > outfile or nawk ' { !a++ } ' infile > outfile BR (4 Replies)
Discussion started by: ahmad.diab
4 Replies

9. UNIX for Dummies Questions & Answers

Strange Behavior on COM2

Hi, I have a problem with a new touch screen controller that I am trying to use on a SCO 3.0 system. THe touch screen controller only wants to talk at 9600baud. I have updated /etc/inittab per the manual and also edited /usr/lib/event/devices to use 9600 baud. The only way I can get the... (0 Replies)
Discussion started by: Elwood51
0 Replies
Login or Register to Ask a Question