Convert Date Time to UTC in Ruby - ruby

I want to display the date and time in the following format
Sat, Jul 07, 2012 19:28:06 UTC
I haven't been able to find a conversion method that leaves the UTC at the end.
I am currently using the following
t = Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S %Z")
However this displays the following
Sat, Jul 07, 2012 19:28:06 GMT
I want to see UTC not GMT at the end.

According to the Time documentation:
The Time class treats GMT (Greenwich Mean Time) and UTC (Coordinated Universal Time) as equivalent.
My system shows "UTC" at the end when running your code. For whatever reason, your system prefers the "GMT" moniker. Since this is just cosmetic, you can use:
Time.now.utc.strftime("%a, %B %d, %Y %H:%M:%S UTC")

Related

How to convert GMT date to UNIX time in golang?

I need to convert a GMT date like (Mon, 23 Dec 2019 18:52:45 GMT) to time.Time.Unix in Go
I've tried to parse it before and then convert it to Unix format. But it's not so clean what are the predefined layouts in time.Parse
https://golang.org/pkg/time/#Parse
How to do that?
The layout of the date is RFC1123. Parse it with time.Parse() then convert it to Unix with time.Time.Unix().
t, _ := time.Parse(time.RFC1123, "Mon, 23 Dec 2019 18:52:45 GMT")
tUnix:= t.Unix()
fmt.Printf("%s in Unix is %d",t,tUnix)
// Output: 2019-12-23 18:52:45 +0000 GMT in Unix is 1577127165
Try it online
That looks like time.RFC1123. If not, refer to the same section to create your own layout reference string.

Ruby unix date incorrect

I have the following Unix timestamp: 1478698378000
And I'm trying to show this as a datetime in Ruby, e.g.
<%= Time.at(#timestamp).to_datetime %>
Which should be returning a date of: Wed, 09 Nov 2016 13:32:58 GMT but the above code actually returns a date of: 48828-02-01T13:26:40+00:00 Ignore formatting!
As you can see it thinks that timestamp is 2nd Feb 48828 13:26:40.
Why is the datetime coming out completely incorrect and the year so far into the future like that? Checking the timestamp on http://www.epochconverter.com/ reveals the timestamp to be correct, so it's Ruby that's returning it incorrectly.
Time.at expects seconds as an argument and your timestamp is an amount of milliseconds. See documentation on Time.at
Why won’t you check the unix timestamp correctness against “Fashion Week Magazine” or “Cosmopolitan” Site?
Unix timestamp is an amount of seconds lasted since 1970-01-01 UTC:
date --date='#1478698378000'
mar feb 1 14:26:40 CET 48828
BTW, dropping last three zeroes gives you back what you’ve expected:
date --date='#1478698378'
mié nov 9 14:32:58 CET 2016

Is DateTime.parse() in Ruby dependent on locale?

I'm wondering regarding the output of the following example:
when parsing 01/03, will it be resolved as Mar, 1st or Jan, 3rd?
Ruby is not locale dependent. Because Ruby is a server-side language and not a client-side language like JavaScript, Ruby uses the system clock from your web app server - and uses this information to calculate the time. Whatever timezone you have your system set to is what your Ruby app will use.
When parsing a date from a string, DateTime will make its best guess based on how the input is formatted:
DateTime.parse('01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('2019/01/03')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.parse('01/03/2019')
#=> Fri, 01 Mar 2019 00:00:00 +0000
You can also explicitly tell DateTime how you want your string parsed using strptime:
date = '01-03-2019'
DateTime.strptime(date, '%m-%d-%Y')
#=> Thu, 03 Jan 2019 00:00:00 +0000
DateTime.strptime(date, '%d-%m-%Y')
#=> Fri, 01 Mar 2019 00:00:00 +0000

Detect daylight saving time in bash

I want to detect if I'm in winter or summer time. My current approach is:
if date +%Z | grep -e CET -e EST; then
# I'm in winter time
else
# I'm in summer time
fi
which have obvious drawback as you have to know all the timezone names.
I don't now if this exactly answers your question, but it gives you some tools to help you better understand and test whats going on.
You can use date and environment-var TZ to help you.
So for example I live in Sweden so my timezone location is Europe/Stockholm. So in the winter date +%Z reports CET and in the summer CEST. The nice thing is that you could specify timezone for the environment of a specific command, then you could specify what date the date command should present. So by summing up this you could do any of the following:
TZ=Europe/Stockholm date +%Z # CET or CEST depending of when its run
TZ=Europe/Stockholm date --date=20170101 +%Z # CET
TZ=Europe/Stockholm date --date=20170601 +%Z # CEST
TZ=CET date --date=20170101 +%Z # CET
TZ=CET date --date=20170601 +%Z # CEST, note that its auto adjusted to CEST
If you instead want the time difference to UTC you could use lowercase-z:
TZ=Europe/Stockholm date +%z # +0100 or +0200 depending of when its run
TZ=Europe/Stockholm date --date=20170101 +%z # +0100
TZ=Europe/Stockholm date --date=20170601 +%z # +0200
TZ=CET date --date=20170101 +%z # +0100
TZ=CET date --date=20170601 +%z # +0200
NOTE: You can not use TZ=CEST or TZ=ULFR since that is not a valid TZ:
TZ=CEST date --date=20170101 +%z # +0000
TZ=ULFR date --date=20170101 +%z # +0000
crontab example:
We run our servers on UTC but some of the jobs run by crontab needs to be run at a specified wallclock (CET/CEST) time. So since we want the jobs to be run one hour later in the winter (the clock is put one hour forward in the summer witch makes it reach a specified UTC-time one hour earlier in the summer than in the winter) we do sleep before the actual job is executed in the winter.
We want the job /whatever/bin/foobar to be run at 04:15 wallclock time every day. But since cron runs on UTC the job needs to be set one hour earlier for CET and two hours earlier for CEST. That would be the same as always running the command two hours earlier but sleeping for an hour during winter-time. Ex crontab row:
15 2 * * * [ `TZ=CET date +\%Z` = CET ] && sleep 3600; /whatever/bin/foobar
If you have a nicer solution to this issue, then please feel free to advice me!
Perl to the rescue:
if perl -e 'exit ((localtime)[8])' ; then
echo winter
else
echo summer
fi
In the northern hemisphere, in regions with daylight savings, then it's active when the offset is greater than the offset is in January. In southern hemisphere time zones, daylight savings is active when the offset is greater than that in July.
You can discover the offsets in January and July, as well as now:
OFF_1=$(date -d '1 Jan' +%z)
OFF_7=$(date -d '1 Jul' +%z)
OFF_NOW=$(date +%z)
If your zone doesn't have daylight savings, all three will be the same (this year). In any case, we can make two comparisons, one for the northern hemisphere and one for the southern; if the current offset is greater than either one of those, then the current zone is in daylight savings time:
if test $OFF_NOW -gt $OFF_1 || test $OFF_NOW -gt $OFF_7
then
# I'm in summer time
else
# I'm in winter time
fi
The assumption here is that all regions that observe daylight savings have their transition somewhere between July and January. That's true in all zones, as far as I know. The only case where this might fail is in a year where the winter offset changes (such 1940 in the UK, when the subsequent winter was on GMT+1).
for info :
zdump -v Europe/Paris | grep 2020
gives this :
Europe/Paris Sun Mar 29 00:59:59 2020 UT = Sun Mar 29 01:59:59 2020 CET isdst=0 gmtoff=3600
Europe/Paris Sun Mar 29 01:00:00 2020 UT = Sun Mar 29 03:00:00 2020 CEST isdst=1 gmtoff=7200
Europe/Paris Sun Oct 25 00:59:59 2020 UT = Sun Oct 25 02:59:59 2020 CEST isdst=1 gmtoff=7200
Europe/Paris Sun Oct 25 01:00:00 2020 UT = Sun Oct 25 02:00:00 2020 CET isdst=0 gmtoff=3600

How can I format date -u so that the results include timezone offset in a Mac OSX terminal session?

In a terminal session I can use date -u to get
Mon Mar 16 03:34:39 2009 UTC
However, I'd like to include the offset. I'm modifying a TextMate tab trigger so that I can insert the full date including the local offset, in standard UTC format. I believe that would be in the following form:
Mon Mar 16 03:34:39 2009 UTC -0500
So, as you can see, I don't know how to get the timezone offset and combine that with formatted date results.
Try this:
echo `date -u` `date +%z`

Resources