Why can't I modify a frozen Time in Twitter gem? - ruby

I have a Tweet object from the twitter gem, called #tweet.
I am able to do:
#tweet.created_at --> `#tweet.created_at.class` outputs `Time`
However, I want to change the timezone of created_at, so I tried:
#tweet.created_at.utc
And got:
can't modify frozen Time
How would I change from UTC−08:00 that my current created_at is, to CET time?

Since the created_at time field of the tweet has been frozen, you can't modify it, so utc will raise the Exception, because it tries to change value of self. Instead of modification you shell to duplicate the variable and reassign it:
#tweet.created_at = #tweet.created_at.dup.utc

Related

Does anyone know what TimezoneOffset does on LuisPredictionOptions?

I'm sending LUIS a query that is based on a time value (e.g. "what is the time 10 minutes from now" - just an example). I want the time to come back in the local timezone, so on the LuisPredictionOptions object (C#) I set the TimezoneOffset (as an example I set it to 2 hours ahead, or 120 minutes).
In Fiddler I can see when it calls the LUIS endpoint it's correctly adding "timezoneOffset=120.0".
However, the timezone comes back as UTC - it doesn't matter whether the timezoneOffset is set, or even what it is set to, the time always comes back UTC, using the builtin datetimeV2 entity.
Does anyone know what the TimezoneOffset property is for? Am I just using it incorrectly? Is there another way perhaps to get a local time from LUIS?
[Update]: Here are some examples: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=120&log=true
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[AppId]?verbose=true&timezoneOffset=0&subscription-key=[subscriptionkey]&q=/luis/v2.0/apps/c1be57f4-3850-489e-8266-db376b82c011?timezoneOffset=240&log=true
and I'm trying the following example utterance: "in 10 minutes".
When I do this, the timex is in UTC (e.g. timex=2020-01-11T16:08:25) and the "value" comes back with the same value, minus the "T", as follows: value=2020-01-11 16:08:25
I could understand perhaps if the timex is in UTC, but then possibly "value" should be adjusted by the timezoneOffset?
It looks like there's an incorrect question mark in your URL, right before timezoneOffset.
Using the same query I was able to get the expected behavior, where the returned value is different by 10 minutes.
Which SDK are you using? Perhaps you're using the V3 Runtime SDK which uses the V3 endpoint that doesn't use timeZoneOffset but instead uses datetimeReference, and need to use the V2 Runtime SDK instead.
https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/[app-id]?verbose=true&timezoneOffset=10&subscription-key=[key]&q=in 10 minutes
The TimeZoneInfo class's FindSystemTimeZoneById method can be used to determine the correct timezoneOffset based on system time. An example in C# is shown below:
// Get CST zone id
TimeZoneInfo targetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
// Get local machine's value of Now
DateTime utcDatetime = DateTime.UtcNow;
// Get Central Standard Time value of Now
DateTime cstDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDatetime, targetZone);
// Find timezoneOffset
int timezoneOffset = (int)((cstDatetime - utcDatetime).TotalMinutes);
Reference:
https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-concept-data-alteration?tabs=V2#c-code-determines-correct-value-of-timezoneoffset

Converting a date to string representation

I have a date like this:
Date.today - 7
I tried to convert it into a string:
#last_week = strftime((Date.today - 7), '%Y-%m-%d')
But I get the error "undefined method `strftime'". What am I doing wrong?
You can do it like this:
#last_week = (Date.today - 7).strftime('%Y-%m-%d')
This is what you want, but don't do it.:
module Kernel
def strftime(date, format)
date.strftime(format)
end
end
for the reason, see below comments~~~~~
You are trying to use strftime() as though it was a standalone function. In Ruby, there is no such function. The correct way to do this is to call the method Date#strftime().
Here's an example to format today's date as a string:
Date.today.strftime("%m/%d/%y")
Now that you know how to get a date and format the date to a printable string, you can address your specific code need, which is
#last_week = (Date.today - 7).strftime("%Y-%m-%d")
This will give you the date formatted string "2016-04-28" (or thereabouts, depending on when you run the code).
There is no method as strftime on Kernel (although there is such instance method on Date), but you are trying to call such method.
Addition by #Keith Bennett
You are not calling strftime with an explicit object to receive the method, so the Ruby runtime defaults to calling the method on self, which, in this context, is the top level object, an instance of Object, which inherits from BasicObject and includes the Kernel module. None of these contain a strftime method. However, the Date method does have strftime defined. So you can do what you want to do by calling strftime on the calculated Date instance.

Why does Ruby Time.at() only return 01/01/2000 inside Heroku?

I've got a tiny Sinatra app running on Heroku, and it has a method that receives a POST and creates a database object with the parameters of the POST.
One of those params is a timestamp of the form "1432565475.000007". I want that as a date & time in the database, so I added a column, then in the method that handles the post I have:
e.time = Time.at params["timestamp"].to_f
The times come out correctly, but the dates are all Jan 1, 2000.
If I run irb at heroku ("heroku run irb") and try the above line of code manually it converts correctly. It's only when it's running inside my server instance that it interprets the calendar date wrong.
So then I created a view that just iterates through the db, converting the original timestamp (I also have that in a column) into dates. The idea being that this is more closely approximating the code that handles the POST:
-MyEvent.all.each do |me|
%p
=Time.at me.timestamp.to_i
And that works perfectly.
Any ideas?

Resolving and Fixing MongoDB / Mongoid ID issues

This is related to another StackOverflow question from a year ago. But, a bit different.
This is in Ruby/Mongoid: 2.2.6.
When I got going with MongoDB, it looks like we stored the ID of an MongoDb document incorrectly, because a simple find on an ID is not working, yet it's returning the document when we run a where query on other attributes.
I've tried "resetting" the id field by using the object returned from the where and setting the "id" and the "_id" to a BSON::ObjectId version of the stored string. This did not work, as the then record is still not queryable by this field.
Any other suggestions before I just completely wipe the disk (losing months of production data) and starting over?
Edit: An example of a document that is retuned in the loop that is not retrievable.
{"_id"=>"4f47267193546d160b0171a2", "attribute_tags"=>[{"tag"=>"website"}, {"tag"=>"twitter"}, {"tag"=>"website"}, {"tag"=>"twitter"}], "contact_info"=>{"facebook"=>[], "success"=>true, "created_at"=>2012-02-24 05:58:06 UTC, "tags"=>[], "twitter"=>[], "email"=>[], "phone"=>[], "linkedin"=>[], "google_plus"=>[], "youtube"=>[], "contact_form"=>false}, "created_at"=>2012-02-24 05:56:01 UTC, "data"=>{"twitter_followers_count"=>112, "twitter_is_translator"=>112, "twitter_protected"=>false, "twitter_url"=>"http://www.bettyunderground.com", "twitter_verified"=>false, "twitter_statuses_count"=>2040, "twitter_listed_count"=>14, "twitter_geo_enabled"=>true, "twitter_friends_count"=>124, "twitter_created_at"=>"Fri Jul 17 21:41:00 +0000 2009", "twitter_contributors_enabled"=>false, "enriched_at"=>2012-02-24 05:58:09 UTC}, "demographics"=>{}, "description"=>"The trials and tribulations of a polemicist", "directory_ids"=>[], "forums"=>[], "found_at_url"=>"http://www.bettyunderground.com", "geographics"=>{"language"=>"en", "location"=>"San Francisco, CA"}, "hashtags"=>{"tag"=>{"website"=>true, "twitter"=>true}, "reachable_via"=>{"twitter"=>true}}, "host_names"=>[], "ignore_project_ids"=>[], "keyword_scores"=>{"return policy"=>0.0}, "keywords"=>["return policy"], "last_contact_info_update"=>2012-02-24 05:58:09 UTC, "name"=>"Betty Underground", "new_profiles"=>[{"service"=>"twitter", "user_id"=>"BettyUndergrnd", "score"=>1.0}, {"service"=>"twitter", "username"=>"BettyUndergrnd", "score"=>1.0}], "presence_score"=>0, "profile_url"=>"http://a2.twimg.com/profile_images/1459407098/image_normal.jpg", "profiles_retrieved"=>true, "references"=>[], "share_counts"=>{}, "tags"=>["website", "twitter"], "twitter"=>"BettyUndergrnd", "updated_at"=>2012-03-17 10:08:09 UTC, "wordsmaster_ids"=>[], "reachable_via"=>[], "read_project_ids"=>[]}
It doesn't have a ObjectId for the ID field. Not sure how it got busted this way, but that's the way it is.
The code I'm using to modify it is:
#if d is the document
old_id = d._id
d["_id"] = BSON::ObjectId(old_id)
d.save
I have placed a gist of doing this from my console. You can see exactly what I'm doing.
Any thoughts would be appreciated.
https://gist.github.com/2087011
_id field is immutable. You have to insert a new document with a new value of _id and delete the old one.
In Mongoid, there is a rake task to convert the ObjectIds.
If you use this, you'll have a mirror of your collection. Then, simply rename and you'll be set.
It will error if you have duplicate object_ids though, so you might need to run it a few times.
And, it's SLOW.

Saving GMAIL Attachment as 'Subject - (Date of message)' using ruby

I am trying to incorporate this script here (http://snippets.dzone.com/posts/show/7530), yet i'm no ruby wizard.
Right now it pulls down mail that I want with the local_file name, but was wondering if its possible to have it pull it down and save it by the 'message subject title + date of message'. When I try using mail.date, I get a longer than normal string, and can't convert it with Time.parse().
For instance, right now it pulls down an attachment as 103134.htm, which has no significance to me, and I'd rather have it be "Logs from Blah - 11/12/2009.htm" since it pulls the subject and date from the original message instead of the attachment local_name.
Any help would be greatly appreciated!
Thanks!
mail.date already returns a Time object, so it is not required that you parse it again. Instead use mail.date.strftime("%m/%d/%Y") to get your desired output

Resources