Find out what time zone an RSS feed item was published - xml-parsing

I'm parsing an RSS 2.0 feed which has missing time zone information in its pubDate tag.
For example, the element is written as such:
<pubDate>Tue, 3 Dec 2013 09:00:00</pubDate>
How can I figure out or find out the time zone from this node element?

If you cannot infer or tell what the time zone is for an RSS feed item, then the item is not valid.
A valid <pubDate> element will include the time zone, or time zone hours offset.
According to the RSS 2.0 specifications,
All date-times in RSS conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).
The following are valid examples, and you can determine what time zone the item was published at:
<pubDate>Wed, 02 Oct 2002 08:00:00 EST</pubDate>
<pubDate>Wed, 02 Oct 2002 13:00:00 GMT</pubDate>
<pubDate>Wed, 02 Oct 2002 15:00:00 +0200</pubDate>
The options available then are to ask the feed publisher to update the <pubDate> field with the correct date format, or somehow note what time zone you believe the particular source is publishing from.

Related

Google Spread Sheet Script. Working with Dates

I am reading list of Google Sheet cells containing DateTime, using Google Apps Script.
The values in the cells are:
A1: Jul 26 13:00
A2: Jul 27 0:00
var dateValues = SpreadsheetApp.getActiveSheet().getRange("A1:A2").getValues();
However, values read are 1 hour behind. This is what I see in the debugger:
dateValues[0] = Wed Jul 26 2017 12:00:00 GMT+0200 (EET)
dateValues[1] = Wed Jul 26 2017 23:00:00 GMT+0200 (EET)
I guess this is a time zone issue, but I don't really understand the concept.
My time zone is currently (due to DTS) GMT + 3. Indeed, outside the DTS period it is GMT +2. The spread sheet time zone is Jerusalem GMT+2.
EET - don't underrated why it is being used.
Basically, I would expect to get in code the values with in the sheet.
What is the concept?
there are two ways to solved this
Use getDisplayValues() rather than getValues(). This will force to do some conversions, as getDisplayValues() returns strings not dates
make you script editor time zone match the sheet tz.
In my case the sheet was (GMT+2 Jerusalem), but the script editor was different (GMT+2 Moscow) for some reason.
Setting the script editor TZ, solved the problem.

Daylight Savings Time ignored using in_time_zone Rails 4

I'm having a frustrating issue that I can't seem to narrow down. I have searched many similar articles but they are not close enough to my issue to resolve. I am trying to pull a time from the database and display it in more than one time zone. My Rails app is using UTC as default. Here is what I'm doing:
On the create action I take the string of time which will be saved in the time column in my DB:
params[:schedule][:start] = "09:00"
Time.zone = "Central Time (US & Canada)"
#schedule.start = Time.zone.parse(params[:schedule][:start])
The above formats the time as it is supposed to:
2016-04-12 09:00:00 -0500
This is saved in the DB as:
2000-01-01 14:00:00
This has no time offset which is fine since I know it's in UTC. The problem happens when I go to display the time:
#schedule.start.in_time_zone("Central Time (US & Canada)")
This returns:
Sat, 01 Jan 2000 08:00:00 CST -06:00
Now, since this is a time column, I don't care about the date. I plan on formatting the value to only show the time. However, it is showing CST when it is currently CDT.
I can't figure out why this is happening. As I said I am not setting the Time Zone anywhere in my application.rb or anywhere else and I only set the Time zone on the create action which should be fine when moving to a new action.
Any help on clarifying this would be awesome!
This seems to be because when the time is stored it is stored with the date in the year 2000-01-01 which seems to be why it is using CST. How can I ignore the date when converting it to a particular timezone or will I need to change the column type to DateTime to get this to work properly?
It is showing CST simply because the time is read from the database including the stored date, i.e. it's read as 09:00 of Jan 1st 2000.
I guess you'd have to parse the time upon reading the attribute back. You can use a helper method in your model, for example:
# schedule model
def start_in_zone(zone)
self.start.strftime("%H:%M").in_time_zone(zone)
end
This will take only the hours and minutes part of the stored time and parse it in the given time zone with the date set to today. See this example:
"Sat, 01 Jan 2000 08:00:00".to_time.
strftime("%H:%M").
in_time_zone("Central Time (US & Canada)")
# => Tue, 12 Apr 2016 08:00:00 CDT -05:00
The fact that it matters whether it's CST or CDT means you do, on some level, care about the date. While I'm not familiar with the exact rules of Daylight Savings in that region, I do know that Jan 1 is the middle of winter and will definitely not be on Daylight Savings time.
Add the relevant date into your #schedule before putting it into a time zone, and it should fix the problem.

Moment JS Show Local Time from DateTime.UTCNow

I have a JSON method which is returning a UTC DateTime I am storing on the server. It returns the following:
/Date(1394155885817)/
I'm trying to figure out how to get Moment JS to show me the local (browser) time. So my timezone is -5 (EST) I'd want to see 3:31 instead of 8:31. See the fiddle below.
http://jsfiddle.net/R9UbS/
What am I doing wrong here? How can I force Moment to return local?
I just ran the fiddle in Romania and got 3:31. Also, javascript's Date constructor handles UTC millis correctly. You're getting 8:31 which makes me think it's doing what it's supposed to do, showing the local time. Why is 8:31 incorrect? And is your device's timezone set correctly? (Btw if your timezone is GMT -5 then you would indeed get 8:31 PM the previous day)
Just to put this a bit in perspective, i put the number in a milliseconds to time converter and i saw that 1394155885817 actually means Fri Mar 07 2014 1:31:25 AM in UTC (or GMT for convenience) - subtracting 5 hours from this would result in Fri Mar 06 2014 8:31:25 PM

Timezone issue in Blackberry

I am working on a project which requires me to create a Date value by setting it to a particular timezone .
I have used the following code
Date formatter1 = new Date(HttpDateParser.parse("2013-08-02 11:00:00"));
System.out.println("Date dd formatter1"+formatter1);
Result as follows:
Fri Aug 02 16:30:00 Asia/Calcutta 2013
After parsing, it is giving me time according to device time zone ...
adding 5:30 automatically if device time zone is set to India Kolkata.
I want result to be as :
Fri Aug 02 11:00:00 Asia/Calcutta 2013
I mean it should not add the offset as reference to GMT .
How could I work upon this code to set the data required to me as per the Timezone and not change it internally ?
One problem is that your original date string:
2013-08-02 11:00:00
does not include time zone information. So, it is being interpreted as a GMT time. Which then means that, displayed in Calcutta time, it will be
Fri Aug 02 16:30:00 Asia/Calcutta 2013
You want to specify that 11:00 is already in Calcutta time. To do that, use one of the formats defined in the HttpDateParser documentation:
// we make sure to specify time zone information "+05:30"!
long timeSinceEpoch = HttpDateParser.parse("2013-08-02T11:00:00+05:30");
Date date = new Date(timeSinceEpoch);
System.out.println("Date: " + date);
// use this to slightly change the date formatting ... same time zone
String pattern = "yyyy-MM-dd hh:mma";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
String formattedDate = formatter.formatLocal(timeSinceEpoch);
System.out.println("SimpleDateFormat: " + formattedDate);
Note: that in addition to adding "+5:30" to the time string, you have to replace a space after the date with a 'T'.
This code will output:
[0.0] Date: Fri Aug 02 11:00:00 Asia/Calcutta 2013
[0.0] SimpleDateFormat: 2013-08-02 11:00am
if the device's time zone is actually set to Calcutta (Kolkata +5.5).
References
Have a look at this answer on Stack Overflow.
and maybe this one, too.

Rails: how to create Time object in specific time zone

My app is working in "Moscow" (+04:00) timezone. But sometimes I need to create time object by only local time (for example "01 may 2012 13:45") and name of ActiveSupport::TimeZone object (for example "Berlin": +02:00 in Summer Time and +01:00 otherwise).
For example if I get "01 may 2012 13:45" and "Berlin" as input I want to yield "2012-05-01 13:45:00 +0200" or "2012-05-01 11:45:00 +0000". I create following function:
def from_local_datetime(local_datetime, time_zone)
offset = Time.now.in_time_zone(time_zone).formatted_offset
datetime = case local_datetime
when String
DateTime.parse(local_datetime)
else
DateTime.new(local_datetime)
end.change(:offset => offset)
return datetime
end
And at the first look it works as I expected. But is it a best practice for this kind of task? May be in some situation It works with errors. I'm not definitely sure.
I would be greatful to any comments.
UPD: I think bug may occur about time when DST changing the time. For example 26 march 2011 was GMT+1 in Berlin time zone and Time.now.in_time_zone("Berlin").formatted_offset returns "GMT+1", but it would be GMT+2 in 27 march 2011. So if I call from_local_datetime("28 march 2011", "Berlin") before 27 march it returns 28 march 2011 00:00:00 +0100, but If I call it after changing the time my function returns 28 march 2011 00:00:00 +0200 :(
Your conversion method is the right approach.
With web sites, you should make sure times are stored as UTC in the database. If you can get the UTC value out of the database, instead of the local time (or maybe you can set your web server's time zone to UTC) it won't have to convert the time from UTC to local time, when you are going to then convert it to the user's timezone anyway.
And, of course, you will have to store the user's time zone preference.
TZInfo::Timezone.get('Europe/London')
Find the time zone
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

Resources