How to set time zone with pure Ruby [duplicate] - ruby

I'm here on Ubuntu 12.04, and I can see:
$ cat /etc/timezone
America/Phoenix
Accordingly Time will return a time with a non-UTC zone:
$ irb
> Time.now
=> 2013-03-27 13:44:49 -0700
> Time.at 0
=> 1969-12-31 17:00:00 -0700
I can override the system time zone using the TZ environment variable:
$ TZ=UTC irb
> Time.now
=> 2013-03-27 20:47:19 +0000
> Time.at 0
=> 1970-01-01 00:00:00 +0000
Is there anyway I can make this change programmatically, within a Ruby process?

You can also set environment variables from within ruby by accessing the ENV hash:
ENV['TZ'] = 'UTC'
Time.at 0
#=> 1970-01-01 00:00:00 +0000
also see this answer: Set time zone offset in Ruby, It provides a way to write something like
with_time_zone 'UTC' do
# do stuff
end
# now TZ is reset to system standard

You can use Time#gmtime. For example
Time.now
# => Wed Mar 27 16:55:11 -0400 2013
Time.now.gmtime
# => Wed Mar 27 20:55:14 UTC 2013
Time.at(0)
# => Wed Dec 31 19:00:00 -0500 1969
Time.at(0).gmtime
# => Thu Jan 01 00:00:00 UTC 1970
Time#utc also works and is an alias for Time#gmtime

Depending on the use case, ActiveSupport offers a lot of TimeZone related goodness.
$ gem install activesupport
$ irb
> require 'active_support/time' # => true
> Time.zone = 'Pacific Time (US & Canada)' # => "Pacific Time (US & Canada)"
> Time.zone.now # => Wed, 27 Mar 2013 16:14:19 PDT -07:00
ActiveSupport may be a larger dependency than you want, but you shouldn't overlook it.

Related

Laravel 5.7 Reformat Date Time Stamp in View

By default when Laravel creates a post it stores the date and time stamp as:
2019-01-31 23:32:06
How can I show it in the view as - Jan 31st, 2018 11:32pm?
U can use Php Carbon in modifying date. eg.
$dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584');
// $dt->toAtomString() is the same as $dt->format(DateTime::ATOM);
echo $dt->toAtomString(); // 2019-02-01T03:45:27+00:00
echo $dt->toCookieString(); // Friday, 01-Feb-2019 03:45:27 UTC
echo $dt->toIso8601String(); // 2019-02-01T03:45:27+00:00
// Be aware we chose to use the full-extended format of the ISO 8601 norm
// Natively, DateTime::ISO8601 format is not compatible with ISO-8601 as it
// is explained here in the PHP documentation:
// https://php.net/manual/class.datetime.php#datetime.constants.iso8601
// We consider it as a PHP mistake and chose not to provide method for this
// format, but you still can use it this way:
echo $dt->format(DateTime::ISO8601); // 2019-02-01T03:45:27+0000
echo $dt->toISOString(); // 2019-02-01T03:45:27.612584Z
echo $dt->toJSON(); // 2019-02-01T03:45:27.612584Z
echo $dt->toIso8601ZuluString(); // 2019-02-01T03:45:27Z
echo $dt->toDateTimeLocalString(); // 2019-02-01T03:45:27
echo $dt->toRfc822String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc850String(); // Friday, 01-Feb-19 03:45:27 UTC
echo $dt->toRfc1036String(); // Fri, 01 Feb 19 03:45:27 +0000
echo $dt->toRfc1123String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc2822String(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toRfc3339String(); // 2019-02-01T03:45:27+00:00
echo $dt->toRfc7231String(); // Fri, 01 Feb 2019 03:45:27 GMT
echo $dt->toRssString(); // Fri, 01 Feb 2019 03:45:27 +0000
echo $dt->toW3cString(); // 2019-02-01T03:45:27+00:00
Kindly read more about Php Carbon
You can use Carbon in laravel.
First, use carbon:
use Carbon\Carbon;
Then use the format you want, in your case, it would be like this:
public function test()
{
$dt = User::find(1);
return $dt->created_at->format('M jS\\, Y h:i:s A'); //Dec 1st, 1975 02:15:16 PM
}
You can also use it in your view, just use carbon at the top of your file:
<?php use Carbon\Carbon;?>
then change the format in your display like {{$dt->created_at->format('M jS\\, Y h:i:s A')}}
Your input is 2018-01-31 23:32:06.
Your expected output is Jan 31st, 2018 11:32pm.
I have created a function:
function convertToPreferedDateFormat($dateString='')
{
$toTimeString = strtotime($dateString);
$yourExpectedFormat = 'M dS, Y h:ia';
// $prettyBeautifullWay = 'l jS \of F Y h:i:s A';
return date($yourExpectedFormat, $toTimeString);
}
Then now testing
$timeString = '2018-01-31 23:32:06';
$expectedOutput = 'Jan 31st, 2018 11:32pm';
echo convertToPreferedDateFormat($timeString);
echo "<br>";
echo $expectedOutput;

Rspec fails because date is not in correct format

I have an example like this in Rspec:
describe "#parse" do
context "when disposition date is present" do
it "parses data" do
expect(#practice.parse[0]).to match_array [ nil, "502011TR053942AXXXNB", Date.parse("2011-02-23") ]
end
end
end
The date that is generated into the array is generated like this:
Date.strptime(date.strftime("%m/%d/%Y"), "%m/%d/%Y")
So it is a Date object.
However, Rspec fails:
Failure/Error:
expect(#practice.parse[0]).to match_array [
nil, "502011TR053942AXXXNB", Date.parse("2011-02-23") ]
expected collection contained: [nil, "502011TR053942AXXXNB", Wed, 23
Feb 2011 ]
It says it expected "Wed, 23 Feb 2011". But isn't this just a to_s called on the Date object? How should I handle this?
Write it:
expect(#practice.parse[0]).to match_array [ nil, "502011TR053942AXXXNB", Date.parse("2011-02-23").strftime("%a, %d %b %Y") ]
Read the directives from here.
~$ ruby -e 'require "date";puts Date.parse("2011-02-23").strftime("%a, %d %b %Y")'
# => Wed, 23 Feb 2011

Convert epoch to local time in perl on windows

We are trying to convert the output received from the below code
The current output is in this form
testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin
The output required is something like
testingwindows from Fri Oct 3 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows1 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows2 from Fri Oct 2 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
testingwindows3 from Fri Oct 12 13:51:05 2015 GMT to Mon Nov 9 13:51:05 2015 GMT by kkulka11 for testingwin.
This is my current code
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS" ,
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
);
system(#args_query);
We tried using perl -pe 's/(\d{10})/gmtime($1)/e'; but not able to convert and it gives this error
'o~}go⌂⌂t⌂x⌂w' is not recognized as an internal or external command,
operable program or batch file.
when we used the code as
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my $mqt = "$MCELL_HOME\\mqt.pl";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS",
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
) | $mqt;
system(#args_query);
Needed experts quick help and guidance to achieve the output in human-readable format.
Edit:
Updated the code as per Jacob comments but still not received the output as desired. Please suggest
if ( $COMMAND eq 'queryone' ) {
my $msend_query = "$MCELL_HOME\\bin\\mquery";
my #args_query = (
$msend_query,
"-q",
"-c", "$MCELL_HOME\\etc\\mclient.conf",
"-n", "$CS_BLACKOUT_CELL",
"-d",
"-f", "csv",
"-a", "CS_EMB_GBF_BLACKOUTS" ,
"-s", "blackout_host,start_timestamp,stop_timestamp,userid,reason",
"-w", "blackout_host: == '${BLACKOUTHOST}'"
);
chomp;
my #parts = split(/,/, system(#args_query));
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
Output:
M:\AbhayBackup\PerlKK>test.pl -q -h testingwin
testingwin
sub: testingwin
testingwin,1446727960,1446728560,kkulka11,testingwin
0 from Thu Jan 1 05:30:00 1970 to Thu Jan 1 05:30:00 1970 by for
while (<DATA>) {
chomp;
my #parts = split(/,/, $_);
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
__DATA__
testingwindows,1446727960,1446728560,kkulka11,testingwin
testingwindows1,1446727160,141228560,kkulka11,testingwin
testingwindows2,1446727120,1446728560,kkulka11,testingwin
testingwindows3,1446727960,1446728560,kkulka11,testingwin
Output:
testingwindows from Thu Nov 5 05:52:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
testingwindows1 from Thu Nov 5 05:39:20 2015 to Sun Jun 23 07:09:20 1974 by kkulka11 for testingwin
testingwindows2 from Thu Nov 5 05:38:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
testingwindows3 from Thu Nov 5 05:52:40 2015 to Thu Nov 5 06:02:40 2015 by kkulka11 for testingwin
Edit: based on your most recent update to the question, I now believe that you're trying to capture the output of the command and process it. Since you haven't provided a minimal, complete, and verifiable example, and because I have no idea what mquery is and you haven't provided an explanation for that, either, I present to you this guess:
if ($COMMAND eq 'queryone') {
my #lines = `$MCELL_HOME\\bin\\mquery -q -c $MCELL_HOME\\etc\\mclient.conf -n $CS_BLACKOUT_CELL -d -f csv -a CS_EMB_GBF_BLACKOUTS -s blackout_host,start_timestamp,stop_timestamp,userid,reason -w blackout_host: == '${BLACKOUTHOST}'`;
for (#lines) {
chomp;
my #parts = split(/,/, $_);
$parts[1] = localtime($parts[1]);
$parts[2] = localtime($parts[2]);
printf("%s from %s to %s by %s for %s\n", #parts);
}
}

Time Zone difference between Time.now and 1.day.ago

My app time_zone is set to UTC(default, I didn't change it in config/application.rb).
This is what I see in rails console
1.9.3p194 :004 > Time.now
=> 2014-03-20 14:45:23 -0500
1.9.3p194 :005 > 1.day.ago
=> Wed, 19 Mar 2014 19:45:48 UTC +00:00
Why do I get the time in central when I do Time.now? It should return time in UTC like 1.day.ago
Time.now uses the locale of the machine it is running on. For consistency, you can do Time.now.utc to force UTC:
1.9.3-p484 :001 > Time.now
2014-03-20 16:14:23 -0400
1.9.3-p484 :002 > Time.now.utc
2014-03-20 20:14:26 UTC
use Time.zone.now - it will take into account your application's time zone:
project with default TZ:
2.0.0-p353 :001 > Time.now
=> 2014-04-01 23:12:06 +0300
2.0.0-p353 :002 > Time.zone.now
=> Tue, 01 Apr 2014 20:12:11 UTC +00:00
2.0.0-p353 :003 >
project with custom TZ:
2.1.0 :001 > Time.now
=> 2014-04-01 23:14:23 +0300
2.1.0 :002 > Time.zone.now
=> Tue, 01 Apr 2014 22:14:27 CEST +02:00
2.1.0 :003 >
Here you'll find more useful info about working with timezones (especially take a look at DOs and DONTs section):
http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails

Can I override the system timezone in Ruby?

I'm here on Ubuntu 12.04, and I can see:
$ cat /etc/timezone
America/Phoenix
Accordingly Time will return a time with a non-UTC zone:
$ irb
> Time.now
=> 2013-03-27 13:44:49 -0700
> Time.at 0
=> 1969-12-31 17:00:00 -0700
I can override the system time zone using the TZ environment variable:
$ TZ=UTC irb
> Time.now
=> 2013-03-27 20:47:19 +0000
> Time.at 0
=> 1970-01-01 00:00:00 +0000
Is there anyway I can make this change programmatically, within a Ruby process?
You can also set environment variables from within ruby by accessing the ENV hash:
ENV['TZ'] = 'UTC'
Time.at 0
#=> 1970-01-01 00:00:00 +0000
also see this answer: Set time zone offset in Ruby, It provides a way to write something like
with_time_zone 'UTC' do
# do stuff
end
# now TZ is reset to system standard
You can use Time#gmtime. For example
Time.now
# => Wed Mar 27 16:55:11 -0400 2013
Time.now.gmtime
# => Wed Mar 27 20:55:14 UTC 2013
Time.at(0)
# => Wed Dec 31 19:00:00 -0500 1969
Time.at(0).gmtime
# => Thu Jan 01 00:00:00 UTC 1970
Time#utc also works and is an alias for Time#gmtime
Depending on the use case, ActiveSupport offers a lot of TimeZone related goodness.
$ gem install activesupport
$ irb
> require 'active_support/time' # => true
> Time.zone = 'Pacific Time (US & Canada)' # => "Pacific Time (US & Canada)"
> Time.zone.now # => Wed, 27 Mar 2013 16:14:19 PDT -07:00
ActiveSupport may be a larger dependency than you want, but you shouldn't overlook it.

Resources