mirror of
https://github.com/bendtherules/is-nifty-open.git
synced 2026-08-18 13:52:20 +00:00
Use moment-timezone + tz helpers + some OpenToday DOM changes
This commit is contained in:
@@ -4,9 +4,11 @@
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@types/classnames": "^2.2.3",
|
||||
"@types/moment-timezone": "^0.5.4",
|
||||
"classnames": "^2.2.5",
|
||||
"linq": "^3.0.9",
|
||||
"moment": "^2.21.0",
|
||||
"moment-timezone": "^0.5.14",
|
||||
"react": "^16.2.0",
|
||||
"react-dom": "^16.2.0",
|
||||
"react-scripts-ts": "2.13.0"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import * as React from 'react';
|
||||
import * as Moment from 'moment';
|
||||
import * as moment from 'moment-timezone';
|
||||
import * as Linq from 'linq';
|
||||
import * as classnames from 'classnames';
|
||||
import "./Component.css";
|
||||
@@ -22,14 +22,28 @@ export class OpenToday extends React.Component<{
|
||||
this.processAnswer();
|
||||
}
|
||||
|
||||
checkSameDayInSameTZ(moment1: moment.Moment, moment2: moment.Moment, raiseErrorTZMismatch: boolean = true) {
|
||||
// tslint:disable-next-line:triple-equals
|
||||
if (raiseErrorTZMismatch && (moment1.tz() != moment2.tz())) {
|
||||
throw new Error("Timezones don't match");
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:triple-equals
|
||||
return (moment1.startOf("day").format() == moment2.startOf("day").format());
|
||||
}
|
||||
|
||||
createTodayDateInIndiaTZ(): moment.Moment {
|
||||
return moment.tz(moment.tz.guess()).tz("Asia/Kolkata");
|
||||
}
|
||||
|
||||
processAnswer(): void {
|
||||
var todayDateTime = Moment();
|
||||
var todayDateInIndiaTZ = this.createTodayDateInIndiaTZ();
|
||||
|
||||
{
|
||||
const matchingHoliday = Linq
|
||||
.from<Holiday>(this.allHolidays)
|
||||
.firstOrDefault(
|
||||
(eachHoliday, _tmp) => todayDateTime.isSame(eachHoliday.Date, 'day')
|
||||
(eachHoliday, _tmp) => this.checkSameDayInSameTZ(todayDateInIndiaTZ, eachHoliday.Date)
|
||||
);
|
||||
|
||||
if (matchingHoliday === null) {
|
||||
@@ -40,7 +54,7 @@ export class OpenToday extends React.Component<{
|
||||
}
|
||||
|
||||
{
|
||||
const weekendHoliday = getWeekendHoliday(todayDateTime);
|
||||
const weekendHoliday = getWeekendHoliday(todayDateInIndiaTZ);
|
||||
|
||||
if (typeof weekendHoliday !== "undefined") {
|
||||
this.answer = { open: false, holiday: weekendHoliday };
|
||||
@@ -60,14 +74,16 @@ export class OpenToday extends React.Component<{
|
||||
</span>
|
||||
</div>
|
||||
<div className="answerComposite">
|
||||
<div className="answerComposite-inner">
|
||||
<div className="answerBoolean">
|
||||
<span>
|
||||
{answerBoolean ? "Yes" : "No"}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="answerReason">
|
||||
{!this.answer.open ? <ClosedReason holiday={this.answer.holiday as Holiday} /> : undefined}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
+24
-20
@@ -1,8 +1,8 @@
|
||||
import * as Moment from 'moment';
|
||||
import * as moment from 'moment-timezone';
|
||||
|
||||
export interface Holiday {
|
||||
Description: string;
|
||||
Date: Moment.Moment;
|
||||
Date: moment.Moment;
|
||||
// Use it later, if needed
|
||||
Day: string;
|
||||
}
|
||||
@@ -11,24 +11,28 @@ export type HolidayList = Array<Holiday>;
|
||||
|
||||
export const allHolidays: HolidayList =
|
||||
[
|
||||
{ "Description": "Republic Day", "Date": Moment("2018-1-26"), "Day": "Friday" },
|
||||
{ "Description": "Mahashivratri", "Date": Moment("2018-2-13"), "Day": "Tuesday" },
|
||||
{ "Description": "Holi", "Date": Moment("2018-3-2"), "Day": "Friday" },
|
||||
{ "Description": "Mahavir Jayanti", "Date": Moment("2018-3-29"), "Day": "Thursday" },
|
||||
{ "Description": "Good Friday", "Date": Moment("2018-3-30"), "Day": "Friday" },
|
||||
{ "Description": "Maharashtra Day", "Date": Moment("2018-5-1"), "Day": "Tuesday" },
|
||||
{ "Description": "Independence Day", "Date": Moment("2018-8-15"), "Day": "Wednesday" },
|
||||
{ "Description": "Bakri ID", "Date": Moment("2018-8-22"), "Day": "Wednesday" },
|
||||
{ "Description": "Ganesh Chaturthi", "Date": Moment("2018-9-13"), "Day": "Thursday" },
|
||||
{ "Description": "Moharram", "Date": Moment("2018-9-20"), "Day": "Thursday" },
|
||||
{ "Description": "Mahatama Gandhi Jayanti", "Date": Moment("2018-10-2"), "Day": "Tuesday" },
|
||||
{ "Description": "Dasera", "Date": Moment("2018-10-18"), "Day": "Thursday" },
|
||||
{ "Description": "Diwali-Laxmi Pujan", "Date": Moment("2018-11-7"), "Day": "Wednesday" },
|
||||
{ "Description": "Diwali-Balipratipada", "Date": Moment("2018-11-8"), "Day": "Thursday" },
|
||||
{ "Description": "Gurunanak Jayanti", "Date": Moment("2018-11-23"), "Day": "Friday" },
|
||||
{ "Description": "Christmas", "Date": Moment("2018-12-25"), "Day": "Tuesday" },
|
||||
{ "Description": "Republic Day", "Date": parseDateInIndiaTZ("2018-1-26"), "Day": "Friday" },
|
||||
{ "Description": "Mahashivratri", "Date": parseDateInIndiaTZ("2018-2-13"), "Day": "Tuesday" },
|
||||
{ "Description": "Holi", "Date": parseDateInIndiaTZ("2018-3-2"), "Day": "Friday" },
|
||||
{ "Description": "Mahavir Jayanti", "Date": parseDateInIndiaTZ("2018-3-29"), "Day": "Thursday" },
|
||||
{ "Description": "Good Friday", "Date": parseDateInIndiaTZ("2018-3-30"), "Day": "Friday" },
|
||||
{ "Description": "Maharashtra Day", "Date": parseDateInIndiaTZ("2018-5-1"), "Day": "Tuesday" },
|
||||
{ "Description": "Independence Day", "Date": parseDateInIndiaTZ("2018-8-15"), "Day": "Wednesday" },
|
||||
{ "Description": "Bakri ID", "Date": parseDateInIndiaTZ("2018-8-22"), "Day": "Wednesday" },
|
||||
{ "Description": "Ganesh Chaturthi", "Date": parseDateInIndiaTZ("2018-9-13"), "Day": "Thursday" },
|
||||
{ "Description": "Moharram", "Date": parseDateInIndiaTZ("2018-9-20"), "Day": "Thursday" },
|
||||
{ "Description": "Mahatama Gandhi Jayanti", "Date": parseDateInIndiaTZ("2018-10-2"), "Day": "Tuesday" },
|
||||
{ "Description": "Dasera", "Date": parseDateInIndiaTZ("2018-10-18"), "Day": "Thursday" },
|
||||
{ "Description": "Diwali-Laxmi Pujan", "Date": parseDateInIndiaTZ("2018-11-7"), "Day": "Wednesday" },
|
||||
{ "Description": "Diwali-Balipratipada", "Date": parseDateInIndiaTZ("2018-11-8"), "Day": "Thursday" },
|
||||
{ "Description": "Gurunanak Jayanti", "Date": parseDateInIndiaTZ("2018-11-23"), "Day": "Friday" },
|
||||
{ "Description": "Christmas", "Date": parseDateInIndiaTZ("2018-12-25"), "Day": "Tuesday" },
|
||||
];
|
||||
|
||||
function parseDateInIndiaTZ(dateString: string): moment.Moment {
|
||||
return moment.tz(dateString, "YYYY-MM-DD", "Asia/Kolkata");
|
||||
}
|
||||
|
||||
export enum ISOWeekDay {
|
||||
Monday = 1,
|
||||
Tuesday = 2,
|
||||
@@ -39,7 +43,7 @@ export enum ISOWeekDay {
|
||||
Sunday = 7
|
||||
}
|
||||
|
||||
export function isWeekend(date: Moment.Moment): boolean {
|
||||
export function isWeekend(date: moment.Moment): boolean {
|
||||
const dateWeekDay = date.isoWeekday();
|
||||
if ((dateWeekDay === ISOWeekDay.Saturday) ||
|
||||
(dateWeekDay === ISOWeekDay.Sunday)) {
|
||||
@@ -49,7 +53,7 @@ export function isWeekend(date: Moment.Moment): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
export function getWeekendHoliday(date: Moment.Moment): Holiday | undefined {
|
||||
export function getWeekendHoliday(date: moment.Moment): Holiday | undefined {
|
||||
if (isWeekend(date)) {
|
||||
const dateWeekDay = date.isoWeekday();
|
||||
return { "Description": "Weekend", "Date": date, "Day": ISOWeekDay[dateWeekDay] };
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
version "22.2.0"
|
||||
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.2.0.tgz#55ce83139f7ad1b48b414c3927746614c6963c0f"
|
||||
|
||||
"@types/moment-timezone@^0.5.4":
|
||||
version "0.5.4"
|
||||
resolved "https://registry.yarnpkg.com/@types/moment-timezone/-/moment-timezone-0.5.4.tgz#b600e033f871f3074c283e2bd26fe4f4ec3949d4"
|
||||
dependencies:
|
||||
moment ">=2.14.0"
|
||||
|
||||
"@types/node@*", "@types/node@^9.4.6":
|
||||
version "9.4.6"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.6.tgz#d8176d864ee48753d053783e4e463aec86b8d82e"
|
||||
@@ -3828,7 +3834,13 @@ mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
|
||||
dependencies:
|
||||
minimist "0.0.8"
|
||||
|
||||
moment@^2.21.0:
|
||||
moment-timezone@^0.5.14:
|
||||
version "0.5.14"
|
||||
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.14.tgz#4eb38ff9538b80108ba467a458f3ed4268ccfcb1"
|
||||
dependencies:
|
||||
moment ">= 2.9.0"
|
||||
|
||||
"moment@>= 2.9.0", moment@>=2.14.0, moment@^2.21.0:
|
||||
version "2.21.0"
|
||||
resolved "https://registry.yarnpkg.com/moment/-/moment-2.21.0.tgz#2a114b51d2a6ec9e6d83cf803f838a878d8a023a"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user