The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
nawk strange behavior ahmad.diab Shell Programming and Scripting 4 05-20-2009 10:00 AM
strange behavior of find with xargs jerardfjay Shell Programming and Scripting 9 08-09-2007 09:06 AM
Very Strange Behavior for redirection cahook Shell Programming and Scripting 5 08-08-2007 12:32 PM
Strange Behavior on COM2 Elwood51 UNIX for Dummies Questions & Answers 0 08-02-2006 04:31 PM
strange sed behavior Kevin Pryke UNIX for Dummies Questions & Answers 5 06-13-2003 05:34 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2009
AlbertGM AlbertGM is offline
Registered User
  
 

Join Date: Mar 2009
Location: Barcelona - Catalonia
Posts: 28
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":

Code:
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:
Quote:
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


Thanks.

Last edited by AlbertGM; 09-28-2009 at 01:36 PM..
  #2 (permalink)  
Old 09-28-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
Hi AlbertGM,

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


Code:
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)

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

  #3 (permalink)  
Old 09-29-2009
AlbertGM AlbertGM is offline
Registered User
  
 

Join Date: Mar 2009
Location: Barcelona - Catalonia
Posts: 28
Thanks Scrutinizer,

I thought to get seconds from Epoch in UTC the command should be:
Code:
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:
Code:
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:
Quote:
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
  #4 (permalink)  
Old 09-29-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
I ran these commands on a machine that is set to CEST.

Code:
[me@somehost ~]$ date --date "1970-01-01" +%s
-3600
[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
0

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 (permalink)  
Old 09-29-2009
AlbertGM AlbertGM is offline
Registered User
  
 

Join Date: Mar 2009
Location: Barcelona - Catalonia
Posts: 28
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 (permalink)  
Old 09-29-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761
I don't think that should be necessary. What are you trying to do?
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0