Last active
July 14, 2023 14:51
-
-
Save beigirad/b9d6da1e91d7b03d2d4d04f06a3c948e to your computer and use it in GitHub Desktop.
working timesheet csv generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter | |
import java.io.File | |
import java.io.FileWriter | |
import java.io.Writer | |
import kotlin.random.Random | |
val weekOffset = 5 | |
val holidays = setOf(8, 16) | |
val month = 4 | |
val year = 1402 | |
val weekdays = listOf("شنبه", "یکشنبه", "دوشنبه", "سهشنبه", "چهارشنبه", "پنجشنبه", "جمعه") | |
val file = File("/Users/farhad/Desktop/sheet$month.csv") | |
if (file.exists().not()) | |
file.createNewFile() | |
BufferedWriter(FileWriter(file)).use { | |
it.write("\"Week\"") | |
it.writeComma() | |
it.write("\"Date\"") | |
it.writeComma() | |
it.write("\"Start\"") | |
it.writeComma() | |
it.write("\"End\"") | |
it.writeComma() | |
it.write("\"Diff\"") | |
it.writeComma() | |
it.write("\"Desc\"") | |
(1..31).map { day -> | |
val weekday = (day.rem(7) + weekOffset - 1).rem(7) | |
val start = when { | |
day in holidays || weekday == 5 || weekday == 6 -> null | |
else -> (Time(8, 0) + minToTime(Random.nextInt(0, 70))) | |
} | |
val end = when { | |
day in holidays || weekday == 5 || weekday == 6 -> null | |
else -> (Time(16, 50) + minToTime(Random.nextInt(0, 80))) | |
} | |
it.newLine() | |
it.write(weekdays[weekday]) | |
it.writeComma() | |
it.write("%d/%02d/%02d".format(year, month, day)) | |
it.writeComma() | |
it.write(start?.formatted.orEmpty()) | |
it.writeComma() | |
it.write(end?.formatted.orEmpty()) | |
it.writeComma() | |
it.write(start?.let { end?.minus(it) }?.formatted.orEmpty()) | |
it.writeComma() | |
it.write("\"تعطیل رسمی\"".takeIf { day in holidays }.orEmpty()) | |
} | |
} | |
fun minToTime(min: Int): Time { | |
return Time(min / 60, min.rem(60)) | |
} | |
data class Time( | |
val hour: Int, | |
val minute: Int | |
) : Comparable<Time> { | |
val formatted: String get() = "%02d:%02d".format(hour, minute) | |
override fun compareTo(other: Time): Int { | |
if (hour < other.hour) return -1 | |
if (hour == other.hour) { | |
if (minute < other.minute) return -1 | |
return if (minute > other.minute) 1 else 0 | |
} | |
return 1 | |
} | |
operator fun plus(value: Time): Time { | |
val newHour = (minute + value.minute) / 60 | |
val remMinute = (minute + value.minute) % 60 | |
return Time( | |
(hour + value.hour + newHour), | |
remMinute | |
) | |
} | |
operator fun minus(value: Time): Time { | |
val totalMin = (hour * 60 + minute) - (value.hour * 60 + value.minute) | |
val newHour = (totalMin) / 60 | |
val newMinute = (totalMin) % 60 | |
return Time(newHour, newMinute) | |
} | |
} | |
fun Writer.writeComma() { | |
write(",") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment