Skip to content

Commit

Permalink
FIX: calculated module compatibility with all timezones
Browse files Browse the repository at this point in the history
Crocoblock/issues-tracker#6259
  • Loading branch information
girafffee committed Jul 29, 2024
1 parent 1f38cf1 commit dc8d27a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/build/frontend/main.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-i18n'), 'version' => '2926fb998e5f04f254e1');
<?php return array('dependencies' => array('wp-i18n'), 'version' => '04fbf45aba52b61e09a5');
2 changes: 1 addition & 1 deletion assets/build/frontend/main.js

Large diffs are not rendered by default.

39 changes: 21 additions & 18 deletions assets/src/frontend/main/calc.module/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,44 @@ function zerofill( number, count ) {
}

/**
* @param date {Date}
* @param date {Date}
* @param isUtc
*/
function toDate( date ) {
export function toDate( date, isUtc = true ) {
const month = isUtc ? date.getUTCMonth() : date.getMonth();
const day = isUtc ? date.getUTCDate() : date.getDate();
const year = isUtc ? date.getUTCFullYear() : date.getFullYear();

return [
date.getFullYear(),
zerofill( date.getMonth() + 1, 2 ),
zerofill( date.getDate(), 2 ),
year,
zerofill( month + 1, 2 ),
zerofill( day, 2 ),
].join( '-' );
}

/**
* @param date {Date}
* @param date {Date}
* @param isUtc
*/
function toTime( date ) {
export function toTime( date, isUtc = true ) {
const hours = isUtc ? date.getUTCHours() : date.getHours();
const mins = isUtc ? date.getUTCMinutes() : date.getMinutes();

return [
zerofill( date.getHours(), 2 ),
zerofill( date.getMinutes(), 2 ),
zerofill( hours, 2 ),
zerofill( mins, 2 ),
].join( ':' );
}

function toDateTime( date ) {
return toDate( date ) + 'T' + toTime( date );
export function toDateTime( date ) {
return toDate( date, false ) + 'T' + toTime( date, false );
}

/**
* @param timeOrDate {String|Number}
* @return {{time: number, type: string}}
*/
function getTimestamp( timeOrDate ) {
export function getTimestamp( timeOrDate ) {
if ( !Number.isNaN( +timeOrDate ) ) {
return { time: +timeOrDate, type: 'number' };
}
Expand All @@ -51,10 +60,6 @@ function getTimestamp( timeOrDate ) {
if ( dateParts.length > 1 ) {
const date = new Date( timeOrDate );

if ( !timeOrDate.includes( 'T' ) ) {
date.setHours( 0, 0, 0 );
}

return {
time: date.getTime(),
type: 'date',
Expand Down Expand Up @@ -86,5 +91,3 @@ function getTimestamp( timeOrDate ) {
type: 'time',
};
}

export { toDate, toTime, toDateTime, getTimestamp };

0 comments on commit dc8d27a

Please sign in to comment.