Skip to content

Commit

Permalink
Merge pull request #2 from burhanahmeed/major-v2.0.0
Browse files Browse the repository at this point in the history
Major v2.0.0
  • Loading branch information
burhanahmeed authored Jun 14, 2020
2 parents 2aee0ca + 88acb0d commit eeeb9f5
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 72 deletions.
53 changes: 33 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div align='center'>

![time.ts](https://raw.githubusercontent.com/burhanahmeed/time.ts/master/header.jpg)
![time.ts](https://raw.githubusercontent.com/burhanahmeed/time.ts/master/header.png)

![release](https://badgen.net/github/release/burhanahmeed/time.ts)
![tags](https://badgen.net/github/tags/burhanahmeed/time.ts)
Expand All @@ -12,26 +12,30 @@
</div>


## Time.ts version: v1.0.0
**Important Note**: there will be a major update on v2.0.0

#### Version: Time.ts v2.0.0
Please refers to [Time.ts v1.0.0](https://github.com/burhanahmeed/time.ts/tree/v1.0.0) if still using the old version.

## Deno
- [Deno.land third party module](https://deno.land/x/time.ts)

## Imports

**Version v1.0.0**
**From master branch**
```typescript
import { Time, timezone } from "https://deno.land/x/time.ts/mod.ts";
```

**More safe import**
```typescript
import { Time, timezone } from "https://denopkg.com/burhanahmeed/time.ts@v1.0.0/mod.ts";
import { Time, timezone } from "https://denopkg.com/burhanahmeed/time.ts@v2.0.0/mod.ts";
```
or
```typescript
import { Time, timezone } from "https://deno.land/x/time.ts@v1.0.0/mod.ts";
import { Time, timezone } from "https://deno.land/x/time.ts@v2.0.0/mod.ts";
```
or
```typescript
import { Time, timezone } from "https://denoland.id/x/time.ts@v1.0.0/mod.ts";
import { Time, timezone } from "https://denoland.id/x/time.ts@v2.0.0/mod.ts";
```

## API
Expand All @@ -45,23 +49,32 @@ Check this out on [Example directory](https://github.com/burhanahmeed/time.ts/tr
```typescript
import { Time } from "https://denopkg.com/burhanahmeed/[email protected]/mod.ts";

console.log('Jakarta Timezone: ', new Time().timezone('Asia/Jakarta'))
//Jakarta Timezone: 2020-06-06T20:21:14.765Z
console.log('Time now UTC: ',time().t)
//Time now UTC: 2020-06-14T06:19:37.483Z

console.log('Time now Singapore: ',time().tz('asia/singapore').t)
//Time now Singapore: 2020-06-14T14:19:37.483Z

console.log('Time now New York: ',time().tz('America/New_york').t)
//Time now New York: 2020-06-14T01:19:37.484Z

console.log('Time now Jakarta: ',time().tz('asia/Jakarta').t)
//Time now Jakarta: 2020-06-14T13:19:37.484Z

console.log('Singapore Timezone: ', new Time().timezone('Asia/Singapore'))
//Singapore Timezone: 2020-06-06T21:21:14.766Z
console.log('Time now UTC string: ',time().toString())
//Time now UTC string: Sun, 14 Jun 2020 06:21:21 GMT

console.log('Jakarta Timezone: ', new Time('2020-06-06 14:33:12').timezone('Asia/Jakarta'))
//Jakarta Timezone: 2020-06-06T14:33:12.000Z
console.log('Time now UTC: ',time('2020-06-09 09:19').t)
//Time now UTC: 2020-06-09T02:19:00.000Z

console.log('Singapore Timezone: ', new Time('2020-06-06 14:33:12').timezone('Asia/Singapore'))
//Singapore Timezone: 2020-06-06T15:33:12.000Z
console.log('Time now Singapore: ',time('2020-06-09 09:19').tz('asia/singapore').t)
//Time now Singapore: 2020-06-09T10:19:00.000Z

console.log('UTC timezone: ', new Time().utc)
//UTC timezone: 2020-06-06T13:21:14.766Z
console.log('Time now UTC another way: ',time().tz('utc').t)
//Time now UTC another way: 2020-06-14T06:34:19.344Z

console.log('Now timezone: ', new Time().now)
//Now timezone: 2020-06-06T20:21:14.766Z
console.log('Time now of your server timezone: ',time().now())
//Time now of your server timezone: 2020-06-14T13:35:17.927Z

```
If you want to get the list of available timezone you can do like this.
Expand Down
28 changes: 28 additions & 0 deletions example/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { time } from "../mod.ts";

console.log('Time now UTC: ',time().t)
//Time now UTC: 2020-06-14T06:19:37.483Z

console.log('Time now Singapore: ',time().tz('asia/singapore').t)
//Time now Singapore: 2020-06-14T14:19:37.483Z

console.log('Time now New York: ',time().tz('America/New_york').t)
//Time now New York: 2020-06-14T01:19:37.484Z

console.log('Time now Jakarta: ',time().tz('asia/Jakarta').t)
//Time now Jakarta: 2020-06-14T13:19:37.484Z

console.log('Time now UTC string: ',time().toString())
//Time now UTC string: Sun, 14 Jun 2020 06:21:21 GMT

console.log('Time now UTC: ',time('2020-06-09 09:19').t)
//Time now UTC: 2020-06-09T02:19:00.000Z

console.log('Time now Singapore: ',time('2020-06-09 09:19').tz('asia/singapore').t)
//Time now Singapore: 2020-06-09T10:19:00.000Z

console.log('Time now UTC another way: ',time().tz('utc').t)
//Time now UTC another way: 2020-06-14T06:34:19.344Z

console.log('Time now of your server timezone: ',time().now())
//Time now of your server timezone: 2020-06-14T13:35:17.927Z
Binary file removed header.jpg
Binary file not shown.
Binary file added header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 73 additions & 50 deletions lib/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,90 @@
* Copyright (c) 2020 Burhanuddin Ahmed
*/


import { timezone as List } from "./api.ts";
import { privateReturn } from "./type.ts";
import { Timezone } from "./timezone.ts";

const MINUTES_TO_MILISECOND = 60000;

export class Time {
public now: string;
public utc:string;
private time:string | undefined;
class Time {
private _t: string | undefined;
public t: Date;

constructor (time?: string) {
this.time = time;
this.now = this.timezone();
this.utc = new Date().toISOString();
constructor (time: number | string | undefined = undefined) {
this.t = new Date();
this._t = convertToString(time);
this.parse(time);
}

public timezone (timezone:string | null = null): string {
let getDifferenceToUtcInMilisec = new Date().getTimezoneOffset() * MINUTES_TO_MILISECOND;
let getUTCMilisecond = new Date().getTime();
if (this.time) {
getUTCMilisecond = new Date(this.time).getTime();
}

if (!timezone) {
// return current system datetime
return new Date(getUTCMilisecond - getDifferenceToUtcInMilisec).toISOString();
}
toString () {
return this.t.toUTCString();
}

if (timezone == 'UTC') {
return this.utc;
private parse (time: number | string | undefined) {
if (time == undefined) {
this.t = parseDate()
} else {
this.t = parseDate(time)
}
}

let addedtime = this.parseText(timezone);
if (!addedtime.status) {
throw addedtime.text;
}

// return manipulated timezone based on calculation additional / differentiation time
return new Date(getUTCMilisecond + Number(addedtime.text)).toISOString();
public now () {
this.t = parseDateNow();
return this.t;
}

private parseText (textTimezone: string): privateReturn {
let obj = List.find(el => el.id.toLowerCase() == textTimezone.toLocaleLowerCase());
if (!obj) {
return {
status: false,
text: 'timezone you entered is not compatible'
};
}
let splittedObject = obj.text.split(' ');
let removeText = splittedObject[0]
.replace('(', '')
.replace(')', '')
.replace('GMT', '');
let getTimezoneHours = removeText.split(':');
let getMinutes = ((Number(getTimezoneHours[0]) * 60) + Number(getTimezoneHours[1])) * MINUTES_TO_MILISECOND;
return {
status: true,
text: `${getMinutes}`
public tz (timezone: string = 'utc') {
const { _t } = this;
if (timezone == 'utc') {
this.t = parseDate(_t);
} else {
let t = new Timezone({
timezone,
time: _t
});
this.t = new Date(t.exec().manipulated);
}
return this;
}

}
}

function convertToString (time: any) {
if (typeof time == 'undefined') return undefined;
if (typeof time == 'string') return time;
return new Date(time).toISOString();
}
function parseDate (cfg: string | number | Date | undefined = undefined) {
if (cfg == undefined) return new Date(); //returns time now in UTC
if (cfg == null) return new Date(NaN); //invalid time
return new Date(cfg); //returns inputted time in UTC
}
function parseDateNow () {
let getDifferenceToUtcInMilisec = new Date().getTimezoneOffset() * MINUTES_TO_MILISECOND;
let getUTCMilisecond = new Date().getTime();

return new Date(getUTCMilisecond - getDifferenceToUtcInMilisec); //returns date based on server time
}
/**
*
* @param time
* time() without any parameter will return datetime now on UTC time
*/
function times (time: string | undefined = undefined) {
return new Time(time);
}

type AnyFunction = (...args: any) => any;

const wrap = <Func extends AnyFunction>(
fn: Func,
): ((...args: Parameters<Func>) => ReturnType<Func>) => {
const wrappedFn = (...args: Parameters<Func>): ReturnType<Func> => {
// your code here
return fn(...Array.from(args)).toString();
};
return wrappedFn;
};

times.prototype = wrap<typeof times>(times);

export { times };
60 changes: 60 additions & 0 deletions lib/timezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* MIT License
* Copyright (c) 2020 Burhanuddin Ahmed
*/

import { timezone as List } from "./api.ts";
import { privateReturn, TimezoneType } from "./type.ts";

const MINUTES_TO_MILISECOND = 60000;

export class Timezone {
public manipulated: string;
public userTime: string | undefined;
private timezone: string | undefined;

constructor ({timezone, time}: TimezoneType) {
this.userTime = time;
this.manipulated = '';
this.timezone = timezone;
}

public exec () {
let getUTCMilisecond = new Date().getTime();
if (this.userTime) {
getUTCMilisecond = new Date(this.userTime).getTime();
}

if (this.timezone) {
let addedtime = this.parseText(this.timezone);
if (!addedtime.status) {
throw addedtime.text;
}

// return manipulated timezone based on calculation additional / differentiation time
this.manipulated = new Date(getUTCMilisecond + Number(addedtime.text)).toISOString();
}
return this;
}

private parseText (textTimezone: string): privateReturn {
let obj = List.find(el => el.id.toLowerCase() == textTimezone.toLocaleLowerCase());
if (!obj) {
return {
status: false,
text: 'timezone you entered is not compatible'
};
}
let splittedObject = obj.text.split(' ');
let removeText = splittedObject[0]
.replace('(', '')
.replace(')', '')
.replace('GMT', '');
let getTimezoneHours = removeText.split(':');
let getMinutes = ((Number(getTimezoneHours[0]) * 60) + Number(getTimezoneHours[1])) * MINUTES_TO_MILISECOND;
return {
status: true,
text: `${getMinutes}`
}
}
}
17 changes: 16 additions & 1 deletion lib/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
export interface privateReturn {
status: boolean,
text: string
}
}

// export interface TimeType {
// time?: string
// }

export interface TimezoneType {
time?: string,
timezone?: string
}

// export interface TimeObject {
// toString: Function,
// current: string,
// tz: Function
// }
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Time } from './lib/time.ts';
export { times as time } from './lib/time.ts';
export { timezone } from './lib/api.ts';

0 comments on commit eeeb9f5

Please sign in to comment.