posted by 구로공돌이 2025. 5. 19. 13:53

import java.time.*;
import java.time.temporal.ChronoUnit;

public class WorkTimeCalculator {

    private static final LocalTime MIN_START = LocalTime.of(8, 30);
    private static final Duration DAILY_MAX = Duration.ofHours(8);
    private static final Duration MINIMUM_WORK_TIME = Duration.ofMinutes(6);

    public static Duration calculateWorkDuration(LocalDateTime start, LocalDateTime end) {
        if (end.isBefore(start)) return Duration.ZERO;

        Duration total = Duration.ZERO;
        LocalDate currentDate = start.toLocalDate();

        while (!currentDate.isAfter(end.toLocalDate())) {
            DayOfWeek day = currentDate.getDayOfWeek();
            if (day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY) {
                currentDate = currentDate.plusDays(1);
                continue;
            }

            Duration workDuration = Duration.ZERO;

            // 시작일 처리
            if (currentDate.equals(start.toLocalDate())) {
                LocalTime from = start.toLocalTime().isBefore(MIN_START) ? MIN_START : start.toLocalTime();
                LocalTime to = currentDate.equals(end.toLocalDate()) ? end.toLocalTime() : LocalTime.MAX;
                if (to.isBefore(from)) {
                    workDuration = Duration.ZERO;
                } else {
                    workDuration = Duration.between(from, to);
                }
            }

            // 종료일 처리
            else if (currentDate.equals(end.toLocalDate())) {
                LocalTime from = MIN_START;
                LocalTime to = end.toLocalTime();
                if (to.isAfter(from)) {
                    workDuration = Duration.between(from, to);
                }
            }

            // 중간일 처리
            else {
                workDuration = DAILY_MAX;
            }

            // 하루 최대 8시간 제한
            if (workDuration.compareTo(DAILY_MAX) > 0) {
                workDuration = DAILY_MAX;
            }

            total = total.plus(workDuration);
            currentDate = currentDate.plusDays(1);
        }

        // 최소 시간 보정
        if (total.compareTo(MINIMUM_WORK_TIME) < 0) {
            total = MINIMUM_WORK_TIME;
        }

        return total;
    }

    // 예시 테스트
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2025, 5, 15, 19, 0);
        LocalDateTime end = LocalDateTime.of(2025, 5, 16, 20, 0);
        Duration result = calculateWorkDuration(start, end);
        System.out.println("작업시간 (분): " + result.toMinutes());
    }
}

posted by 구로공돌이 2025. 5. 19. 13:39

import java.time.*;
import java.time.temporal.ChronoUnit;

public class WorkTimeCalculator {

    private static final LocalTime WORK_START = LocalTime.of(8, 30);
    private static final LocalTime WORK_END = LocalTime.of(18, 0);
    private static final Duration DAILY_MAX = Duration.ofHours(8);
    private static final Duration MINIMUM_WORK_TIME = Duration.ofMinutes(6); // 예시 6분 기준

    public static Duration calculateWorkDuration(LocalDateTime start, LocalDateTime end) {
        if (end.isBefore(start)) return Duration.ZERO;

        Duration total = Duration.ZERO;
        LocalDate currentDate = start.toLocalDate();

        while (!currentDate.isAfter(end.toLocalDate())) {
            DayOfWeek day = currentDate.getDayOfWeek();
            if (day != DayOfWeek.SATURDAY && day != DayOfWeek.SUNDAY) {

                // 시작일
                if (currentDate.equals(start.toLocalDate())) {
                    if (currentDate.equals(end.toLocalDate())) {
                        // 시작일 == 종료일
                        LocalTime from = start.toLocalTime();
                        LocalTime to = end.toLocalTime().isAfter(WORK_END) ? WORK_END : end.toLocalTime();
                        if (!to.isAfter(WORK_START)) continue;
                        if (from.isBefore(WORK_START)) from = WORK_START;
                        if (to.isAfter(WORK_END)) to = WORK_END;
                        Duration duration = Duration.between(from, to);
                        total = total.plus(duration);
                    } else {
                        // 시작일만
                        Duration duration = Duration.between(start.toLocalTime(), WORK_END);
                        if (duration.isNegative()) continue;
                        total = total.plus(duration.compareTo(DAILY_MAX) > 0 ? DAILY_MAX : duration);
                    }
                }

                // 종료일
                else if (currentDate.equals(end.toLocalDate())) {
                    LocalTime from = WORK_START;
                    LocalTime to = end.toLocalTime().isAfter(WORK_END) ? WORK_END : end.toLocalTime();
                    if (!to.isAfter(from)) continue;
                    Duration duration = Duration.between(from, to);
                    total = total.plus(duration.compareTo(DAILY_MAX) > 0 ? DAILY_MAX : duration);
                }

                // 중간 평일
                else {
                    total = total.plus(DAILY_MAX);
                }
            }

            currentDate = currentDate.plusDays(1);
        }

        // 최소 시간 보정
        if (total.compareTo(MINIMUM_WORK_TIME) < 0) {
            total = MINIMUM_WORK_TIME;
        }

        return total;
    }

    // 테스트 예시
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2025, 5, 15, 7, 0);
        LocalDateTime end = LocalDateTime.of(2025, 5, 15, 10, 0);
        Duration result = calculateWorkDuration(start, end);
        System.out.println("작업시간 (분): " + result.toMinutes());
    }
}

posted by 구로공돌이 2025. 5. 15. 09:49

<!DOCTYPE html>
<html lang="ko">
<head>
  <meta charset="UTF-8">
  <title>AL32UTF8 문자 바이트 계산기</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 40px; }
    textarea { width: 100%; height: 100px; font-size: 16px; }
    .result { margin-top: 20px; font-size: 18px; }
  </style>
</head>
<body>
  <h2>AL32UTF8 문자 바이트 계산기</h2>
  <textarea id="inputText" placeholder="여기에 문자열을 입력하세요..."></textarea>
  <div class="result" id="resultBox">총 문자 수: 0 / 예상 바이트 수: 0</div>

  <script>
    const input = document.getElementById('inputText');
    const resultBox = document.getElementById('resultBox');

    input.addEventListener('input', () => {
      const text = input.value;
      let byteCount = 0;

      for (const char of text) {
        const code = char.codePointAt(0);
        if (code <= 0x7F) {
          byteCount += 1; // ASCII
        } else if (code <= 0x7FF) {
          byteCount += 2; // Latin, 한글 자음/모음 단독 등
        } else if (code <= 0xFFFF) {
          byteCount += 3; // 대부분의 한글, 한자
        } else {
          byteCount += 4; // 이모지, 특수 문자
        }
      }

      const charCount = [...text].length;
      resultBox.innerText = `총 문자 수: ${charCount} / 예상 바이트 수: ${byteCount}`;
    });
  </script>
</body>
</html>