![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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:
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.. |
|
||||
|
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" |
|
||||
|
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:
Thanks and sorry for my english
|
|
||||
|
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). |
|
||||
|
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!!! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|