Blun.Build.Tasks.BuildTimeStamp 1.0.1

dotnet add package Blun.Build.Tasks.BuildTimeStamp --version 1.0.1
                    
NuGet\Install-Package Blun.Build.Tasks.BuildTimeStamp -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blun.Build.Tasks.BuildTimeStamp" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Blun.Build.Tasks.BuildTimeStamp" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Blun.Build.Tasks.BuildTimeStamp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Blun.Build.Tasks.BuildTimeStamp --version 1.0.1
                    
#r "nuget: Blun.Build.Tasks.BuildTimeStamp, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Blun.Build.Tasks.BuildTimeStamp@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Blun.Build.Tasks.BuildTimeStamp&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Blun.Build.Tasks.BuildTimeStamp&version=1.0.1
                    
Install as a Cake Tool

Blun.Build.Tasks.BuildTimeStamp

NuGet Version NuGet Downloads Issues License Repo Gitea Stars

πŸ“‹ Introduction

A small MSBuild Task that inserts a build timestamp into assembly metadata during the build process. This allows applications to read the build time at runtime.

The package adds an assembly-level System.Reflection.AssemblyMetadataAttribute with key Blun.Build.Tasks.BuildTimeStamp_UTC and an ISO 8601 UTC timestamp value. The package also ships MSBuild .props/.targets files to integrate automatically when the NuGet package is referenced.

Supported Frameworks for MSBuild Task

  • .NET Standard 2.0 and newer
  • .NET Framework 4.7.2 and newer

⭐ Features

  • πŸ•’ Automatic Timestamp: Writes AssemblyMetadata("Blun.Build.Tasks.BuildTimeStamp_UTC", "<ISO8601 UTC timestamp>") at build time
  • πŸ“¦ Build-Time Generation: Timestamp is captured during the build process, reflecting the actual build artifact creation time
  • πŸ”§ Extension Method: Provides an auto-generated GetBuildTimeStampUtc() extension method for easy runtime access
  • 🎯 Zero Configuration: Works automatically once the NuGet package is installed
  • βœ… ISO 8601 Format: Timestamps are stored in standardized ISO 8601 UTC format for easy parsing and interoperability

πŸš€ Installation & Quick Start

Via NuGet Package Manager

Install the NuGet package Blun.Build.Tasks.BuildTimeStamp:

# using the .NET CLI
dotnet add package Blun.Build.Tasks.BuildTimeStamp

Or via NuGet Package Manager in Visual Studio:

Install-Package Blun.Build.Tasks.BuildTimeStamp

Auto-Integration

Once installed, the included build/Blun.Build.Tasks.BuildTimeStamp.props and build/Blun.Build.Tasks.BuildTimeStamp.targets are imported automatically by the SDK-style project system. No additional configuration is required.

To prevent this build-only task from being included as a runtime dependency in other projects that reference your project, add PrivateAssets="all" to the package reference:

<ItemGroup>
  <PackageReference Include="Blun.Build.Tasks.BuildTimeStamp" Version="1.0.1" PrivateAssets="all" />
</ItemGroup>

Important: Set parameter for build task! (optional)

<PropertyGroup>
  
  <BlunAccessModifierForGeneratedClass>public</BlunAccessModifierForGeneratedClass>
  
  
  <BlunGeneratedClass>true</BlunGeneratedClass>
</PropertyGroup>

πŸ”§ How It Works

What Happens at Build Time

The MSBuild task automatically generates two C# source files in your project:

  1. /opt/**/Blun.Build.Tasks.BuildTimeStamp.generated.cs β€” Contains the assembly metadata attribute with the build timestamp - prebuild file
  2. Blun.Build.Tasks.BuildTimeStamp.generated.cs β€” Contains an extension method GetBuildTimeStampUtc() for easy runtime access

Assembly Metadata Attribute

The build task injects an AssemblyMetadataAttribute into your assembly:

[assembly: System.Reflection.AssemblyMetadata("Blun.Build.Tasks.BuildTimeStamp_UTC", "2025-01-15T10:30:45.1234567+00:00")]

Example Generated Files

The generated code is similar to:


<ItemGroup>
  <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
    <_Parameter1>Blun.Build.Tasks.BuildTimeStamp_UTC</_Parameter1>
    <_Parameter2>$([System.DateTimeOffset]::UtcNow.ToString('o'))</_Parameter2>
  </AssemblyAttribute>
</ItemGroup>

βœ… Usage at Runtime

The generated Blun.Build.Tasks.BuildTimeStamp.generated.cs file provides an extension method. Simply call it on any assembly:

using System.Reflection;

var buildTimeStamp = typeof(Program).Assembly.GetBuildTimeStampUtc();
if (buildTimeStamp.HasValue)
{
    Console.WriteLine($"Build Time (UTC): {buildTimeStamp:O}");
}
else
{
    Console.WriteLine("No build timestamp found");
}

Alternative: Direct Reflection

If you prefer to access the metadata directly without the extension method:

using System.Reflection;

var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var metadataAttributes = assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
var buildTime = metadataAttributes
    .FirstOrDefault(a => a.Key == "Blun.Build.Tasks.BuildTimeStamp_UTC")
    ?.Value;

Console.WriteLine(buildTime ?? "(no build timestamp found)");

Parsing the Timestamp

The timestamp is stored in ISO 8601 format (UTC). Parse it using:

var buildTimeStamp = typeof(Program).Assembly.GetBuildTimeStampUtc();
if (buildTimeStamp.HasValue)
{
    // Already parsed as DateTimeOffset
    DateTime buildDateTime = buildTimeStamp.Value.DateTime;
    Console.WriteLine($"Build Date: {buildDateTime:yyyy-MM-dd}");
    Console.WriteLine($"Build Time: {buildDateTime:HH:mm:ss}");
}

Notes

  • The timestamp is in UTC and includes timezone information
  • The format is ISO 8601 round-trip format (O), preserving full precision
  • Timestamps can be compared, formatted, or serialized without additional parsing

πŸ“š Example Project

The example/Blun.TestBuildApp directory contains a working example that demonstrates:

  • βœ… Installing the Blun.Build.Tasks.BuildTimeStamp NuGet package
  • βœ… Reading the build timestamp at runtime using the extension method
  • βœ… Formatting and displaying the timestamp

Example Output:

Build Time (UTC): 2025-12-06T09:53:28.5041962+00:00
Build Date (local): 2025-12-06
Build Time (local): 10:53:28

See the full example:

🀝 Contributing

Contributions are welcome! Here are some ways you can help:

  • πŸ“ Documentation: Improve guides, fix typos, add examples
  • πŸ§ͺ Tests: Add new tests, improve test coverage, fix failing tests
  • πŸ› Issues: Report bugs or request features via the issue tracker
  • πŸ’» Code: Submit pull requests with improvements

Guidelines

  • Follow the existing code style and conventions
  • Add or update tests for any behavior changes
  • Update documentation as needed
  • Use clear commit messages

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file in the repository root for full details.


Author: BjΓΆrn LundstrΓΆm (BLun78)
Repository: https://codeberg.org/BLun78/Blun.Build.Tasks.BuildTimeStamp
NuGet Package: https://www.nuget.org/packages/Blun.Build.Tasks.BuildTimeStamp

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

First release, provide a MSBuild Task how adds a System.Reflection.AssemblyMetadataAttribute with the BuildTimeStamp.

To read them do:
using System.Reflection;

var buildTimeStamp = typeof(Program).Assembly.GetBuildTimeStampUtc();
if (buildTimeStamp.HasValue)
{
Console.WriteLine($"Build Time (UTC): {buildTimeStamp:O}");
}
else
{
Console.WriteLine("No build timestamp found");
}