clockey 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clockey",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "API-first time utilities for backend developers",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
package/src/core/date.ts CHANGED
@@ -1,33 +1,34 @@
1
+ const DAY_NAMES = [
2
+ "Sunday",
3
+ "Monday",
4
+ "Tuesday",
5
+ "Wednesday",
6
+ "Thursday",
7
+ "Friday",
8
+ "Saturday",
9
+ ];
10
+
11
+ const MONTH_NAMES = [
12
+ "January",
13
+ "February",
14
+ "March",
15
+ "April",
16
+ "May",
17
+ "June",
18
+ "July",
19
+ "August",
20
+ "September",
21
+ "October",
22
+ "November",
23
+ "December",
24
+ ];
25
+
1
26
  export function formatCurrentDate() {
2
27
  const currentDate = new Date();
3
- const dayAsString = [
4
- "Sunday",
5
- "Monday",
6
- "Tuesday",
7
- "Wednesday",
8
- "Thursday",
9
- "Friday",
10
- "Saturday",
11
- ];
12
-
13
- const monthAsString = [
14
- "January",
15
- "February",
16
- "March",
17
- "April",
18
- "May",
19
- "June",
20
- "July",
21
- "August",
22
- "September",
23
- "October",
24
- "November",
25
- "December",
26
- ];
27
28
 
28
29
  const yr = currentDate.getFullYear();
29
- const month = monthAsString[currentDate.getMonth()];
30
- const day = dayAsString[currentDate.getDay()];
30
+ const month = MONTH_NAMES[currentDate.getMonth()];
31
+ const day = DAY_NAMES[currentDate.getDay()];
31
32
  const date = currentDate.getDate();
32
33
  const ordinal =
33
34
  date === 1 ? "st" : date === 2 ? "nd" : date === 3 ? "rd" : "th";
@@ -38,3 +39,53 @@ export function formatCurrentDate() {
38
39
  date: { yr, month, day, date, ordinal, fullDate },
39
40
  };
40
41
  }
42
+
43
+ export function currentYear() {
44
+ return new Date().getFullYear();
45
+ }
46
+
47
+ export function currentMonth() {
48
+ const now = new Date();
49
+ const idx = now.getMonth();
50
+ const monthNumber = idx + 1;
51
+ return {
52
+ monthNumber,
53
+ monthName: MONTH_NAMES[idx],
54
+ monthStr: monthNumber.toString().padStart(2, "0"),
55
+ };
56
+ }
57
+
58
+ export function currentDay() {
59
+ const now = new Date();
60
+ const idx = now.getDay();
61
+ return {
62
+ dayIndex: idx,
63
+ dayName: DAY_NAMES[idx],
64
+ };
65
+ }
66
+
67
+ export function currentDateNumber() {
68
+ const d = new Date().getDate();
69
+ return {
70
+ date: d,
71
+ dateStr: d.toString().padStart(2, "0"),
72
+ };
73
+ }
74
+
75
+ export function currentOrdinal() {
76
+ const d = new Date().getDate();
77
+ const ordinal = d === 1 ? "st" : d === 2 ? "nd" : d === 3 ? "rd" : "th";
78
+ return { ordinal };
79
+ }
80
+
81
+ export function currentFullDate() {
82
+ const now = new Date();
83
+ const yr = now.getFullYear();
84
+ const month = MONTH_NAMES[now.getMonth()];
85
+ const day = DAY_NAMES[now.getDay()];
86
+ const date = now.getDate();
87
+ const ordinal =
88
+ date === 1 ? "st" : date === 2 ? "nd" : date === 3 ? "rd" : "th";
89
+ const fullDate = `${month} ${date}${ordinal}, ${day}, ${yr}`;
90
+ return { fullDate };
91
+ }
package/src/core/time.ts CHANGED
@@ -1,28 +1,80 @@
1
+ const pad = (n: number) => n.toString().padStart(2, "0");
2
+
1
3
  export function formatCurrentTime() {
2
- const currentTime = new Date();
4
+ const now = new Date();
3
5
 
4
- let hrs24 = currentTime.getHours();
5
- const min = currentTime.getMinutes();
6
- const sec = currentTime.getSeconds();
6
+ const hr24 = now.getHours();
7
+ const min = now.getMinutes();
8
+ const sec = now.getSeconds();
7
9
 
8
- const period = hrs24 >= 12 ? "PM" : "AM";
9
- let hrs12 = hrs24 % 12;
10
- if (hrs12 === 0) hrs12 = 12;
10
+ const period = hr24 >= 12 ? "PM" : "AM";
11
+ const hr12 = hr24 % 12 || 12;
11
12
 
12
- const pad = (n: number) => n.toString().padStart(2, "0");
13
+ return {
14
+ hr24,
15
+ hr12,
16
+ min,
17
+ sec,
18
+ period,
19
+ hr24Str: pad(hr24),
20
+ hr12Str: pad(hr12),
21
+ minStr: pad(min),
22
+ secStr: pad(sec),
23
+ timeAsString24: `${pad(hr24)}:${pad(min)}:${pad(sec)}`,
24
+ timeAsString12: `${pad(hr12)}:${pad(min)}:${pad(sec)} ${period}`,
25
+ };
26
+ }
27
+
28
+ export function currentHour() {
29
+ const currentTime = new Date();
30
+ const hr24 = currentTime.getHours();
31
+ const hr12 = hr24 % 12 || 12;
13
32
 
14
- const timeAsString24 = `${pad(hrs24)}:${pad(min)}:${pad(sec)}`;
15
- const timeAsString12 = `${pad(hrs12)}:${pad(min)}:${pad(sec)} ${period}`;
33
+ return {
34
+ hour24: hr24,
35
+ hour12: hr12,
36
+ hour24Str: pad(hr24),
37
+ hour12Str: pad(hr12),
38
+ };
39
+ }
40
+
41
+ export function currentMinute() {
42
+ const currentTime = new Date();
43
+ const minute = currentTime.getMinutes();
44
+ return {
45
+ minute,
46
+ minuteStr: pad(minute),
47
+ };
48
+ }
16
49
 
50
+ export function currentSecond() {
51
+ const currentTime = new Date();
52
+ const second = currentTime.getSeconds();
17
53
  return {
18
- time: {
19
- hrs24,
20
- hrs12,
21
- min,
22
- sec,
23
- period,
24
- timeAsString24,
25
- timeAsString12,
26
- },
54
+ second,
55
+ secondStr: pad(second),
27
56
  };
28
57
  }
58
+
59
+ // String Based Responses
60
+ export function currentHour24Str() {
61
+ return currentHour().hour24Str;
62
+ }
63
+
64
+ export function currentHour12Str() {
65
+ return currentHour().hour12Str;
66
+ }
67
+
68
+ export function currentMinuteStr() {
69
+ return currentMinute().minuteStr;
70
+ }
71
+
72
+ export function currentSecondStr() {
73
+ return currentSecond().secondStr;
74
+ }
75
+
76
+ export function currentPeriod() {
77
+ const hr24 = new Date().getHours();
78
+ const period = hr24 >= 12 ? "PM" : "AM";
79
+ return { period };
80
+ }
@@ -1,4 +1,12 @@
1
- import { formatCurrentDate } from "../core/date";
1
+ import {
2
+ formatCurrentDate,
3
+ currentYear,
4
+ currentMonth,
5
+ currentDay,
6
+ currentDateNumber,
7
+ currentOrdinal,
8
+ currentFullDate,
9
+ } from "../core/date";
2
10
  import { ClockeyDateResponse } from "../types/date";
3
11
 
4
12
  export function getCurrentDate() {
@@ -11,3 +19,87 @@ export function getCurrentDate() {
11
19
  };
12
20
  return response;
13
21
  }
22
+
23
+ export function getCurrentYear() {
24
+ return currentYear();
25
+ }
26
+
27
+ export function getCurrentYearResponse() {
28
+ const value = currentYear();
29
+ return {
30
+ success: true,
31
+ code: 200,
32
+ msg: "Current year fetched successfully",
33
+ value,
34
+ };
35
+ }
36
+
37
+ export function getCurrentMonth() {
38
+ return currentMonth();
39
+ }
40
+
41
+ export function getCurrentMonthResponse() {
42
+ const value = currentMonth();
43
+ return {
44
+ success: true,
45
+ code: 200,
46
+ msg: "Current month fetched successfully",
47
+ value,
48
+ };
49
+ }
50
+
51
+ export function getCurrentDay() {
52
+ return currentDay();
53
+ }
54
+
55
+ export function getCurrentDayResponse() {
56
+ const value = currentDay();
57
+ return {
58
+ success: true,
59
+ code: 200,
60
+ msg: "Current day fetched successfully",
61
+ value,
62
+ };
63
+ }
64
+
65
+ export function getCurrentDateNumber() {
66
+ return currentDateNumber();
67
+ }
68
+
69
+ export function getCurrentDateNumberResponse() {
70
+ const value = currentDateNumber();
71
+ return {
72
+ success: true,
73
+ code: 200,
74
+ msg: "Current date number fetched successfully",
75
+ value,
76
+ };
77
+ }
78
+
79
+ export function getCurrentOrdinal() {
80
+ return currentOrdinal();
81
+ }
82
+
83
+ export function getCurrentOrdinalResponse() {
84
+ const value = currentOrdinal();
85
+ return {
86
+ success: true,
87
+ code: 200,
88
+ msg: "Current ordinal fetched successfully",
89
+ value,
90
+ };
91
+ }
92
+
93
+ export function getCurrentFullDate() {
94
+ return currentFullDate();
95
+ }
96
+
97
+ export function getCurrentFullDateResponse() {
98
+ const value = currentFullDate();
99
+ return {
100
+ success: true,
101
+ code: 200,
102
+ msg: "Current full date fetched successfully",
103
+ value,
104
+ };
105
+ }
@@ -1,8 +1,24 @@
1
- import { formatCurrentTime } from "../core/time";
1
+ import {
2
+ currentHour,
3
+ formatCurrentTime,
4
+ currentMinute,
5
+ currentSecond,
6
+ currentHour24Str,
7
+ currentHour12Str,
8
+ currentMinuteStr,
9
+ currentSecondStr,
10
+ currentPeriod,
11
+ } from "../core/time";
2
12
  import { ClockeyTimeResponse } from "../types/time";
3
13
 
4
14
  export function getCurrentTime() {
5
- const { time } = formatCurrentTime();
15
+ const { timeAsString24, timeAsString12 } = formatCurrentTime();
16
+
17
+ return { timeAsString12, timeAsString24 };
18
+ }
19
+
20
+ export function getCurrentTimeResponse() {
21
+ const time = formatCurrentTime();
6
22
  const response: ClockeyTimeResponse = {
7
23
  success: true,
8
24
  code: 200,
@@ -11,3 +27,117 @@ export function getCurrentTime() {
11
27
  };
12
28
  return response;
13
29
  }
30
+
31
+ export function getCurrentHr() {
32
+ return currentHour();
33
+ }
34
+
35
+ export function getCurrenHrResponse() {
36
+ const hour = currentHour();
37
+ const response = {
38
+ success: true,
39
+ code: 200,
40
+ msg: "Current hour fetched successfully",
41
+ hour,
42
+ };
43
+ return response;
44
+ }
45
+
46
+ export function getCurrentMinute() {
47
+ return currentMinute();
48
+ }
49
+
50
+ export function getCurrentMinuteResponse() {
51
+ const minute = currentMinute();
52
+ return {
53
+ success: true,
54
+ code: 200,
55
+ msg: "Current minute fetched successfully",
56
+ minute,
57
+ };
58
+ }
59
+
60
+ export function getCurrentSecond() {
61
+ return currentSecond();
62
+ }
63
+
64
+ export function getCurrentSecondResponse() {
65
+ const second = currentSecond();
66
+ return {
67
+ success: true,
68
+ code: 200,
69
+ msg: "Current second fetched successfully",
70
+ second,
71
+ };
72
+ }
73
+
74
+ // String data type responses
75
+ export function getCurrentHour24Str() {
76
+ return currentHour24Str();
77
+ }
78
+
79
+ export function getCurrentHour24StrResponse() {
80
+ const value = currentHour24Str();
81
+ return {
82
+ success: true,
83
+ code: 200,
84
+ msg: "Current hour24 string fetched",
85
+ value,
86
+ };
87
+ }
88
+
89
+ export function getCurrentHour12Str() {
90
+ return currentHour12Str();
91
+ }
92
+
93
+ export function getCurrentHour12StrResponse() {
94
+ const value = currentHour12Str();
95
+ return {
96
+ success: true,
97
+ code: 200,
98
+ msg: "Current hour12 string fetched",
99
+ value,
100
+ };
101
+ }
102
+
103
+ export function getCurrentMinuteStr() {
104
+ return currentMinuteStr();
105
+ }
106
+
107
+ export function getCurrentMinuteStrResponse() {
108
+ const value = currentMinuteStr();
109
+ return {
110
+ success: true,
111
+ code: 200,
112
+ msg: "Current minute string fetched",
113
+ value,
114
+ };
115
+ }
116
+
117
+ export function getCurrentSecondStr() {
118
+ return currentSecondStr();
119
+ }
120
+
121
+ export function getCurrentSecondStrResponse() {
122
+ const value = currentSecondStr();
123
+ return {
124
+ success: true,
125
+ code: 200,
126
+ msg: "Current second string fetched",
127
+ value,
128
+ };
129
+ }
130
+
131
+ export function getCurrentPeriod() {
132
+ return currentPeriod();
133
+ }
134
+
135
+ export function getCurrentPeriodResponse() {
136
+ const period = currentPeriod();
137
+ return {
138
+ success: true,
139
+ code: 200,
140
+ msg: "Current period fetched successfully",
141
+ period,
142
+ };
143
+ }
@@ -1,6 +1,6 @@
1
1
  export interface TimeData {
2
- hrs24: number;
3
- hrs12: number;
2
+ hr24: number;
3
+ hr12: number;
4
4
  min: number;
5
5
  sec: number;
6
6
  period: "AM" | "PM" | string;