How to change the project default Time format in Rails - datetime

Before I updated Rails, the default Time format the API was returning was yyyy-MM-dd'T'HH:mm:ss'Z'. Now it returns yyyy-MM-dd'T'HH:mm:ss:sss'Z'. I want to change it back due to apps that rely on the old time format. How can I change this PROJECT default format that Time::DateTime's to_s method returns?
Edit:
I should probably mention I started using the active_model_serializer gem. Not sure if that makes a difference. My guess is this change happened after the update from Rails 4.0.x to 4.1.x. Also I want to change this format for the project. This format changed between Rails versions. active_model_serializer uses to_s, so it makes more sense to override how to project sets this. I don't know where that is.

You can just use strftime:
Time.now.strftime("%Y-%M-%d %H:%m:%S%Z")
one of your gems is probably overriding the DateTime to_s method. you can just override it again with you own method back

Maybe you need this:
# config/initializers/time_with_zone.rb
module ActiveSupport
class TimeWithZone
def as_json(options = nil)
iso8601
end
end
end

You can augment the Ruby built-in Date::DATE_FORMATS and Time::DATE_FORMATS
DATE_FORMAT = "%m/%d/%Y".freeze
TIME_FORMAT = "#{DATE_FORMAT} %l:%M%P".freeze
format_defaults = {
default: DATE_FORMAT,
date_time12: TIME_FORMAT,
date_time24: "#{DATE_FORMAT} %H:%M",
}.freeze
Date::DATE_FORMATS.merge!(format_defaults)
Time::DATE_FORMATS.merge!(format_defaults)

Related

Cypress.moment using locale

I am trying to use Cypress.moment to compare dates. The targeted website is in french so the date format.
Therefore, I intended to use moment.js method to switch fr locale.
Cypress.moment.locale('fr')
I should be able to do that as they say on cypress documentation :
Cypress automatically includes moment.js and exposes it as Cypress.moment
https://docs.cypress.io/api/utilities/moment.html#Syntax
Then,
const todaysDate = Cypress.moment().format('Do')
const currentMonth = Cypress.moment().format('MMMM')
cy.get('.date__title').should('contain', todaysDate)
cy.get('.c-title').should('contain', currentMonth)
But the assertion fails as cypress refuse to take into account the fr locale. It keeps comparing 'décembre' with 'december' for instance. Which fails obviously.
I am doing something wrong ?
in the same aforementioned thread, a working answer recently surfaced:
put this in support/index.js:
Cypress.moment.locale('de');
it worked brilliantly in my project
Unfortunately, at the time of writing, I think this is not possible with Cypress.moment.locale(), as you can see in this issue in their git repository.
As commented there moment.locale requires an import in addition to the standard moment import, that import is moment-with-locales.min.js and has not been included in Cypress.

CakePHP: FrozenTime from database without timezone

I'm on "Europe/Rome":
debug(new \Cake\I18n\Time());
Output:
object(Cake\I18n\Time) {
'time' => '2016-03-23T14:18:17+01:00',
'timezone' => 'Europe/Rome',
'fixedNowTime' => false
}
If I save a post (for example), the datatime is handled properly.
The problem occurs when I retrieve data. This post has been just saved:
debug($post->created);
Output:
object(Cake\I18n\FrozenTime) {
'time' => '2016-03-23T14:14:45+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
The datetime is correct, but the timezone of course not.
Now if I do something like this:
debug($post->created > new \Cake\I18n\Time());
the result will obviously be TRUE and not FALSE (as it should be). I conclude that this happens because, in comparison phase, it obviously uses the same timezone.
Where am I doing wrong? Thanks.
EDIT
My bootstrap.php
date_default_timezone_set('UTC');
Instead, my php.ini (I think it's not relevant):
date.timezone = Europe/Rome
So I have to think that the value of $post->created is correct.
But now... why new \Cake\I18n\Time() uses the correct timezone (which is not set) and why the post is saved with the correct timezone?
I would kinda doubt that new \Cake\I18n\Time() will use the timezone set in your INI after date_default_timezone_set() has been used, as the latter should win over the former. Maybe there's a version specific behavior/bug where this doesn't work as expected, not sure, you'll have to debug your environment to figure what's going on there.
That being said, when data is being read, the ORM casts it to PHP datatypes. The date/time type classes responsible for this are using cloned date/time class instances instead of creating new ones, and that base instance is created when the type instance is first built and/or when the mutability is being set, which usually happens at bootstrapping time when Type::build() is invoked.
If you'd then for example change the timezone at a later point, new \Cake\I18n\Time based instances would use the newly set timezone, but the date/time type would still use the instance that was created with the original timezone at bootstrapping time.
tl;dr
Set the proper timezone via date_default_timezone_set() in your bootstrap, and if you need to change the timezone at a later point, then you'll have to make sure that a new base type instance for cloning is being set, which could for example be done by setting the mutability again, like
Type::build('time')->useImmutable();
This feels kinda workaroundish though, you may want to open a ticket over at GitHub and report this behavior.

ADF - Overriding default settings for locale

We are writing a ADF application, and we are implementing internationalization. It looks like JavaVM doesn't recognize the Faroese locale (fa), and therefor uses the wrong date format.
Is there a way to override the locale specific date format in Oracle ADF?
Found it!
Added: <formatting-locale>da_DK</formatting-locale> to trinidad-config.xml

Flex 4.5 DateTimeFormatter: why is timeZone specific information ignored in dateTimePattern?

var df:DateTimeFormatter = new DateTimeFormatter();
df.dateTimePattern = "yyyy/MM/dd HH:mm:ss Z";
var formattedDate:String = df.format(new Date(2011, 6, 7));
formattedDate value is "2011/07/07 00:00:00 ". what am i doing wrong? cannot format nothing timeZone specific? i'm getting crazy about this... help is appreciated a lot
See http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/globalization/DateTimeFormatter.html#setDateTimePattern%28%29 .
The documentation says that some operating systems don't support the timezone pattern... Apparently because the class implementing the formatting uses OS specific functions for timezone.
Workaround would be to implement your own formatter.
You shouldn't have to write your own formatter...
In Flex 4.5 they added some new localization features. You can't unfortunately always depend on the system to correctly identify the locale automatically.
Here adobe describes four different methods you can employ of identifying the locale of the user and setting it. Once this is set, the formatter will work correctly so you won't need to roll your own formatter.
http://help.adobe.com/en_US/flex/using/WS19f279b149e7481c-1c03f02c12bd00c4763-8000.html#WS19f279b149e7481c-99797f112bd080033f-8000

Custom date format (callback with php logic)

I want to create a dynamic php date format. Example: show the time of the node if it was published today and only the day/month when older then today. I want this format to be available throughout Drupal like the other predefined date formats in Drupal (not just on theme level).
I found the (D7 only) hook for hook_date_format_types but even that one doesn't seem to allow for a callback where I could define this PHP logic.
Does anyone know which hook would make this possible? Or a module which does this?
In Drupal6, format_date() has the dates and times hardcoded. Also, format_date() does not allow callbacks, but it does allow a custom string. That is where you can apply a trick: instead of hardcoding the string in there, you call a function that returns a string.
function mydate_format($timestamp) {
$now = time();
if (($now - $timestamp) < (60*60*24)) {
return "H:i";
}
else {
return "d:m:Y";
}
}
print format_date($timestamp, 'custom', mydate_format($timestamp));
The second option is to re-define a date-timestamp, but that is both hackish and limited. Date-formats are defined with variable_get(), but don't pass the timestamp along; so your example of switching formats based on the value of timestamp is not possible this way.
In settings.php:
$conf['date_format_long'] = $conf['debug'] ? 'r' : 'l, F j, Y - H:i';
This will switch from one value to another, based on whether your settings.php has a flag "debug" set to TRUE or not. As mentioned: the use for this is limited, since you cannot get any context.
The third alternative is to use Date API which offers onlinle configurable time-formats. But that is both clumsy and insecure (inputting executable PHP in your database). It also depends on a very large module-set. And has the same downside as the first solution: you cannot use format_date, but must use a modified function call, instead of format_date(). See all the options at The Drupal.org date themeing handbook.
GOTCHA In all cases Drupal will not call this function for cached content. If you want to have the dates really dynamic you either need to avoid caching alltogether, or implement the date-formatting in clientside javascript.
TL;DR: You cannot have dynamic date-formats without changing some of the code on theme-level. Using a callback-function to generate the "custom" dateformat is the simplest, working solution.
You can use Date API module to add your custom date formatting. Date API module is inside the Date module. After enabling the Date API module you can go the path "admin/settings/date-time/formats/add" to add your custom format.
"admin/settings/date-time/formats/configure" is the path to configure date formats.
Have a look at these. Happy coding.
Thanks
RT
You can go to node.tpl.php(possibly in sites/all/themes/theme_name/node.tpl.php). Here yo can find $created variable, to reformat date you can use your custom function and change the $created as you want.After this all nodes will use your formatted dates.
Regatds,
Chintan.
Use the features module. Create a feature.
In the resulting feature module, on the [feature].module file, create a hook_nodeapi function and format the field with a conditional statement that takes into account the current date() for how the date will be displayed and feed it into the $node->content var.
And that is the way rockstars would do it (-;

Resources