MCPcopy
hub / github.com/spencermountain/spacetime / time

Function time

builds/spacetime.cjs:2919–2961  ·  view source on GitHub ↗
(s, str, goFwd)

Source from the content-addressed store, hash-verified

2917 };
2918
2919 const time = function (s, str, goFwd) {
2920 let m = str.match(/([0-9]{1,2})[:h]([0-9]{1,2})(:[0-9]{1,2})? ?(am|pm)?/);
2921 if (!m) {
2922 //fallback to support just '2am'
2923 m = str.match(/([0-9]{1,2}) ?(am|pm)/);
2924 if (!m) {
2925 return null // invalid time string - mark as invalid
2926 }
2927 m.splice(2, 0, '0'); //add implicit 0 minutes
2928 m.splice(3, 0, ''); //add implicit seconds
2929 }
2930 let h24 = false;
2931 let hour = parseInt(m[1], 10);
2932 let minute = parseInt(m[2], 10);
2933 if (minute >= 60) {
2934 minute = 59;
2935 }
2936 if (hour > 12) {
2937 h24 = true;
2938 }
2939 //make the hour into proper 24h time
2940 if (h24 === false) {
2941 if (m[4] === 'am' && hour === 12) {
2942 //12am is midnight
2943 hour = 0;
2944 }
2945 if (m[4] === 'pm' && hour < 12) {
2946 //12pm is noon
2947 hour += 12;
2948 }
2949 }
2950 // handle seconds
2951 m[3] = m[3] || '';
2952 m[3] = m[3].replace(/:/, '');
2953 const sec = parseInt(m[3], 10) || 0;
2954 const old = s.clone();
2955 s = s.hour(hour);
2956 s = s.minute(minute);
2957 s = s.second(sec);
2958 s = s.millisecond(0);
2959 s = fwdBkwd(s, old, goFwd, 'day'); // specify direction
2960 return s.epoch
2961 };
2962
2963 const date = function (s, n, goFwd) {
2964 n = validate(n);

Callers 1

spacetime.cjsFile · 0.70

Calls 5

hourMethod · 0.80
minuteMethod · 0.80
secondMethod · 0.80
millisecondMethod · 0.80
fwdBkwdFunction · 0.70

Tested by

no test coverage detected