ActiveRecord with timezone not working as expected - mysql

I have a model called Order which has a DATETIME (Mysql) column called expiration
I do the following:
order = Order.last
wanted_time = Time.use_zone('Pacific Time (US & Canada)') { Time.now.end_of_day.to_datetime }
And wanted_time gives me: Thu, 20 Nov 2014 23:59:59 -0800
However, when I do:
order.expiration = wanted_time
When I try to fetch order.expiration, I get:
Fri, 21 Nov 2014 07:59:59 UTC +00:00
Why is that? If I specifically have defined the zone I wanted to store it with?

See this: How to change default timezone for Active Record in Rails?
Change application.rb to have the following lines... I'm guessing theres a difference between rails and db.
config.time_zone = 'Eastern Time (US & Canada)'
config.active_record.default_timezone = 'Eastern Time (US & Canada)'

Related

Unknow HTML date format "1615856400000" = "Thuesday, Mar 16, 2021", "1615770000" = "Thuesday, Mar 15, 2021" etc

I'm web scraping a HTML page which shows a time series generated by a function inside a <script> tag. On the webbpage I can see different dates for the time series, but when I "view page source" these dates seem to be converted into numbers.
For example "1615856400000" = "Thuesday, Mar 16, 2021", "1615770000" = "Monday, Mar 15, 2021" and "1612227600000" = "Thuesday, Feb 2, 2021".
Does anyone know what the logic is for these conversions? Ideally I would like a list which shows the mappings per day, starting at some arbitrary date e.g. "Wednesday, Feb 26, 2014".

JSON string to date with Javascript in Google Apps Script editor

I am working through w3schools, particularly https://www.w3schools.com/js/js_json_parse.asp
I ran this example and got an unexpected result
let dashText = '{ "name":"John", "birth":"1986-12-14", "city":"New York"}';
let objD = JSON.parse(dashText);
console.log("objD: ", objD);
objD.birth = new Date(objD.birth);
console.log("objD.birth: ", objD.birth);
3:09:04 PM Info objD: { name: 'John', birth: '1986-12-14', city: 'New York' }
3:09:04 PM Info objD.birth: Sat Dec 13 1986 18:00:00 GMT-0600 (Central Standard Time)
Note the difference in the dates. I then changed the dashes to slashes out of curiosity and the date was correctly determined from the string.
let slashText = '{ "name":"John", "birth":"1986/12/14", "city":"New York"}';
let objS = JSON.parse(slashText);
console.log("objS: ", objS);
objS.birth = new Date(objS.birth);
console.log("objS.birth: ", objS.birth);
3:09:04 PM Info objS: { name: 'John', birth: '1986/12/14', city: 'New York' }
3:09:04 PM Info objS.birth: Sun Dec 14 1986 00:00:00 GMT-0600 (Central Standard Time)
Can anyone explain the results?
Javascript parses DateTime strings differently based on how the string is formatted. The dashes are parsed to ISO date, i.e. international time. You can see this when it tries to handle the timezone conversion, where it sets the time to 18:00:00 to account for the 6 hour shift from Universal Time. Slashes are parsed as just the date, and doesn't try to adjust the time based on timezones.
Here's a w3schools link that goes over this in more detail.

How to render correct datetime in hugo post?

I created a post using hugo new posts/mypost.md and it created mypost.md for me with header toml configuration like that
But, when i run on server (local), the datetime rendered wrong like:
How can I fix that?
Thanks in advance!
This is how I got it to work:
Add your date format in config.toml
[params]
dateFormat = "02 Jan 2006"
Your post should contain the date in its front-matter:
---
date: "2020-12-23T15:21:54+05:30"
...
---
Use the format in your layout:
<div>{{ .Params.date.Format .Site.Params.dateFormat }}</div>
Note: Please DO NOT change the numbers in the date format. It must be 02 for day, Jan for month, 2006 for year, etc. check this for more details.
Check also Hugo 0.87 (Aug. 2021, two years later), which comes with:
a timezone
a timedate format
{{ time.Format "Monday, Jan 2, 2006" "2015-01-21" }}
→
"Wednesday, Jan 21, 2015"
Note that since Hugo 0.87.0, time.Format will return a localized string for the current language.
Date/time formatting layouts
{{ .Date | time.Format ":date_long" }}
The full list of custom layouts with examples for English:
:date_full => Wednesday, June 6, 2018
:date_long => June 6, 2018
:date_medium => Jun 6, 2018
:date_short => 6/6/18
:time_full => 2:09:37 am UTC
:time_long => 2:09:37 am UTC
:time_medium => 2:09:37 am
:time_short => 2:09 am
In Hugo v0.95.0 (released 16 Mar 2022), it should be
[params]
date_format = "02 Jan 2006"
with
---
date: 2022-03-18T21:10:00+07:00
---
in the post front matter, where double quote marks are not necessary. I used Ananke theme.
Setting the date_format in the config file seems weird to me. I would set the following in the config file:
defaultContentLanguage: nl
languageCode: nl_NL
These are variables that do NOT go into params, but on the root level of the config file. When you have these you can simply call:
{{ .Date | time.Format ":date_long" }}
or... if you need another format:
{{ .Date | time.Format ":date_medium" }}
The date will be formatted in your language.
You need to set dateformat in file config.toml at 2.1.2006 (any proper format, ensure 2, Jan, 2006).
This link save my day

Change date format in my HTML / CSS output

Have a dates saved in my sqlite database in this format 2019-01-24 13:41:40.515955 and when I output to my web page its displayed as 2019-01-24 13:41:40 UTC. Please can I get guidance on displaying something like Wed 24 January 2019 ? No sure how to approach it
Use Javascript, would be a simple approach to parse and format dates with desired output.
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
var date = new Date('6/29/2011 4:52:48 PM UTC');
date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)"
console.log(`Year: ${date.getFullYear()}, Month: ${date.getMonth()}, Day, ${date.getDay()}`)
You can also parse dates using Date.parse(``);

Json set Date Format - Print timestamp and epoch time both

I am using objectmapper.enable(SerializationFeature.INDENT_OUTPUT).writerWithDefaultPrettyPrinter().writeValueAsString(r);
Result: "begin" : 1513644267698,
which returns the values in the object 'r' and the time in epoch format as stored. I now wish to print the time in a readable format, so i did the following change:
DateFormat df = new SimpleDateFormat(dateFormat);
return myMapper.setDateFormat(df).enable(SerializationFeature.INDENT_OUTPUT).writerWithDefaultPrettyPrinter().writeValueAsString(r);
Here i can pass the required dateFormat and print it in my desired format for eg. "EEE MMM dd, yyyy HH:mm:ss.SSS (z)".
Result: "begin" : "Tue Feb 20, 2018 09:02:24.941 (UTC)",
My question is - is there any way where i can print both epoch time and the above time format together in the same.
Required Result: "updatedTime" : [1513644267698] Tue Feb 20, 2018 09:02:24.941 (UTC),