UNPKG

fluro

Version:

Promise based HTTP Fluro client for the browser and node.js

273 lines (190 loc) 12 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>api/fluro.date.js - Documentation</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="FluroAccess.html">FluroAccess</a><ul class='methods'><li data-type='method'><a href="FluroAccess.html#.retrieveCurrentSession">retrieveCurrentSession</a></li><li data-type='method'><a href="FluroAccess.html#.setDefaultApplication">setDefaultApplication</a></li></ul></li><li><a href="FluroAPI.html">FluroAPI</a></li><li><a href="FluroAsset.html">FluroAsset</a><ul class='methods'><li data-type='method'><a href="FluroAsset.html#.avatarUrl">avatarUrl</a></li><li data-type='method'><a href="FluroAsset.html#.downloadUrl">downloadUrl</a></li><li data-type='method'><a href="FluroAsset.html#.filesize">filesize</a></li><li data-type='method'><a href="FluroAsset.html#.getUrl">getUrl</a></li><li data-type='method'><a href="FluroAsset.html#.imageUrl">imageUrl</a></li><li data-type='method'><a href="FluroAsset.html#.posterUrl">posterUrl</a></li></ul></li><li><a href="FluroAuth.html">FluroAuth</a><ul class='methods'><li data-type='method'><a href="FluroAuth.html#.addEventListener">addEventListener</a></li><li data-type='method'><a href="FluroAuth.html#.changeAccount">changeAccount</a></li><li data-type='method'><a href="FluroAuth.html#.getCurrentToken">getCurrentToken</a></li><li data-type='method'><a href="FluroAuth.html#.getCurrentUser">getCurrentUser</a></li><li data-type='method'><a href="FluroAuth.html#.login">login</a></li><li data-type='method'><a href="FluroAuth.html#.logout">logout</a></li><li data-type='method'><a href="FluroAuth.html#.refreshAccessToken">refreshAccessToken</a></li><li data-type='method'><a href="FluroAuth.html#.removeAllListeners">removeAllListeners</a></li><li data-type='method'><a href="FluroAuth.html#.removeEventListener">removeEventListener</a></li><li data-type='method'><a href="FluroAuth.html#.sendResetPasswordRequest">sendResetPasswordRequest</a></li><li data-type='method'><a href="FluroAuth.html#.set">set</a></li><li data-type='method'><a href="FluroAuth.html#.signup">signup</a></li></ul></li><li><a href="FluroCache.html">FluroCache</a><ul class='methods'><li data-type='method'><a href="FluroCache.html#.get">get</a></li><li data-type='method'><a href="FluroCache.html#.reset">reset</a></li></ul></li><li><a href="FluroContent.html">FluroContent</a><ul class='methods'><li data-type='method'><a href="FluroContent.html#.external">external</a></li><li data-type='method'><a href="FluroContent.html#.get">get</a></li><li data-type='method'><a href="FluroContent.html#.query">query</a></li><li data-type='method'><a href="FluroContent.html#.retrieve">retrieve</a></li><li data-type='method'><a href="FluroContent.html#.search">search</a></li><li data-type='method'><a href="FluroContent.html#.slug">slug</a></li></ul></li><li><a href="FluroCore.html">FluroCore</a></li><li><a href="FluroDate.html">FluroDate</a><ul class='methods'><li data-type='method'><a href="FluroDate.html#.localDate">localDate</a></li><li data-type='method'><a href="FluroDate.html#.readableEventDate">readableEventDate</a></li></ul></li><li><a href="FluroTypes.html">FluroTypes</a><ul class='methods'><li data-type='method'><a href="FluroTypes.html#.retrieve">retrieve</a></li></ul></li><li><a href="FluroUtils.html">FluroUtils</a><ul class='methods'><li data-type='method'><a href="FluroUtils.html#.arrayIDs">arrayIDs</a></li><li data-type='method'><a href="FluroUtils.html#.comma">comma</a></li><li data-type='method'><a href="FluroUtils.html#.errorMessage">errorMessage</a></li><li data-type='method'><a href="FluroUtils.html#.getStringID">getStringID</a></li><li data-type='method'><a href="FluroUtils.html#.mapParameters">mapParameters</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-fluro.html">fluro</a></li></ul> </nav> <div id="main"> <h1 class="page-title">api/fluro.date.js</h1> <section> <article> <pre class="prettyprint source linenums"><code> import _ from 'lodash'; import moment from 'moment-timezone'; import FluroUtils from './fluro.utils'; /////////////////////////////////////////////////////////////////////////////// /** * @classdesc A static service that provides useful functions for working with dates and timestamps. * @class * @hideconstructor */ const FluroDate = { defaultTimezone:Intl.DateTimeFormat().resolvedOptions().timeZone, } /////////////////////////////////////////////////////////////////////////////// /** * A function that will return a date in context of a specified timezone * If no timezone is specified then the default timezone of the current clock will be used. * This will return dates that are incorrect on purpose. So that it can appear to the user as if they were in another timezone. * As Javascript dates are always in the context of the timezone they are being viewed in, this function will give you a date that is technically * not the Universal point in time of the date, but rather a time that reads in your timezone as if you were in the specified timezone. * @alias FluroDate.localDate * @param {Date} date Either a javascript date object, or a string timestamp representing a javascript date object * @param {String} specifiedTimezone The timezone to retrieve the date in eg. Australia/Melbourne * @return {Date} A javascript date object transformed to match the specified timezone */ FluroDate.localDate = function(d, specifiedTimezone) { // console.log('LOCAL DATE', d, specifiedTimezone); //Date var date;// = new Date(d); if(!d) { date = new Date(); } else { date = new Date(d); } /////////////////////////////////////////// var timezoneOffset; var browserOffset = date.getTimezoneOffset(); /////////////////////////////////////////// if(!specifiedTimezone) { specifiedTimezone = FluroDate.defaultTimezone; } if(specifiedTimezone) { timezoneOffset = moment.tz(date, specifiedTimezone).utcOffset(); browserOffset = moment(date).utcOffset(); var difference = (timezoneOffset - browserOffset); var offsetDifference = difference * 60 * 1000; var prevDate = new Date(date); date.setTime(date.getTime() + offsetDifference); } return date; } /////////////////////////////////////////////////////////////////////////////// // https://momentjs.com/docs/#/displaying/format/ FluroDate.formatDate = function(dateString, format, timezone) { var date = FluroDate.localDate(dateString, timezone); return moment(date).format(format); } FluroDate.dateFromID = function(id, format, timezone) { id = FluroUtils.getStringID(id); var date = new Date(parseInt(id.substring(0, 8), 16) * 1000); return FluroDate.formatDate(date, format, timezone); } /////////////////////////////////////// /** * A helper function that can display a human-readable date for an event * taking into consideration the context of the current time, the event's start and end time. * This is often used as a string filter * and what is relevant * @alias FluroDate.readableEventDate * @param {Object} event An object that has both a startDate and endDate property, Usually an event object from the Fluro API * @param {String} style Whether to return a 'short', 'medium' or 'long' date * @return {String} The human readable date for the event * @example * //Returns 5:30pm 1 May * FluroDate.readableEventDate({"startDate": "2019-05-01T07:30:00.000Z", "endDate":"2019-05-01T07:30:00.000Z"}) * //Returns 5:30pm - 7:30pm 1 May * FluroDate.readableEventDate({"startDate": "2019-05-01T07:30:00.000Z", "endDate":"2019-05-01T09:30:00.000Z"}) * //Returns 1 - 5 May 2015 * FluroDate.readableEventDate({"startDate": "2015-05-01T07:30:00.000Z", "endDate":"2015-05-05T09:30:00.000Z"}) * //1 May - 21 Jun 2019 * FluroDate.readableEventDate({"startDate": "2019-05-01T07:30:00.000Z", "endDate":"2019-06-21T09:30:00.000Z"}) */ FluroDate.readableEventDate = function(event, style) { //////////////////////////////////////// var startDate; var endDate; //////////////////////////////////////// if (!event) return; //////////////////////////////////////// if (event.startDate) { startDate = FluroDate.localDate(event.startDate, event.timezone); } else { return; } if (!event.endDate) { endDate = FluroDate.localDate(event.endDate, event.timezone); } else { endDate = startDate; } /////////////////////////////////////////////// startDate = moment(startDate); endDate = moment(endDate); /////////////////////////////////////////////// var noEndDate = startDate.format('h:mma D MMM YYYY') == endDate.format('h:mma D MMM YYYY'); var sameYear = (startDate.format('YYYY') == endDate.format('YYYY')); var sameMonth = (startDate.format('MMM YYYY') == endDate.format('MMM YYYY')); var sameDay = (startDate.format('D MMM YYYY') == endDate.format('D MMM YYYY')); switch (style) { case 'short': // console.log('SHORT', startDate, endDate); if (noEndDate) { return startDate.format('D MMM') } if (sameDay) { //8am - 9am Thursday 21 May 2016 return startDate.format('D MMM') } if (sameMonth) { //20 - 21 May 2016 return startDate.format('D') + '-' + endDate.format('D MMM') } if (sameYear) { //20 Aug - 21 Sep 2016 return startDate.format('D') + '-' + endDate.format('D MMM') } //20 Aug 2015 - 21 Sep 2016 return startDate.format('D MMM Y') + ' - ' + endDate.format('D MMM Y') break; default: if (noEndDate) { return startDate.format('h:mma dddd D MMM Y') } if (sameDay) { //8am - 9am Thursday 21 May 2016 return startDate.format('h:mma') + ' - ' + endDate.format('h:mma dddd D MMM Y') } if (sameMonth) { //20 - 21 May 2016 return startDate.format('D') + '-' + endDate.format('D MMM Y') } if (sameYear) { //20 Aug - 21 Sep 2016 return startDate.format('D MMM') + ' - ' + endDate.format('D MMM Y') } //20 Aug 2015 - 21 Sep 2016 return startDate.format('D MMM Y') + ' - ' + endDate.format('D MMM Y') break; } } /////////////////////////////////////////////////////////////////////////////// export default FluroDate;</code></pre> </article> </section> </div> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Apr 24 2019 19:46:45 GMT+1000 (Australian Eastern Standard Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme. </footer> <script>prettyPrint();</script> <script src="scripts/polyfill.js"></script> <script src="scripts/linenumber.js"></script> </body> </html>