mcp-wtit
Version:
A Model Context Protocol (MCP) server that provides current time in ISO8601 format with timezone support
79 lines • 3.16 kB
JavaScript
import { InvalidTimeZoneError, TimeFormatError } from '../../shared/errors/time.errors.js';
export class TimeService {
defaultOptions = {
includeMilliseconds: true,
timezone: 'UTC',
};
getCurrentTime(options) {
const mergedOptions = { ...this.defaultOptions, ...options };
const now = new Date();
return {
iso8601: this.formatToISO8601(now, mergedOptions),
timestamp: now.getTime(),
timezone: mergedOptions.timezone || this.getTimeZone(),
};
}
getCurrentTimeISO8601(options) {
const mergedOptions = { ...this.defaultOptions, ...options };
return this.formatToISO8601(new Date(), mergedOptions);
}
getCurrentTimestamp() {
return Date.now();
}
getTimeZone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}
formatToISO8601(date, options) {
try {
if (options.timezone && options.timezone !== 'UTC') {
return this.formatWithTimeZone(date, options.timezone, options.includeMilliseconds);
}
const iso = date.toISOString();
return options.includeMilliseconds ? iso : iso.replace(/\.\d{3}/, '');
}
catch (error) {
throw new TimeFormatError(`Failed to format date: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
formatWithTimeZone(date, timezone, includeMilliseconds) {
try {
const formatter = new Intl.DateTimeFormat('en-US', {
timeZone: timezone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
});
const parts = formatter.formatToParts(date);
const dateParts = {};
parts.forEach(part => {
if (part.type !== 'literal') {
dateParts[part.type] = part.value;
}
});
let iso = `${dateParts.year}-${dateParts.month}-${dateParts.day}T${dateParts.hour}:${dateParts.minute}:${dateParts.second}`;
if (includeMilliseconds) {
iso += `.${date.getMilliseconds().toString().padStart(3, '0')}`;
}
const offset = this.getTimeZoneOffset(date, timezone);
iso += offset;
return iso;
}
catch {
throw new InvalidTimeZoneError(timezone);
}
}
getTimeZoneOffset(date, timezone) {
const utcDate = new Date(date.toLocaleString('en-US', { timeZone: 'UTC' }));
const tzDate = new Date(date.toLocaleString('en-US', { timeZone: timezone }));
const offset = (tzDate.getTime() - utcDate.getTime()) / 60000;
const hours = Math.floor(Math.abs(offset) / 60);
const minutes = Math.abs(offset) % 60;
const sign = offset >= 0 ? '+' : '-';
return `${sign}${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;
}
}
//# sourceMappingURL=time.service.js.map