RubyMotion Day Number of year for today - rubymotion

I want to generate the current day number using RubyMotion code. I have looked at several IOS solutions but I'm not experienced enough to translate the code successfully to RubyMotion.
I am currently at the following point:
def today
NSDate.today
end
def day_number
NSDate.from_components (day: today)
end
When I run the above it gives me an return of 3852055-06-16 00:00:00 +0100. I thought that the 3852055 part was seconds but it doesn't seem to equate to either todays date or to 16th of June - and in any case why should it be returning 06-16 instead of 02-08?? Totally confused here.
I just want to get todays day number. As I write the date is 2nd August 2014 and the day number should be 214 so I'm obviously way out somewhere.
Any help would be greatly appreciated.
cheers

This will do the trick:
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:NSDate.date)
Now suppose you have the date in the form of a string, and you want to get the day number for it:
datestr = "2014-01-01 11:08:56 +0000"
First create an NSDateFormatter to convert the String into an NSDate
df = NSDateFormatter.new
df.dateFormat = "yyyy-MM-dd HH:mm:ss z"
mydate = df.dateFromString(datestr)
daynum = NSCalendar.currentCalendar.ordinalityOfUnit(NSDayCalendarUnit, inUnit:NSYearCalendarUnit, forDate:mydate)
If your date string is simpler:
datestr = "2014-01-01"
just use a simpler dateFormat string:
df.dateFormat = "yyyy-MM-dd"

I suggest you to take a look at motion-support gem and especially at core-exttime.
You can play with dates as you want:
=> Mon, 04 Aug 2014
(main)> Time
=> Time
(main)> Time.today
=> 2014-08-04 00:00:00 +0200
(main)> Date.today.day
=> 4
(main)> Time.today.day
=> 4
and a lot more.

Related

regex to find the data

EDIT - I added all the last 50 texts, I saw that were sent from various people, unfortunately, it's not an automatic email...
list of all the text is HERE
I'm struggling to find a matched pattern that will identify the needed items (date, start time, time zone) from this text:
1 April 20 16:00-16:30 Israel Time
Tomorrow, Wed Feb 12, 08:00-9:00 AM IST(IL)
Tomorrow, Wed Jan 22, 09:30-10:00 PM PST
11-May-20 19:00-20:30 Israel Time
The start time is an easy one: (\d+:\d+)- but I'm not sure what to be done with the other words and digits.
Based on the data you provided, something like this would do it, with 3 captures as requested:
(\d+[-\s]\w+[-\s]\d+|\w+ \d+),?\s(\d+\:\d+)\-\d+\:\d+\s(?:AM\s|PM\s)?(.*)
Online reference

Regex - Slice Date - Aug 22, 2017 02:00 PM EDT

I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT
and get the month, day, year from it.
month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]
I've started with something like this:
(.*)(?<=[a-zA-Z]{3}\s)
for the month. Is there a better way to do this?
You need to first convert to datetime and then extract the needed values like this (reusing the example):
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
print(datetime_object.year, datetime_object.month, datetime_object.day)
From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.

Trouble rounding times in Stata

I am attempting to round times to the nearest 15 minute interval in Stata, so for instance Dec 31, 2017 23:58 would become Jan 01, 2018 00:00. I have time stored (based on my understanding of the documentation) as the number of milliseconds since the start of 1960. So I thought this would do it:
gen round = round(datetime, 60000*15)
However, this doesn't quite work. For instance Nov 03, 2017 19:45:27 becomes Nov 03, 2017 19:46:01, when I think I should become 19:45:00. Does anyone know what I'm missing here?
Let's show a worked example illustrating my comment that you need to store datetime values as double rather than float.
. clear
. set obs 1
number of observations (_N) was 0, now 1
. gen double datetime = clock("Nov 03, 2017 19:45:27","MDYhms")
. gen round_f = round(datetime, 60000*15)
. gen double round_d = round(datetime, 60000*15)
. format datetime round_f round_d %tc
. list, clean noobs
datetime round_f round_d
03nov2017 19:45:27 03nov2017 19:46:01 03nov2017 19:45:00

How do I get delivery slots between the times of 2pm and 5pm?

I have a model called DeliverySlot. Its attributes look like:
#<DeliverySlot:0x007f955a322cf0> {
:id => 2562,
:from => Sat, 31 Dec 2016 12:00:00 UTC +00:00
}
from is a datetime column. Delivery slots are an hour and 30 minutes apart from each other.
How can I get all delivery slots from Monday-Friday that are between the hour of 2pm (14:00) and 5pm (17:00)?
As of now I have:
Assuming, Time.now.utc.strftime('%A') is Monday.
DeliverySlot.where(from: (Time.now.utc..(Time.now.utc + 5.days).end_of_day))
I am using Postgres btw. Should I be using Postgres date functions? If so, which ones?
I know I am bit late and you probably got this down pat by now.
I created a method called slots and it takes 2 parameters they are dates.
...
# Model.slot(Date.today, Date.today+7.days)
def self.slot(start_date, end_date)
start_date.upto(end_date) do |date|
if ['Monday','Tuesday','Wednesday','Thursday','Friday'].include?(date.strftime('%A'))
(date.beginning_of_day.to_i .. date.end_of_day.to_i).step(30.minutes).each do |time|
if Time.at( time ).strftime('%R') > '13:00' && Time.at( time ).strftime('%R') < '17:00'
puts "Create Slot Date: #{date.strftime('%D')} Time: #{Time.at( time )}"
end
end
end
end
end
I hope that this helps

Week number calculation in rails

This is my first question on Stack.
I am working on a booking site that relies heavily on searching and finding full weeks of accommodation. Most user searches will be on weeknumber of the year, eg. week 27 for the first week of july.
It is important that the user does not need to fill in year when searching for accommodation, and so the only thing we will get from the user is the weeknumber.
How can I get the year from the week given by the user considering that it always has to be the next upcoming occurrence of that week number?
(There is a gotcha in this. I could get the upcoming week 27 by doing something like this:
def week
week = 27
Date.commercial(Date.current.year + 1, week, 1) # gives the first day of the week
end
But that would only be right until the 1 of January, after that it would be looking for week 27 of 2015.)
You could compare the current calendar week with Date.current.cweek (Reference) with your number.
require 'active_support/core_ext' # Already included in Rails
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013
p calendar_week(1)
# => Mon, 30 Dec 2013 # don't know if that's the way calendar weeks are counted
p calendar_week(27)
# => Mon, 30 Jun 2014