ProDOS-Utilities/prodos/time.go

69 lines
1.7 KiB
Go
Raw Normal View History

// Copyright Terence J. Boldt (c)2021-2023
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
// This file provides conversion to and from ProDOS time format
2021-06-06 03:22:30 +00:00
package prodos
import (
"time"
)
2022-01-23 22:30:18 +00:00
// DateTimeToProDOS converts Time to ProDOS date time
//
// 49041 ($BF91) 49040 ($BF90)
2022-01-23 22:30:18 +00:00
//
// 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
// +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
// DATE: | year | month | day |
// +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
2022-01-23 22:30:18 +00:00
//
// 49043 ($BF93) 49042 ($BF92)
//
// 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
// +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
// TIME: | hour | | minute |
// +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
2021-06-06 03:22:30 +00:00
func DateTimeToProDOS(dateTime time.Time) []byte {
year := dateTime.Year() % 100
month := dateTime.Month()
day := dateTime.Day()
hour := dateTime.Hour()
minute := dateTime.Minute()
buffer := make([]byte, 4)
buffer[0] = ((byte(month) & 15) << 5) + byte(day)
buffer[1] = (byte(year) << 1) + (byte(month) >> 3)
buffer[2] = byte(minute)
buffer[3] = byte(hour)
return buffer
}
2022-01-23 23:57:16 +00:00
// DateTimeFromProDOS converts Time from ProDOS date time
2021-06-06 03:22:30 +00:00
func DateTimeFromProDOS(buffer []byte) time.Time {
if buffer[0] == 0 &&
buffer[1] == 0 &&
buffer[2] == 0 &&
buffer[3] == 0 {
return time.Time{}
}
2021-06-06 03:22:30 +00:00
twoDigitYear := buffer[1] >> 1
var year int
if twoDigitYear < 76 {
year = 2000 + int(twoDigitYear)
} else {
year = 1900 + int(twoDigitYear)
}
2021-10-23 14:06:25 +00:00
month := int(buffer[0]>>5 + (buffer[1]&1)<<3)
2021-06-28 02:30:33 +00:00
day := int(buffer[0] & 31)
2021-06-06 03:22:30 +00:00
hour := int(buffer[3])
minute := int(buffer[2])
parsedTime := time.Date(year, time.Month(month), day, hour, minute, 0, 0, time.Local)
return parsedTime
}