I'm having a lot of troubles playing with the facebook API recently:
I'm fetching start_time from an event and I get this:
2012-11-14T04:00:00+0000
Which is pacific time I think since the event occurs the 13th at 7pm in France (my timezone).
How do I convert this to my timezone?
PS:I use PHP
EDIT: I've found a work around that doesn't work all the time, I just write
date_default_timezone_set('America/Los_Angeles');
before using the dates functions. But it doesn't work for some modified dates. Although I really think it comes from facebook... unfixable bug?
$time = '2012-11-14T04:00:00+0000';
$dt = new DateTime($time, new DateTimeZone('PST'));
$dt->setTimezone(new DateTimeZone('CET'));
echo $dt->format('Y-m-d H:i:s'); // 2012-11-14 05:00:00
Facebook uses the ISO 8601 time format.
The +0000 at the end means it is UTC time not pacific time.
date_default_timezone_set('California');
Try California timezone. Works pretty good for me.
Related
my $apptStartDateTime = "20210401100000";
my $formatter = DateTime::Format::Strptime->new(pattern => "%Y%m%d%H%M%S", time_zone => "MST");
my $dt_obj = $formatter->parse_datetime($apptStartDateTime);
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n"; #prints 20210401100000
# to convert to a different zone:
$dt_obj->set_time_zone("America/Los_Angeles");
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n";#prints 20210401100000
The above does not convert from MST to America/Los_Angeles. Can someone please help? I am new to Perl. Also, would the above code take care of DST?
I think you meant to use US Mountain Time, which is identified by America/Denver. But even if the date-time is in US Mountain Time, that won't always give you the right answer.
If you refer to this list of tz database time zones, you'll see that
US Mountain Standard Time (MST) is UTC-7.
US Pacific Time (America/Los_Angeles) is UTC-8 in the winter, and UTC-7 in the summer.
The correct conversion of
2021-04-01 10:00:00 -07:00 (MST)
is therefore
2021-04-01 10:00:00 -07:00 (America/Los_Angeles)
DateTime properly converted the date-time.
Maybe you meant to use US Mountain Time, which is identified by America/Denver (UTC-7 in winter, UTC-6 in summer). However, that introduces an ambiguity. For one hour every year, you'll get the wrong answer due to the overlap during the DST change.
For example,
2020-11-01 02:30:00 (America/Denver)
could refer to both
2020-11-01 02:30:00 -05:00 (America/Denver) Before "fall back"
2020-11-01 02:30:00 -06:00 (America/Denver) After "fall back"
Date-times should be transmitted in UTC or provide the offset from UTC of the date-time. For example, using the standard RFC3339 format, you could use any of
2020-11-01T02:30:00-05:00
2020-11-01T07:30:00+00:00
2020-11-01T07:30:00Z
for the former and any of
2020-11-01T02:30:00-06:00
2020-11-01T08:30:00+00:00
2020-11-01T08:30:00Z
for the latter. Then, you could keep using DateTime::Format::Strptime (with %z), or use DateTime::Format::RFC3339.
Due to the limitation of date parsing on BlackBerry I'm trying to roll my own parse/deparse methods, however I seem to be falling foul of an hour's difference somewhere, somehow.
I do this:
long nowLong = System.currentTimeMillis();
String nowString = DateParser.longToString(nowLong);
Date nowDateFromString = DateParser.stringToDate(nowString);
Date nowDateFromLong = DateParser.longToDate(nowLong);
When outputted in order it produces this in console:
[139.46] 1369132556831
[139.46] 21 May 2013 11:35:56 Europe/Dublin
[139.46] Tue May 21 12:35:56 Europe/Dublin 2013
[139.46] Tue May 21 11:35:56 Europe/Dublin 2013
My simulator's time is set to 11:35 so the third statement - DateParser.stringToDate() - seems to be failing somewhere.
Here is my implementation:
public static Date stringToDate(String date) {
long l = HttpDateParser.parse(date);
Date d = new Date(l);
return d;
}
As my nowString includes the time zone I'd expect HttpDateParser.parse() to take this in to account but it seems not to be.
How can I correct this?
HttpDateParser.parse() is documented to handle "GMT" or a "TZD" which I assume to be a "time zone designator". I suspect this is expected to be the (horrible, ambiguous) abbreviation format - so for example, it might be worth trying to parse
21 May 2013 11:35:56 BST
and seeing what you get out. That would at least take you further in terms of diagnosing the behaviour of HttpDateParser. Keeping the time zone's TZDB ID is a better idea in my view, but you may well need to write your own parsing code. You still need to handle local time ambiguity though, where a particular local time occurs twice due to DST transitions.
It's not entirely clear what the input or expected output are in your case - how much control you have over the format. I'd try to use ISO-8601 as far as possible, with a time zone identifer as well if you need one. (If you're only trying to represent an instant in time, I'd use an ISO-8601 representation of the UTC instant, complete with a Z suffix to indicate UTC.)
When i call [NSDate date] it returns 2 hours less than it should. I've checked my computer clock settings and they are all ok. I've checked settings in iPhone and time zone and clock are all OK. Even simulator shows correct time on top toolbar. But when I try to log current date it shows 2 hours less than it should. I ran out of ideas where to look.
All dates returned by [NSDate date] are in the GMT time zone.
When you use any NSDateFormatter, just set the time zone and it will print out the correct time.
Look carefully at the output of the NSLog() statement. You will see that the output always contains the timezone using standard UTC. Therefore, the date is actually correct, taking into account the timezone.
I found my solution here: How to convert time to the time zone of the iPhone device?
So if you ever want to change [NSDate date] to point to local time just use the code provided in the link above. And dont forget to change timeZoneWithAbbreviation on sourceTimeZone from EST to GMT (because NSDate is always in GMT)
Once again thanks everyone for helping out..
I have an NSString like this #"2010-08-30T11:00:00-04:00" . How to convert this to an NSDate ? Which DateFormat should be used with it ? I tried this #"yyyy-MM-dd'T'HH:mm:ss-SSS" . But didn't worked. Please help me.
Edit
I found #"yyyy-MM-dd'T'HH:mm:ss-SSS" was working fine in OS version 3.1 . But its not getting in 4.0 .This question also pointing similar problem. Please give a solution
[yourNSDateFormatterVariable setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Knowing the timezone to be GMT - 4 hours is not enough to determine if daylight saving should be applied or not. Daylight saving is determined based on the geopolitical borders. For instance is daylight saving not applied in the same months above and below equator. Additional countries close to equator do seldom use daylight saving at all.
So, you need to get the named timezone in order to be able to apply daylight saving correctly.
So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date rolls over from December 26th to December 27th, the year changes to 2010.
I even rolled it forward to 2011... and it changes when December 25th changes to the 26th.... but wait... in 2012, it correctly rolls over on December 31 - January 1... and then its back to 29th-30th in 2013. Is there some kind of astronomical phenomenon I am not aware of going on or does Apple run on some crazy Heechee calendar I don't know of? The calendar app works correctly...
The most likely explanation is I am missing something so obvious that I will slap myself when I realize it. But hey, I haven't slept in... wow I don't remember if its been two days or three. Take pity and help me out here.
UPDATE: So maybe it wasn't something simple. Still looking for an answer here! Am I really the only person who has experienced this?? I'll bet when the end of December rolls around, more people will hit the same roadblock.
UPDATE2: Anyone? Still looking, still not finding...
UPDATE3: Still haven't found a solution. Come on! This is an interesting puzzle!
UPDATE4: Still no answer found. I have submitted the app and it is now in the appstore. Would still like to see this problem solved so I can update the app.
There may be this problem, that when you are on the last week of the month and the week has fewer than 7 days left in current month, then perhaps the API treated the week as the first week of the next month. Since december of 2012 has already 7 days in its last week there is no problem in that month.
I was getting the same problem here, and I solved it.
- (int) currentWeekOfMonth
{
return CFCalendarGetOrdinalityOfUnit (
CFCalendarCopyCurrent(),
kCFCalendarUnitWeek,
kCFCalendarUnitMonth,
[self absoluteTime]);
}
my requirement is to show week number and for this i calculate the week number of first week of month and the add this to the total number of week in month.
int currentWeekNumberInYear = [_calendarInfo currentWeekOfYear];
int currentWeekNumberInMonth = [_calendarInfo currentWeekOfMonth];
currentWeekNumberInYear = currentWeekNumberInYear-currentWeekNumberInMonth +1;
currentWeekNumberInYear = currentWeekNumberInYear<0 ? (NSInteger)[_calendarInfo weeksInMonth] ==5 ?49:48 : currentWeekNumberInYear;
I hope it will be useful to you.
Turns out its the Date format string used to set up the NSDateFormatter that was causing this for me.
"yyyy" corresponds to the 4-digit year, while YYYY corresponds to the year starting from the Sunday of the 1st week of year. Why anyone would want this is anyone's guess, and it would really help if Apple provided a link to their list of format specifiers, but there you go.
Just make sure your format string has the year component in lowercase and it should be sorted.
This post on TUAW describes a similar problem in PhotoBooth on Mac OS X:
http://www.tuaw.com/2009/12/29/beware-photo-booth-time-stamps-its-a-bug-not-a-feature/
One commenter agrees with vikas that it's an end-of-week issue.