Problem in getting week number of sundays , in zend - zend-framework

I want to get the week number of a particular date , using Zend_Date
My local is setted as English(IN) [en_IN], in Opera browser
I am using the following code
$date = new Zend_Date('22 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //output 12, correct
But if we give a sunday , it will not work correctly
for example
$date = new Zend_Date('21 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //output 11, not correct
it should output 12
What is wrong with this?

For the Locale of English (India), 'en_IN', the first day of the week is Monday. Zend_Date is giving you the correct value.
EDIT: I just did a quick test using the 'en_US' locale, and I'm getting the same behavior. It looks like Zend_Date may be ignoring the locale for this calculation.
$locale = new Zend_Locale('en_US');
Zend_Registry::set('Zend_Locale', $locale);
$date = new Zend_Date('2010-03-22', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //outputs 12, correct
$date = new Zend_Date('2010-03-21', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK); //outputs 11, not correct

This should work fine:
$locale = new Zend_Locale('en_IN');
Zend_Registry::set('Zend_Locale', $locale);
$date = new Zend_Date('22 Mar, 2010', null, Zend_Registry::get('Zend_Locale'));
echo $date->get(Zend_Date::WEEK);
If not, try to determine it directly, using Zend_Locale_Data. Sometimes data provided by Unicode are not exactly as we expect for our region.

Related

Groovy Date code producing unexpected output

Why is below code producing output like this ?
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-mm-dd hh:mm:ss', oldDate )
Output : Sat Jan 05 01:34:54 EST 2013
When it should simply produce December 5 as the date in the output ? In fact in the original string whatever month I put in it produces the exact same output.
The problem is your date format isn't correct. mm is for minutes where MM is for month. So it should be:
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-MM-dd hh:mm:ss', oldDate )
You can find out more from the Java API docs for SimpleDateFormat.

How to set client specific timezone and date

pardon me if it seems to be a duplicate question.
I have seen many posts already on this topic. However after trying many examples could not find the solution to my problem.
I tried this code
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH )
Date newDate = sdf.parse(sdf.format( new Date( dateTimeString ) ) )
However the second line of code always converts the date to the server specific date and timezone which i don't want. I also tried the following
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz", Locale.ENGLISH )
log.info "+++++++++++++++++hidden date ++++++++ " + params.hiddenGameDateTime.substring(35, 38)
log.info "x = " + sdf.format( new Date ( params.hiddenGameDateTime ))
String tzone = params.hiddenGameDateTime.substring(35, 38)
sdf.setTimeZone( TimeZone.getTimeZone( tzone ) )
log.info "Timezone = " + sdf.getTimeZone().getDisplayName()
Please note that
sdf.format( new Date( dateTimeString ) )
gives me the desired result, however it gives me the string value of the date and the actual value to be stored in database is of the Data type date which can't hold the string value.
the value for date and time in my case gets converted to PST date and time. how can i avoid this. The user input date with timezone should be stored in the database as it is with no change in date and timezone.
An observation: The constructor new Date( dateTimeString ) is deprecated. A better replacement would be something like that:
SimpleDateFormat sdfOriginal = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = sdfOriginal.parse(dateTimeString);
Furthermore: An expression like sdf.parse(sdf.format(...)) using the same format object does not make much sense.
But most important, your statement "the second line of code always converts the date to the server specific date and timezone" seems to be based on test output like:
System.out.println(newDate);
This implicitly uses toString() which is based on jvm default time zone, in your case the server time zone. But keep in mind, the internal state of j.u.Date does not reference any time zone. A Date is just a container for a long, namely the seconds since 1970-01-01T00:00:00Z in UTC time zone, that is a global time.
Additional remark:
If you need the client time zone (in a scenario with multiple users in different time zones) to create user-specific formatted date strings, then you indeed need to store the time zone preference of every user in the database, so you can use this information for output in an expression like:
SimpleDateFormat sdf = new SimpleDateFormat("{pattern}";
sdf.setTimeZone(TimeZone.getTimeZone("{user-preference-time-zone}");
String userOutput = sdf.format(date);
Date is always jvm timezone specific. You need to normalize it to standard time and store it in DB to cater with different timezone servers.

Modifying date in ColdFusion

How can I adjust a date in coldfusion to get the next day at 1AM?
the date is taken from a database, and stored as a string. I'm thinking the way to do it is through CreateDateTime and filling it with the time and date using year,month,day + 1 etc.
I'm just worried that it won't work when the next day falls on the next month
Using DateAdd() you can always be sure that it will take the context of the current date into account. So if it is August 31 and you add one day it will correctly make the date Sept 1st. It will also properly switch the year if you did the same on Dec 31st.
<cfset nextDate = dateAdd("d", 1, now()) />
<cfset nextDateWithTime = createDateTime(year(nextDate), month(nextDate), day(nextDate), 1, 0, 0) />
<cfoutput>#nextDateWithTime#</cfoutput>
Assuming the date is something which CF recognizes as a date, and contains date only, without time, you could do something like:
<cfscript>
function tomorrowOneAM(date) {
var resultValue = DateAdd("d",1,date);
resultValue = DateAdd("h",1,resultValue);
return resultValue;
}
</cfscript>

Zend_Date: wrong results on DST change day

"25 Mar 2012" was the date when time was changed from 02:00am to 03:00am in the Czech Republic. On that date, one functionality on my website stopped working correctly and a customer complained, etc. After digging for a few hours, I figured out that on that day Zend_Date behaved strangely:
#!/usr/bin/env php
<?php
include 'Zend/Date.php';
date_default_timezone_set('Europe/Prague');
shell_exec('sudo date --set="25 Mar 2012 12:00:00"');
$date = new Zend_Date();
$date->set('00:01:00', Zend_Date::TIMES);
$startDate = $date->get(Zend_Date::TIMESTAMP);
echo 'start date: ' . date("j.n.Y H:i", $startDate) . PHP_EOL;
$date->set('23:59:00', Zend_Date::TIMES);
$endDate = $date->get(Zend_Date::TIMESTAMP);
echo 'end date: ' . date("j.n.Y H:i", $endDate) . PHP_EOL;
This outputs:
start date: 24.3.2012 23:01
end date: 24.3.2012 23:59
which is off by day.
If I change the date to "26 Mar 2012 12:00:00", it correctly outputs:
start date: 26.3.2012 00:01
end date: 26.3.2012 23:59
Using mktime instead of Zend_Date works correctly in both cases. Is it bug in Zend_Date? I think it is, so I have already posted a bug report http://framework.zend.com/issues/browse/ZF-12121. But maybe I am missing something obvious?
I've just find this on stack overflow, it perfectly fixed my issue (same as yours)
See Bug in Zend_Date (back in time)
Good luck
When testing the code with just hardcoding the date: $date = new Zend_Date('2012-03-25 4:00:00', 'YYYY-MM-dd H:mm:ss'); the result is ok. Try to see whether the date output is the same when using $date->toString('d.M.yyyy HH:mm');

Zend_Date::toString() outputs the wrong year. Bug in my code, or Zend_Date?

I'm using Zend_Date to set and get the year, but it is not being set as the correct year. I set the year as 2010, and it returns the year as 2009. What am I doing wrong? Is there a bug in Zend_Date?
$date = new Zend_Date('2010-01-03', 'YYYY-MM-dd');
echo $date->toString('MMMM d, YYYY');
//outputs January 3, 2009
The year must be being set correctly because getting the year part of the date works:
echo $date->get(Zend_Date::YEAR); //2010
Solution:
Well I got it to work...You have you use lowercase: yyyy
echo $date->toString('MMMM d, yyyy');
YYYY stands for the ISO Year. 2010-01-03 is week 53, day 7 of the ISO year 2009
yyyy stands for the actual calendar year.
I've ran into this problem as well.
In the Zend_Date class 'YYYY' means to a 4 digit representation of the 'ISO year' where as 'yyyy' means a 4 digit representation of the 'year'.