Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

AI评测: 代码生成 #6

@bjwswang

Description

@bjwswang

Case 1 解析字符串,转为time.Time

  • Codeium vscode chat

Prompt

Code generation
I have a time in format 1days2hours3minutes4seconds which means 1 day 2 hours 3 minutes 4 seconds start from now, I want to convert it to a time.Time

Result

import (
    "errors"
    "fmt"
    "regexp"
    "strconv"
    "time"
)

func parseDuration(s string) (time.Time, error) {
    // Use regular expressions to extract the duration components
    r := regexp.MustCompile(`(\d+)days(\d+)hours(\d+)minutes(\d+)seconds`)
    matches := r.FindStringSubmatch(s)
    if len(matches) != 5 {
        return time.Time{}, errors.New("invalid duration format")
    }

    // Parse the duration components into integers
    days, _ := strconv.Atoi(matches[1])
    hours, _ := strconv.Atoi(matches[2])
    minutes, _ := strconv.Atoi(matches[3])
    seconds, _ := strconv.Atoi(matches[4])

    // Calculate the total duration in seconds
    totalSeconds := (days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60) + seconds

    // Get the current time and add the duration to it
    now := time.Now()
    duration := time.Duration(totalSeconds) * time.Second
    future := now.Add(duration)

    // Return the future time
    return future, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions