How can I make time ranges for an iCal calendar from a database of entries? - macos

I've got some Perl script that I'm using to scrape calendar events. I've got a database of events, and now I'd like to process these into an iCal file. I opened my university's academic iCal calendar and got the following:
BEGIN:VCALENDAR
CALSCALE:GREGORIAN
X-WR-TIMEZONE;VALUE=TEXT:America/Los_Angeles
PRODID:-//MMISoftware\, //MMISoftware//EN
X-WR-CALNAME;VALUE=TEXT:spring14cal
X-WR-RELCALID;VALUE=TEXT:87FB4C57-1D7B-4B80-B200-AEED845CB3AF
VERSION:2.0
BEGIN:VEVENT
SEQUENCE:1
UID:41D26C6E-965B-4BFB-AEC0-F1614074A4AF
DTSTAMP:20140127T111052Z
DTSTART;VALUE=DATE:20140101
SUMMARY:FAFSA
DESCRIPTION:First day to obtain FAFSA financial aid applications for 201
4-15 academic year online at http://www.fafsa.ed.gov
DURATION:P1D
END:VEVENT
BEGIN:VEVENT
SEQUENCE:1
UID:DFE257FB-F475-43C2-9AEB-569EC4BA321D
DTSTAMP:20140127T111052Z
DTSTART;VALUE=DATE:20140106
SUMMARY:CA Dream
DESCRIPTION:First day to obtain California Dream Act financial aid appli
cations for 2014-15 academic year online at https://dream.csac.ca.gov
.
.
.
.
I see a start date, end date, and a time stamp, but I don't see a range... Not sure how to use my existing database to make an iCal file---you know, metaprogramming---with time ranges like the following:
http://musformation.com/pics/thg_sync_apple_ical.jpg

You could use Date::ICal to format the duration strings.
The duration itself ( in your case we can see only the duration of the first event, because the duration of the second event would be the first line you skipped ) is DURATION:P1D, where P1D is the Duration itself and DURATION is the property name.
Further information about ICals Duration can be found here.
When you want to parse ICal files, you can use ICal::Parser.
Creating ICal Files can be done easily with Data::ICal. An example:
use Data::ICal;
use Data::ICal::Entry::Event;
use strict;
use warnings;
my $calendar = Data::ICal->new;
my $vtodo = Data::ICal::Entry::Event->new;
$vtodo->add_properties(
summary => "read SO",
description => "reading cool SO posts",
dtstart => Date::ICal->new(
day => 4,
month => 6,
year => 2014,
hour => 8,
min => 30,
sec => 00
)->ical,
dtend => Date::ICal->new(
day => 5,
month => 6,
year => 2014,
hour => 9,
min => 50,
sec => 00
)->ical,
);
$calendar->add_entry($vtodo);
print $calendar->as_string;

Related

Working with Rails Timezones?

Working on a 3 week new registration chart for metrics, I have the following code:
(3.weeks.ago.to_date..Date.today).map { |date| Metrics.registrations_on(date) }
In Metrics.rb:
def self.registrations_on(date)
date = date.midnight
end_date = date + 24.hours
User.where(:created_at => date..end_date).count
end
Before the day is done here in California, a new day's numbers are already starting to increase. The created_at timestamp is UTC as well.
I'd like to be able to see the stats from today, using our time zone. With my data already saved as UTC I'm curious as to how about accomplishing this.
Open config/application.rb, find config.time_zone, and assign it with appropriate value:
config.time_zone = 'Pacific Time (US & Canada)'
Restart your app, and all ruby date/time operation should be adjusted automatically to your time zone.
For a list of all supported time zone strings, use:
bundle exec rake time:zones:all

Parsing iCal from Outlook: how do I tell what the recurrence schedule is for this event?

I'm using Python, but I don't think that's relevant here. The iCal snippet below is from an Outlook 2010 export (full data). In Outlook, the event shows up as recurring, including an instance of the event on April 12th, 2012. If you open the series, it says
recurrence: Occurs every Thursday effective 3/29/2012 from 12:00 PM to 12:30 PM
My question is: is it possible to derive the recurrence schedule from the information below? Which fields will give me the information? I would expect to find an RRULE, but there's no such thing here.
BEGIN:VEVENT
CLASS:PUBLIC
CREATED:20120312T133301Z
DESCRIPTION:\n
DTEND;TZID="Eastern Standard Time":20120329T123000
DTSTAMP:20120411T220938Z
DTSTART;TZID="Eastern Standard Time":20120329T120000
LAST-MODIFIED:20120531T155022Z
LOCATION:1501 Fake Street\, Conference Room G
PRIORITY:5
RECURRENCE-ID;TZID="Eastern Standard Time":20120419T120000
SEQUENCE:8
SUMMARY;LANGUAGE=en-us:My Cool Event
TRANSP:OPAQUE
UID:040000008200E00074C5B7101A82E008000000000029934B3300CD01000000000000000
0100000001516438BA45C3946AF9C4C2A563FB2BE
X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//E
N">\n<HTML>\n<HEAD>\n<META NAME="Generator" CONTENT="MS Exchange Server ve
rsion 14.02.5004.000">\n<TITLE></TITLE>\n</HEAD>\n<BODY>\n<!-- Converted f
rom text/rtf format -->\n\n<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG
="en-us"></SPAN></P>\n\n</BODY>\n</HTML>
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-DISALLOW-COUNTER:FALSE
X-MS-OLK-APPTLASTSEQUENCE:16
X-MS-OLK-APPTSEQTIME:20120411T220937Z
X-MS-OLK-AUTOFILLLOCATION:FALSE
X-MS-OLK-CONFTYPE:0
END:VEVENT
I ran through your ICS file with following program:
from icalendar import Calendar, Event
from datetime import datetime
cal = open('test.ics','rb')
ical = Calendar.from_ical(cal.read())
for component in ical.walk():
if component.name == 'VEVENT':
for item in component.sorted_items():
if item[0] == 'RECURRENCE-ID':
reoccur_item = item[1]
print reoccur_item.params
print reoccur_item.dt
continue
if item[0] == 'DTSTART':
print 'DSTART', item[1].dt
continue
if item[0] == 'DTEND':
print 'DTEND', item[1].dt
continue
if item[0] == 'DTSTAMP':
print 'DTSTAMP', item[1].dt
continue
print item
cal.close()
And following is the output that I obtained
('SUMMARY', vText(u'My Cool Event'))
DSTART 2012-03-29 12:00:00
DTEND 2012-03-29 12:30:00
DTSTAMP 2012-04-11 22:09:38+00:00
('UID', vText(u'040000008200E00074C5B7101A82E008000000000029934B3300CD01000000000000000 0100000001516438BA45C3946AF9C4C2A563FB2BE'))
RECURRENCE-ID Parameters({'TZID': 'Eastern Standard Time'})
RECURRENCE-ID 2012-04-19 12:00:00
('SEQUENCE', 8)
('CLASS', vText(u'PUBLIC'))
('CREATED', <icalendar.prop.vDDDTypes instance at 0x101c4e518>)
('DESCRIPTION', vText(u'\n'))
('LAST-MODIFIED', <icalendar.prop.vDDDTypes instance at 0x1020874d0>)
('LOCATION', vText(u'1501 Fake Street, Conference Room G'))
('PRIORITY', 5)
('TRANSP', vText(u'OPAQUE'))
('X-ALT-DESC', vText(u'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//E N">\n<HTML>\n<HEAD>\n<META NAME="Generator" CONTENT="MS Exchange Server ve rsion 14.02.5004.000">\n<TITLE></TITLE>\n</HEAD>\n<BODY>\n<!-- Converted f rom text/rtf format -->\n\n<P DIR=LTR><SPAN LANG="en-us"></SPAN><SPAN LANG ="en-us"></SPAN></P>\n\n</BODY>\n</HTML>'))
('X-MICROSOFT-CDO-BUSYSTATUS', vText(u'BUSY'))
('X-MICROSOFT-CDO-IMPORTANCE', vText(u'1'))
('X-MICROSOFT-DISALLOW-COUNTER', vText(u'FALSE'))
('X-MS-OLK-APPTLASTSEQUENCE', vText(u'16'))
('X-MS-OLK-APPTSEQTIME', vText(u'20120411T220937Z'))
('X-MS-OLK-AUTOFILLLOCATION', vText(u'FALSE'))
('X-MS-OLK-CONFTYPE', vText(u'0'))
The re-occurrence rule is empty and this almost looks like single instance of the re-occuring event but for the various Microsoft specific data in the end. This has sequence number 8 and X-MS-OLK-APPTLASTSEQUENCE:16 suggests that the last instance should have sequence 16.
It almost looks like it has created multiple instances with sequence stamp on each of team with the same UID
did you try exporting the calendar using vba? this might be an option for you to get the rrule. you would have to watch for Item.GetRecurrencePattern (Item being declared as myItem As AppointmentItem) and compare to olRecursMonthly, olRecursYearly, ... then look for the interval and count attributes of your item to rebuild the whole rrule string.
you can find more details at this project: http://sourceforge.net/projects/outlook2ical/files/outlook2ical/v1.04/

Ruby on Rails 3: Why is the conversion from Date to seconds different from Time to seconds

In Ruby I am trying to convert a Date into a format that is usable by the HighCharts JavaScript charting library. Odd thing is when I convert the Date to seconds it converts differently than when I convert a Time to seconds and differently when I convert a DateTime to seconds. Due to this difference in conversion the dates displayed on the Graph can be as much as 1 date behind.
I am sure this has something to do with Rails and how it handles conversion from UTC to Local. If someone could explain to me the details I would greatly appreciate it.
In my examples below I use the same date '2011/05/02' but the seconds come out to be different.
Examples:
Date.new(2011, 5, 2).to_time.to_i * 1000
=> 1304265600000
=> 05/01/2011
Time.utc(2011, 5, 2).to_i * 1000
=> 1304294400000
=> 05/02/2011
Date.new(2011, 5, 2).to_datetime.to_i * 1000
=> 1304294400000
=> 05/02/2011
ruby-1.9.2-p180 :106 > Time.utc(2011, 5, 2)
=> 2011-05-02 00:00:00 UTC
ruby-1.9.2-p180 :107 > Date.new(2011, 5, 2).to_time
=> 2011-05-02 00:00:00 +0300
Date.to_time generates Time with timezone. That's your difference.
The quick fix/hack that popped instantly into my mind is:
Date.new(2011, 5, 2).to_time.utc.midnight
Edit:
http://apidock.com/rails/Date/to_time
Date.new(2011, 5, 2).to_time(:utc)

Set time part of DateTime in ruby

Say I have a datetime object eg DateTime.now. I want to set hours and minutes to 0 (midnight). How can I do that?
Within a Rails environment:
Thanks to ActiveSupport you can use:
DateTime.now.midnight
DateTime.now.beginning_of_day
OR
DateTime.now.change({ hour: 0, min: 0, sec: 0 })
# More concisely
DateTime.now.change({ hour: 0 })
Within a purely Ruby environment:
now = DateTime.now
DateTime.new(now.year, now.month, now.day, 0, 0, 0, now.zone)
OR
now = DateTime.now
DateTime.parse(now.strftime("%Y-%m-%dT00:00:00%z"))
Nevermind, got it. Need to create a new DateTime:
DateTime.new(now.year, now.month, now.day, 0, 0, 0, 0)
Warning: DateTime.now.midnight and DateTime.now.beginning_of_day return the same value (which is the zero hour of the current day - midnight does not return 24:00:00 as you would expect from its name).
So I am adding this as further info for anyone who might use the accepted answer to calculate midnight x days in the future.
For example, a 14 day free trial that should expire at midnight on the 14th day:
DateTime.now.midnight + 14.days
is the morning of the 14th day, which equates to a 13.x day trial (x is the part of the day left over - if now is noon, then it's 13.5 day trial).
You would actually need to do this:
DateTime.now.midnight + 15.days
to get midnight on the 14th day.
For this reason I always prefer to use beginning_of_day, since that is 00:00:00. Using midnight can be misleading/misunderstood.
If you use it often consider install this gem to improve date parse:
https://github.com/mojombo/chronic
require 'chronic'
Chronic.parse('this 0:00')

Business date/holiday handling

I've posted this question for C# but I may be working in Ruby instead. So I'm asking the same question about Ruby:
I'm looking for a Ruby class/library/module that works similarly to the Perl module Date::Manip as far as business/holiday dates. Using that module in Perl, I can pass it a date and find out whether it's a business day (ie, Mon-Fri) or a holiday. Holidays are very simple to define in a config file (see Date::Manip::Holidays). You can enter a 'fixed' date that applies to every year like:
12/25 = Christmas
or 'dynamic' dates for every year like:
last Monday in May = Memorial Day
or 'fixed' dates for a given year like:
5/22/2010 = Bob's Wedding
You can also pass in a date and get back the next/previous business day (which is any day that's not a weekend and not a holiday).
Does anyone know of anything like that in the Ruby world?
You may use the holidays-gem.
http://rubygems.org/gems/holidays
Some national (and regional) holidays are already predefined, you may define your own holiday definitions.
The business_time gem should do what you need.
The example at bottom of the README doc is a good starting example:
require 'rubygems'
require 'active_support'
require 'business_time'
# We can adjust the start and end time of our business hours
BusinessTime::Config.beginning_of_workday = "8:30 am"
BusinessTime::Config.end_of_workday = "5:30 pm"
# and we can add holidays that don't count as business days
# July 5 in 2010 is a monday that the U.S. takes off because
# our independence day falls on that Sunday.
three_day_weekend = Date.parse("July 5th, 2010")
BusinessTime::Config.holidays << three_day_weekend
friday_afternoon = Time.parse("July 2nd, 2010, 4:50 pm")
tuesday_morning = 1.business_hour.after(friday_afternoon)
You probably going to need the chronic gem to help you build the holiday dates from your config file. However YMMV because your example last monday in may doesn't work in chronic. Hackaround is do something like this:
# last monday in May (2010)
Chronic.parse('last monday', :now => Time.parse('2010-06-01'))
And look at the tickle gem which works on top of chronic for a way to add recurring events.
/I3az/
You could take a look at my Workpattern gem. It allows you to specify working and resting times. It was aimed at producing a "Calendar" like is used in planning tools such as Microsoft Project and Primavera P6, so you can specify right down to the minute.
Here is a simple example:
Create a new Workpattern mywp=Workpattern.new('My Workpattern',2011,10) This is for 10 years from 2011 but you can make it longer or shorter.
Tell it you want the Weekends to be resting and that you also want to rest during the week so you work between 9 and 12 in the morning and 1 and 6 in the afternoon.
mywp.resting(:days => :weekend)
mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(0,0),:to_time=>Workpattern.clock(8,59))
mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(12,0),:to_time=>Workpattern.clock(12,59))
mywp.resting(:days =>:weekday, :from_time=>Workpattern.clock(18,0),:to_time=>Workpattern.clock(23,59))
Now just calculate using minutes
mydate=DateTime.civil(2011,9,1,9,0)
result_date = mywp.calc(mydate,1920) # => 6/9/11#18:00
1920 is 4 days * 8 hours a day * 60 minutes and hour.
I wrote the gem to learn Ruby - only scratched the surface.
Check out the biz gem.
Here's an example configuration:
require 'biz'
Biz.configure do |config|
config.hours = {
mon: {'09:00' => '17:00'},
tue: {'00:00' => '24:00'},
wed: {'09:00' => '17:00'},
thu: {'09:00' => '12:00', '13:00' => '17:00'},
sat: {'10:00' => '14:00'}
}
config.holidays = [Date.new(2014, 1, 1), Date.new(2014, 12, 25)]
config.time_zone = 'America/Los_Angeles'
end
When you use the optional core extensions, it's as easy as the following to find out if a date is a business day:
require 'biz/core_ext'
Date.new(2014, 12, 25).business_day? # => false

Resources