ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
With the shell-util "date", on my machine in the Netherlands timezone:
Code:
hko:~$ cat /etc/timezone
Europe/Amsterdam
hko:~$ date --date='2009-01-25 20:57:22'
Sun Jan 25 20:57:22 CET 2009
hko:~$ date --date='TZ="UTC" 2009-01-25 20:57:22'
Sun Jan 25 21:57:22 CET 2009
With the shell-util "date", on my machine in the Netherlands timezone:
Code:
hko:~$ cat /etc/timezone
Europe/Amsterdam
hko:~$ date --date='2009-01-25 20:57:22'
Sun Jan 25 20:57:22 CET 2009
hko:~$ date --date='TZ="UTC" 2009-01-25 20:57:22'
Sun Jan 25 21:57:22 CET 2009
No, that converts from UTC to local time. He wants to convert from local time to UTC. I'm in California; when it's noon UTC, I'm running eight hours earlier, at 4:00am. When we run the proposed (almost correct) solution, we get:
Code:
wally:~/thursday/2$ date --date='2009-01-25 20:57:22'
Sun Jan 25 20:57:22 PST 2009
wally:~/thursday/2$ date --date='TZ="UTC" 2009-01-25 20:57:22'
Sun Jan 25 12:57:22 PST 2009
We would have wanted:
Code:
Mon Jan 26 04:57:22 UTC 2009
I've played with the date command to find a correct solution, but I couldn't get it do work for this.
There's a good possibility that this is not the whole story.
The script as I posted started with this line:
Code:
#!/usr/bin/perl
I was able to duplicate your error messages exactly by removing that line from the script. So I'm guessing that line is not in your copy of the script.
If you removed that line because you got an error message like this:
Code:
-bash: ./2.pl: /usr/bin/perl: bad interpreter: No such file or directory
then do this at the bash command prompt:
Code:
which perl
Then put a #! at the beginning of your output from that command and make that the first line of the script.
Edit:
The above is based on what you posted before you edited it. I'll have to take a look at your new response and see what goes, in my next post.
According to your /etc/localtime, your local time zone is UTC. So converting time from local time to UTC shouldn't make it any different at all, and sure enough, in your experience, that's exactly what's happening.
According to your /etc/localtime, your local time zone is UTC. So converting time from local time to UTC shouldn't make it any different at all, and sure enough, in your experience, that's exactly what's happening.
Hmmm.. but the input is '2009-01-25 20:57:22' is not in UTC (timestamps are from a system in another timezone), so I was expecting the output to show the equivalent in UTC.
Basically, I have a list of timestamps from a server not in UTC but in a timezone (GMT +10) which compiles with DST, and need to convert those timestamps back to UTC for correlation and storage in a database. Apologies if I did not make this clear in my first post but I was just trying to keep it simple.
#!/usr/bin/perl
sub convert_to_utc
{
my($input)=@_;
my $seconds;
my $tm_year;
my $tm_mon;
my $tm_mday;
my $tm_hour;
my $tm_min;
my $tm_sec;
$seconds=`date '+%s' "--date=$input"`;
($tm_sec,
$tm_min,
$tm_hour,
$tm_mday,
$tm_mon,
$tm_year
)=gmtime($seconds);
return sprintf("%04d-%02d-%02d %02d:%02d:%02d",
$tm_year+1900,
$tm_mon +1,
$tm_mday,
$tm_hour,
$tm_min,
$tm_sec
);
}
$ENV{'TZ'}="GMT+10";
print convert_to_utc('2009-01-25 20:57:22')."\n";
Thank you very much. This works perfectly. I was actually researching to see if we could utilise the TZ variable in your script without changing the timezone on the system, then I saw your latest post. :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.