How to parse week and year with strptime - ruby-on-rails

Usually I don't have issues parsing strings to Time in Rails, however I cannot understand why it's behaving like this:
irb(main):073:0> DateTime.strptime("20/2020", "%V/%Y")
=> Wed, 01 Jan 2020 00:00:00 +0000
I want the 20th week of 2020, not the 1st Jan...

I think that you need to use the commercial method.
require 'date'
week = 20
start_week = Date.commercial(2020, week)
# => Mon, 11 May 2020
puts "#{start_week}"
# => 2020-05-11
In case you need a Range, you can do:
Date.commercial(2020, week).all_week
# => Mon, 11 May 2020..Sun, 17 May 2020
For more information see the documentation.

require 'date'
If week 1 begins on Sunday:
Date.strptime("20/2020", "%U/%Y")
#=> #<Date: 2020-05-17 ((2458987j,0s,0n),+0s,2299161j)>
and on Monday:
d = Date.strptime("20/2020", "%W/%Y")
#=> #<Date: 2020-05-18 ((2458988j,0s,0n),+0s,2299161j)>
and also from the doc Date#strftime (which contains the formatting directives for Date::strptime), "The days in the year before the first week are in week 0".

There is generally a difference between %Y and %G for the year depending on your assumed definition for a week number.
For ISO 8601 weeks (which are commonly used in e.g. Europe), you have to use the week-based year here (%G) rather than the day-=based year (%Y). This is important around the edges of the years. For example, 2021-01-01 is in week 53 of 2020. On this date
%G is 2020, but
%Y is already 2021
If you want to parse an ISO 8601 week, you can thus use the following code to ensur you use the right week and year:
require 'date'
DateTime.strptime("20/2020", "%V/%G")
# => #<DateTime: 2020-05-11T00:00:00+00:00 ((2458981j,0s,0n),+0s,2299161j)>

Related

Create TimeWithZone object from integer (unix epoch time with millisecond precision) and with specified zone (string)

I have an integer that represents a unix epoch time in millisecond precision and a time zone string. I need to create a TimeWithZone object with them.
epoch_ms_integer = 1586653140000
time_zone = "America/Los_Angeles"
Trying to convert to:
Sat, 11 Apr 2020 20:59:00 PDT -07:00
I was able to accomplish this by doing:
Time.at(epoch_ms_integer/1000).asctime.in_time_zone("America/Los_Angeles")
but was wondering if this is the best way to achieve this. The app I'm working on is configured to EST/EDT time zone so Time.at(epoch_ms_integer/1000) returns 2020-04-11 20:59:00 -0400.
I was able to find the asctime solution in one of the answers here Ruby / Rails - Change the timezone of a Time, without changing the value
the same question was asked here but no answer converting epoch time with milliseconds to datetime.
Assuming that the timestamp is in milliseconds, then 1586653140000 is
Epoch: 1586653140
GMT: Sunday, April 12, 2020 12:59:00 AM
PDT: Saturday, April 11, 2020 17:59:00 PM -in time zone America/Los Angeles
These are just 3 different ways to refer to a specific point in time around the world.
Sat, 11 Apr 2020 20:59:00 PDT -07:00 and 2020-04-11 20:59:00 -0400 each refer to different points in time and not the same as epoch(1586653140)
Since the Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), it wouldn't make sense to take 1586653140 and only change the time zone without adding the zone's offset because now you are talking about another point in time.
To get the right "translation" from the epoch to any time zone you could just do
Time.zone = "GMT"
Time.zone.at(1586653140)
=> Sun, 12 Apr 2020 00:59:00 GMT +00:00
Time.zone = "America/Los_Angeles"
Time.zone.at(1586653140)
=> Sat, 11 Apr 2020 17:59:00 PDT -07:00
When working with dates in time zones in rails it is important to only use functions that take the set time zone into account:
DON’T USE
Time.now
Date.today
Date.today.to_time
Time.parse("2015-07-04 17:05:37")
Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")
DO USE
Time.current
2.hours.ago
Time.zone.today
Date.current
1.day.from_now
Time.zone.parse("2015-07-04 17:05:37")
Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
Also keep in mind that in a Rails app, we have three different time zones:
system time,
application time, and
database time.
This post by thoughtbot explains things clearly.

Why does adding and subtracting 2 months to a date not give back the same date?

I'm a bit confused about this outcome, taking today's date minus 2 months, and then taking that date again and adding two months, does not give me today's date when assign the dates to a variable.
Time.zone
"Eastern Time (US & Canada)"
> today = Date.today.in_time_zone
=> Thu, 31 Aug 2017 00:00:00 EDT -04:00
> a = today - 2.months # This is persisted to the db
=> Fri, 30 Jun 2017 00:00:00 EDT -04:00
> b = a + 2.months
=> Wed, 30 Aug 2017 00:00:00 EDT -04:00
If I however, just use the same object, it moves back and forth properly:
> today = Date.today.in_time_zone
=> Thu, 31 Aug 2017 00:00:00 EDT -04:00
> today - 2.months
=> Fri, 30 Jun 2017 00:00:00 EDT -04:00
> today + 2.months
=> Tue, 31 Oct 2017 00:00:00 EDT -04:00
The problem is obviously when "a" gets saved to a database, and then retrieved later on, and calculated plus 2 months..., it should match today's date.
TL;DR
A month is not a fixed duration. Adding or taking a month does not give the same "time shift" depending on which day you are.
The usual algorithm
to add or take months is the following :
try to land on the same day number (4th, 30th, 31st) as you started, just by changing the month
if you would land on an impossible date (like 31th September, 30th February, 29th February for some years) then just go the maximum allowed day number of this month
This implies that adding some months then taking out the same number of months will not necessarily give you back the same date.
Examples :
31st of some month + 1 month --> One would want to get to the 31th of next month
But if there is no 31st of next month (like for 31th of August, no 31st of September), then what to do ?
Usual interpretation would say that you want to go to the end of the month, this is 30th September (for rent or other monthly subscription, for instance)
But usually, 30th of some month - 1 month --> One would want to get to the 30th of the previous month.
That would lead to .... 30th of August. Not 31th of August.
Hence: some date + 1 month - 1 month does not necessarily give the original date !
Another example :
Start at the 30th of August.
Take a month -> 30th of July
Add a month -> You want to get to 30th of August (same number, next month) or to the end of August ?
The default algorithm will try to give the same day number -> 30th of August (which is more logical now)
Also with days...
Note that the same problem happens with days,but much less often ! When some days don't have the same number of hours, for daylight saving days, when adding and taking same number of days you might not get back to the original date and time as you started from.

Traverse through a certain month in Date with just a given integer

Would it be possible to go to a certain month of the year with just a given integer. For example
date = Date.today
=> Wed, 30 Dec 2015
What if I want to go back to a certain month based on that date and I am just given a number let's say 7 which is July in the Date::MONTHNAMES so would it be possible to do something like
date = Date.today
=> Wed, 30 Dec 2015
date.go_to_month_of(7) # which will bring me back to July 30, 2015
Okay I found it. It's:
date = Date.today
date.change(:month => x)
Hope this helps you!

Rails, day of year to date

I have day of the year. For example 15th day of 2014, or 210th day of 2014. I need to get the Date on the particular day. Is there any library function in Rails/Ruby that I can use, or any other elegant way?
Something like:
15th day of 2014 = 15-Jan-2014
210th day of 2014 = 29-Jul-2014
You can use Date.ordinal for that:
require 'date'
Date.ordinal(2014, 210)
# => #<Date: 2014-07-29 ((2456868j,0s,0n),+0s,2299161j)>
Date.new(2014, 1, 1) + 210
=> Wed, 30 Jul 2014
Edit - you'd need to subtract 1 day. I prefer #wonderb0lt's suggestion.

Ruby On Rails, week number is not the correct one (-1)

I need the current week number, if I'm not totally mistaken it's week 51 right now? However when testing it in the console I get this.
Time.now
=> 2013-12-19 11:08:25 +0100
Time.now.strftime('%U')
=> "50"
Date.today
=> Thu, 19 Dec 2013
Date.today.strftime("%U").to_i
=> 50
Why is that?
Time.now.strftime('%V') will give you the week number according to ISO 8601.
why is that?
according to %U or %W, The days in the year before the first week are in week 0 (00..53).
with %V (as #Graeme McLean wrote), The days in the year before the first week are in the last week of
the previous year (01..53).
From here.
Hmm, I'm unsure as to why it is that way, but to get the correct one using Ruby, I use this:
require 'Date'
week_number = Date.today.cweek #=> 51

Resources